Lecture 03
Lecture 03
• Purpose:
Ensure the program follows the rules of the source language
grammar.
Role of Parsing in Compiler Design
Parsing is the process of analyzing a sequence of tokens to determine its
grammatical structure based on a given formal grammar.
Main Responsibilities:
•Check for Syntax Errors: Ensures the program adheres to the syntax rules of
the language.
•Construct Parse Tree: Represents the structure of the source code
hierarchically.
•Facilitate Semantic Analysis: Provides input for the next stage of the
compiler (semantic analysis) by structuring the code.
•Assist in Code Generation: Helps in translating the parse tree into
intermediate code or machine code.
•Example: Converting a mathematical expression into a parse tree and using
that for further analysis.
Position of parser in compiler model
❑ Parsing Approach
• Top down
• Bottom UP
Context-Free Grammar (CFG)
A Context-Free Grammar (CFG) is a formal system used to define
the syntax of programming languages. It consists of a set of
production rules that describe how strings in a language can be
generated.
•Formal way to describe the syntax of programming languages.
•Defines the syntactic structure of valid strings in a language.
•Composed of rules, called productions.
Notation:
G = (N, Σ, P, S)
CFG is defined by four tuples:
S → aSbb | abb
Now if we want to derive a string "aabbbb", we can start
with start symbols.
S → aSbb
S → aabbbb
Example 3:
Construct a CFG for a language L = {wcwR | where w € (a, b)*}.
Solution:
The string that can be generated for a given language is {aacaa, bcb, abcba, bacab,
abbcbba, ....}
The grammar could be:
1.S → aSa rule 1
2.S → bSb rule 2
3.S → c rule 3
Now if we want to derive a string "abbcbba", we can start with start symbols.
1.S → aSa
2.S → abSba from rule 2
3.S → abbSbba from rule 2
4.S → abbcbba from rule 3
Derivation
1.E E E
Input:
2.E E E
a*b+c
3.E = a | b | c
Draw a derivation tree for the string "bab" from the CFG given by
1.S → bSb | a | b
LMD
Ambiguity in Grammar
A grammar is said to be ambiguous if there exists
more than one leftmost derivation or more than one
rightmost derivation or more than one parse tree for
the given input string. If the grammar is not
ambiguous, then it is called unambiguous.
If the grammar has ambiguity, then it is not good for
compiler construction. No method can automatically
detect and remove the ambiguity, but we can remove
ambiguity by re-writing the whole grammar without
ambiguity.
Example 1:
Let us consider a grammar G with the production
rule
1.E I
2.E E E
3.E E E
4.E → E
5.I → ε | 0 | 1 | 2 | ... | 9
For the string "3 2 5", the above grammar can generate two parse trees by leftmost
derivation:
Example 2:
Check whether the given grammar G is ambiguous or not.
•E → E + E
•E → E - E
• E → id
From the above grammar derive String "id + id - id"
Example strings:
32
Two derivation trees
for
• Top-Down Parsing:
• Builds the parse tree from the root.
• Example: Recursive Descent Parser, LL Parser.
• Bottom-Up Parsing:
• Builds the parse tree from the leaves.
• Example: Shift-Reduce Parser, LR Parser.