What is Parsers?

Compiler DesignProgramming LanguagesComputer Programming

Syntax Analysis or Parser is the second phase of compilation. The parser takes as its input tokens generated from the previous phase, i.e., the Lexical Analyzer phase, and groups them in such a way that their syntax can be recognized.

It takes input as tokens & converts them into Parse Tree.

Example1 − Statement a = b + c will be converted to a sequence of tokens a, b, c, =, +, and these tokens will be converted to Parse Tree by Parser. The corresponding Parse Tree will be generated as −

Example2 − Draw Parse Tree for the statement. (If A = 10) GOTO 200.

The objective of parsing is to conclude the syntactic validity of a source string. If the string is correct, a tree is constructed for use by subsequent phases of the system. The tree follows the sequence of derivations or reduction used during the parse. Therefore, it is known as a parse tree.

If a string is null, the parser has to problem a diagnostic message recognizing the nature and generate of the errors in the string. Each elementary sub-tree in the parse tree corresponds to a production of grammar. There are two methods of identifying an elementary sub-tree −

  • By deriving a string from non-terminal

  • By reducing a string of symbols to a non-terminal.

The process of parsing consists of three phases which are as follows −

  • Lexical Analysis

Lexical Analysis is the first step of the compiler which reads the source code one character at a time and converts it into an array of tokens. A token is a meaningful collection of characters in a program. These tokens can be keywords including do, if, while etc. and identifiers including x, num, count, etc. and operator symbols including >,>=, +, etc., and punctuation symbols including parenthesis or commas. The output of the lexical analyzer phase passes to the next phase known as syntax analyzer or parser.

  • Syntax Analysis

The syntax analysis phase is also called the parsing phase. It takes tokens as input from the lexical analysis phase. The syntax analysis groups tokens together into syntactic structures. The output of this phase is a parse tree.

  • Semantic Analysis

The semantic analysis phase checks the source code for semantic errors and assemble type information for the successive code generation phase. It facilitates the hierarchical structure decided by the syntax analysis phase to recognize the operators and operands of expressions and statements. In this phase, the compiler checks that each operator has operands that are allowed by the source language specification.

Updated on 29-Oct-2021 06:54:33