Search code examples
compiler-constructioncompilationlexical-analysislexical

Can lexical analyzer stage check grammar rules during compilation?


sorry for such silly question, but I had argument with my pals about lexical analyze and we've decided to ask community.

The question is: Whether the statement "int some_variable = ;" would be interpreted as invalid during the lexical analyze stage or during the syntax analyze stage in C grammar. Thanks


Solution

  • In C, lexical analysis occurs first. Then the preprocessor applies macros and all its magical transforms on the resulting stream of tokens. Only after the preprocessor has acted does syntax analysis take place.

    Thus, to know the answer to your question, just run the code in the preprocessor. With gcc this is a matter of using the -E command-line flag. If the preprocessor is happy then the lexical analysis, by definition, went fine (which is the case for your example).