Question

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

Was it helpful?

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

OTHER TIPS

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.

During the syntactic analysis phase, aka parsing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top