The semantic phase of the compiler

I am trying to make a semantic phase for c compiler using lex and yacc. Right now the problem is that I have a few errors in program c, it stops after the 1st. What can I do?

+4
source share
2 answers

I highly recommend that you perform semantic analysis as a separate step, and not as part of the parsing phase. Use YACC only to create an abstract syntax tree, then cross that tree in a separate function. The mentioned function will have unlimited freedom when it comes to moving around the tree, as opposed to having to “follow the parsing”. Regarding the specific issue you were talking about, the @pmg comment seems to have identified the issue.

+2
source

There is no absolute answer to this question. A typical processing method is to create a special pattern for reading characters until it reaches (for example) a semicolon at the end of the line, giving a reasonable signal that everything after that is intended as a new declaration, definition, statement, etc. .d., and then re-parse the parsing from this point (keeping enough context to know that, for example, you are currently parsing the function body, so you accept / reject the input on this basis).

+1
source

Source: https://habr.com/ru/post/1346469/


All Articles