What is the lexical and syntactic analysis during the process of compiling in C Compiler?

Raulp picture Raulp · Jun 23, 2012 · Viewed 8.8k times · Source

What is the lexical and syntactic analysis during the process of compiling. Does the preprocessing happens after lexical and syntactic analysis ?

Answer

Anudeep Bulla picture Anudeep Bulla · Jun 23, 2012

Consider this code:

     int a = 10;
     if (a < 4)
     {
          printf("%d", a);
     }

In the Lexical Analysis phase: You identify each word/token and assign a meaning to it. In the code above, you start by identifying that i followed by n followed by t and then a space is the word int, and that it is a language keyword;1 followed by 0 and a space is a number 10 and so on.

In the Syntactic Analysis phase: You verify whether the code follows the language syntax(grammar rules). For example, you check whether there is only one variable on the LHS of an operator(considering language C), that each statement is terminated by a ;, that if is followed by a conditional/Boolean statement etc.

Like others have mentioned, usually, preprocessing happens before lexical analysis or syntactical analysis.