Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 908
Raulp Avatar asked Jun 23 '12 19:06

Raulp


People also ask

What is syntactic analysis in compiler design?

Syntax AnalyzersA syntax analyzer or parser takes the input from a lexical analyzer in the form of token streams. The parser analyzes the source code (token stream) against the production rules to detect any errors in the code. The output of this phase is a parse tree.

What is lexical analysis in compiler design?

Lexical analysis is the starting phase of the compiler. It gathers modified source code that is written in the form of sentences from the language preprocessor. The lexical analyzer is responsible for breaking these syntaxes into a series of tokens, by removing whitespace in the source code.

What is lexical syntactical analysis?

The authors define it this way, “Lexical-syntactical analysis is the study of the meaning of individual words (lexicology) and the way those words are combined (syntax) in order to determine more accurately the author's intended meaning.” Individually words have meaning and this has a bearing on the meaning of any ...

What happens during the lexical analysis phase of compilation?

Lexical analysis is the first phase of a compiler. It takes modified source code from language preprocessors that are written in the form of sentences. The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the source code.


1 Answers

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.

like image 192
Anudeep Bulla Avatar answered Sep 18 '22 13:09

Anudeep Bulla