views:

103

answers:

3

Hi guys, 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

A: 

Lexical analysis checks that all of your tokens are valid (they are). Parsing (or syntax analysis) checks whether the sequence of tokens forms a valid production in your grammar (it doesn't). So this would pass the lexical analysis phase and fail the parse phase.

danben
Syntax analysis == Parsing.
Mehrdad Afshari
Yep, I thought of that as I was typing.
danben
A: 

During the syntactic analysis phase, aka parsing

Sam Meldrum
+1  A: 

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).

Thomas Pornin