What is lexical analysis and syntax analysis in a compiler?
What is lexical analysis and syntax analysis in a compiler?
36007-Apr-2023
Updated on 08-Apr-2023
Home / DeveloperSection / Forums / What is lexical analysis and syntax analysis in a compiler?
What is lexical analysis and syntax analysis in a compiler?
Krishnapriya Rajeev
07-Apr-2023Lexical analysis and syntax analysis are two phases of the compilation process that help translate a high-level programming language into machine code that a computer can understand.
Lexical analysis, also known as scanning, is the first phase of the compilation process. It reads the source code character by character and groups them into meaningful sequences called lexemes. It then produces tokens, which are pairs of a token name and an attribute value. The tokens are passed on to the next phase, syntax analysis. Tokens are of the form <token-name, attribute-value> for each lexeme.
Syntax analysis, also known as parsing, is the second phase of the compilation process. It takes the tokens produced by the lexical analyzer and arranges them into a tree-like structure called a syntax tree or parse tree. The syntax tree creates a tree-like representation that shows the grammatical structure of the token stream. The syntax analyzer reports any syntax errors to the programmer if it finds any.