Syntax Analysis: Presentation by
Syntax Analysis: Presentation by
Soumyadeep Das
Syntax
A n a ly s is
Roll No:
13000121136
Reg No:
211300100110125
Compiler Design (PCC-
CS501)
Page 02 of
15
Conten
t
1. Introduction 0
7
Lexical
Analysis vs.
2. S yntax Tree
Syntax
C onstruction 0
8 Analysis
3. Top Down Parser Common
Types of
4.
0 Bottom
Syntax up Parser Syntax Trees
5 Tree
09 Conclusion
0 Example
6 10 Reference
Page 03 of
15
Introduction
Syntax analysis, also known as parsing, is a crucial phase in the
process of compiling a programming language. It plays a
fundamental role in ensuring that a given sequence of source
code follows the correct grammar specified by the language's
syntax rules. In other words, syntax analysis checks the
syntactical correctness of the source code and builds the
foundation for subsequent phases of the compiler.
}
To p D o w n P a r s e r :
LL(1) Parsing:
Here the 1st L represents that the scanning of the Input will be done from the Left to Right
manner and the second L shows that in this parsing technique, we are going to use the Left
most Derivation Tree.
Essential conditions to check fi rst are as follows:
1.The grammar is free from left recursion.
2.The grammar should not be ambiguous.
3.The grammar has to be left factored in so that the grammar is deterministic grammar.
Recursive Descent Parser:
It is a kind of Top-Down Parser. A top-down parser builds the parse tree from the top to down,
starting with the start non-terminal. A Predictive Parser is a special case of Recursive
Descent Parser, where no Back Tracking is required.
Example:
Before removing left After removing left
recursion E – > E + T | T recursion E – > T E’
T–>T* F|F E’ – > + T E’
F – > ( E ) | id |e T–>F
T’
T’ – > * F T’
Bottom Up
Pa r s e r:
LR Parser
The LR parser is a non-recursive, shift-reduce, bottom-up parser. It uses a wide class of
context-free grammar which makes it the most effi cient syntax analysis technique. LR
parsers are also known as LR(k) parsers, where L stands for left-to-right scanning of the
input stream; R stands for the construction of right-most derivation in reverse, and k
denotes the number of lookahead symbols to make decisions.
There are three widely used algorithms available for constructing an LR parser:
3
The CFG consists of a set of production rules that define how valid statements and expressions can be formed using the
language's tokens.
a. Top-Down Parsing:
One common approach to syntax analysis is top-down parsing, where the compiler starts with the highest-level rule
of the CFG (the start symbol) and recursively expands it to build a parse tree or syntax tree.
The parsing process proceeds from the root to the leaves of the parse tree, attempting to match the input
sequence with the CFG rules.
Error Handling :
0 During syntax analysis, if the compiler encounters any syntax errors in the source code, it generates appropriate error
messages to help the programmer identify and correct the issues.
4 Error recovery mechanisms may be employed to allow the compiler to continue parsing the code and report multiple
errors in a single compilation pass.
Presentation by
Soumyadeep Das
EX A MP L
E
Parse tree and Syntax tree
E-> E+T | T
T-> T*F |F |
id F-> id
In the parse tree, most of the leaf nodes are single
child to their parent nodes.
In the syntax tree, we can eliminate this extra
information. Synt ax t ree is a
variant of parse
t ree. In
t he synt ax
t ree, interior nodes are operators and leaves are
operands.
Syntax tree is usually used when represent a program
in a tree structure.
A sentence id + id * id would have
the following syntax tree:
Parse Tree:
A parse t ree is a more general t erm t hat includes bot h ASTs and CSTs. It represent s t he
hierarChiCal st ruCt ure of t he sourCe code.
Conclusion
Synt ax analysis is a Crit iCal phase in t he
compilat ion proCess, responsible f or ensuring
t hat t he sourCe code f ollows t he rules specif ied
by t he programming language's Cont ext - f ree
grammar (CFG). This phase is CruCial because it
est ablishes t he synt aCt iC Correct ness of t he
program, paving t he way f or subsequent
Compiler st ages like semant iC analysis,
opt imizat ion, and Code generat ion.
TMSL | 20 23
Pag 14of
e 15
Re f e re n c e
https:// s
www.tutorialspoint.com/compiler_design/compiler_design_bottom_up_parser.htm
https://www.javatpoint.com/parse-tree-and-syntax-tree https://
www.geeksforgeeks.org/bottom-up-or-shift-reduce-parsers-set-2/
T ha nk
You!