Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

What is Syntax Analysis?

Syntax Analysis is a second phase of the compiler design process in which the given
input string is checked for the confirmation of rules and structure of the formal grammar. It
analyses the syntactical structure and checks if the given input is in the correct syntax of
the
he programming language or not.

Syntax Analysis in Compiler Design process comes after the Lexical analysis phase. It is
also known as the Parse Tree or Syntax Tree. The Parse Tree is developed with the help
of pre
pre-defined
defined grammar of the language. The syntax
syntax analyser also checks whether a given
program fulfills the rules implied by a context-free
context free grammar. If it satisfies, the parser then
creates the parse tree of that source program. Otherwise, it will display error messages.

Syntax Analyser Process

Why do you need Syntax Analyser?


 Check if the code is valid grammatically
 The syntactical analyser helps you to apply rules to the code
 Helps you to make sure that each opening brace
br ace has a corresponding closing
balance
 Each declaration has a type and that the type must be exists
Why do we need Parsing?

A parse also checks that the input string is well-formed,


well formed, and if not, reject it.
Following are important tasks perform by the parser in compiler design:

 Helps you to detect all types of Syntax errors


 Find the position at which error has occurred
 Clear & accurate description of the error.
 Recovery from an error to continue and find further errors in the code.
 Should not affect compilation of "correct" programs.
 The parse must reject invalid texts by reporting syntax errors

Types of Parsing
Syntax analyzers follow production rules defined by means of context
context-free
free grammar. The way the
production rules are implemented
implemented (derivation) divides parsing into two types : top
top-down
down parsing and
bottom
bottom-up
up parsing.

Top
Top-down
down Parsing
When the parser starts constructing the the parse tree from the start symbol and then tries to transform
the start symbol to the input, it is called top-down
top down parsing.
 Recursive descent parsing : It is a common form of toptop-down
down parsing. It is called recursive
as it uses recursive procedures to process
process the input. Recursive descent parsing suffers from
backtracking.
 Backtracking : It means, if one derivation of a production fails, the syntax analyzer restarts
the process using different rules of same production. This technique may process the input
string
tring more than once to determine the right production.

Bottom
Bottom-up
up Parsing
As the name suggests, bottom-up
bottom up parsing starts with the input symbols and tries to construct the
parse tree up to the start symbol.
Example:

Input string : a + b * c
Production rules:

S → E
E → E + T
E → E * T
E → T
T → id
Let us start bottom-up parsing

a + b * c
Read the input and check if any production matches with the input:

a + b * c
T + b * c
E + b * c
E + T * c
E * c
E * T
E
S
Let us start top-down parsing

a + b * c
Read the input and check if any production matches with the input:

S
E
E * T
E + T * T
E + T * c
E + b * c
T + b * c
a + b * c

You might also like