MODULE 3 - Syntax Analysis
MODULE 3 - Syntax Analysis
MODULE 3 - Syntax Analysis
Syntax Analysis
By,
Mrs Snitha Shetty,
Assistant Professor,
Dept. of CSE,
AJIET, Mangaluru
Outline
• Introduction
• Context free grammars
• Writing a grammar
• Top down parser
• Bottom up parser
Role of parser
token
Source Lexical Parse tree Semantic Intermediate
Parser
program Analyzer Analyser representation
getNext
Token
Symbol
table
Role of Parser
• We categorize the parsers into two groups:
• Top-Down Parser- The parse tree is created top to bottom, starting from the root.
• Bottom-Up Parser- The parse is created bottom to top; starting from the leaves
• Both top-down and bottom-up parsers scan the input from left to
right (one symbol at a time).
Common programming errors
• Lexical errors: include misspelling of identifiers, keywords,
or operators.
E -> E + T | T
T -> T * F | F
F -> (E) | id
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id
Derivations
• Productions are treated as rewriting rules to generate a string
• Rightmost and leftmost derivations
• E -> E + E | E * E | -E | (E) | id
• Derivations for –(id+id)
• E => -E
=>-(E)
=>-(E+E)
=>-(id+E)
=>-(id+id)
Parse trees
• -(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
=>-(E+id)=>-(id+id)
Ambiguity
• For some strings there exist more than one parse tree
• Or more than one leftmost derivation
• Or more than one rightmost derivation
• Example: id+id*id
Consider the CFG S->SS+|SS*|a
string is aa+a*
1)Give a LMD
2)Give a RMD
3)Give a Parse tree
4) Whether Grammar is ambiguous or not?
LMD RMD
S->S(S)S|ε (()()) S=>S(S)S S=>S(S)S
⇒ε(S)S =>S(S) ε
⇒(S(S)S)S =>S(S(S)S)
⇒(ε(S)S)S =>S(S(S)S(S)S)
⇒((ε)S)S =>S(S(S)S(S) ε)
⇒(()S(S)S)S =>S(S(S)S(ε))
⇒(() ε(S)S)S
=>S(S(S) ε())
⇒(()() ε)S
⇒(()()) ε =>S(S(ε)())
⇒(()()) =>S(ε()())
⇒ε(()())
⇒(()())
Eliminating Ambiguity
Left Recursion and Left Factoring
Left Recursion
• A production of grammar is said to have left recursion if the leftmost
variable of its RHS is same as variable of its LHS.
• A grammar containing a production having left recursion is called as
Left Recursive Grammar.
Eg:- S → Sa / ∈
B → bB’
B’ → eB’ / ∈
S→A
A → Ad / Ae / aB / ac
B → bBc / f
E→E+T/T
T→T*F/F
F → id
Solution-
The left factored grammar is-
S → iEtSS’ / a
S’ → eS / ∈
E → b
S →ABCD
A → b/ ∈ FIRST(S)={b, c,∈}
FIRST(A)={b, ∈}
B→c FIRST(B)={c}
FIRST(C)={d}
C→d FIRST(D)={e}
D→e
S → aBDh
B → cC
C → bC / ∈
D → EF
E→g/∈
F→f/∈
S → aBDh
B → cC •First(S) = { a }
•First(B) = { c }
C → bC / ∈ •First(C) = { b , ∈ }
D → EF •First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈ }
•First(E) = { g , ∈ }
E→g/∈ •First(F) = { f , ∈ }
F→f/∈
S → ABCD
A → b/ ∈
B→c
C→d
D→e
FIRST(S)={b, c,∈}
S → ABCD FIRST(A)={b, ∈}
FOLLOW(S)={$}
A → b/ ∈ FIRST(B)={c}
FOLLOW(A)={c}
FIRST(C)={d}
FOLLOW(B)={d}
B→c FIRST(D)={e}
FOLLOW(C)={e}
C→d FOLLOW(D)={$}
D→e
C → bC / ∈
D → EF •First(S) = { a }
•First(B) = { c }
E→g/∈ •First(C) = { b , ∈ }
•First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈ }
F→f/∈
•First(E) = { g , ∈ }
•First(F) = { f , ∈ }
FOLLOW(S)={$,,,)}
First Functions- FOLLOW(L)={)}
•First(S) = { ( , a }
•First(L) = First(S) = { ( , a }
•First(L’) = { , , ∈ }
Follow Functions-
•Follow(S) = { $ } ∪ { First(L’) – ∈ } ∪ Follow(L) ∪ Follow(L’) = { $ , , , ) }
•Follow(L) = { ) }
•Follow(L’) = Follow(L) = { ) }
S->A|a
A->a FIRST FOLLOW
S –> A, S –>
S
a
A A –>a
1. S->aSbS/bSaS/ ∈
a
b $
This is not LL(1) grammar
S –> bSaS
S –> aSbS
S S->∈
S-> ∈
S->∈
FIRST FOLLOW
FIRST FOLLOW
E
T
T*F
T*id
F*id
id*id
Reverse of RMD
Mrs Snitha Shetty, Asst. Professor, Dept. of CSE, AJIET,
Mangaluru
Handle Pruning
$ id – id * id $ Shift
$ id – id * id $ Reduce E → id
$E – id * id $ Shift
$E– id * id $ Shift
$ E – id * id $ Reduce E → id
$E–E * id $ Shift
$E–E* id $ Shift
$ E – E * id $ Reduce E → id
$E–E*E $ Reduce E → E * E
$E–E $ Reduce E → E – E
$E $ Accept
Mrs Snitha Shetty, Asst. Professor, Dept. of CSE, AJIET,
Mangaluru
2. Consider the following grammar. Parse the input string ( a , ( a , a ) ) using a shift-reduce parser.
S→(L)|a
L→L,S|S Stack Input Buffer Parsing Action
$ (a,(a,a))$ Shift
$( a,(a,a))$ Shift
$(a ,(a,a))$ Reduce S → a
$(S ,(a,a))$ Reduce L → S
$(L ,(a,a))$ Shift
$(L, (a,a))$ Shift
$(L,( a,a))$ Shift
$(L,(a ,a))$ Reduce S → a
$(L,(S ,a))$ Reduce L → S
$(L,(L ,a))$ Shift
$(L,(L, a))$ Shift
$(L,(L,a ))$ Reduce S → a
$(L,(L,S ))$ Reduce L → L , S
$(L,(L ))$ Shift
$(L,(L) )$ Reduce S → (L)
$(L,S )$ Reduce L → L , S
$(L )$ Shift
$(L) $ Reduce S → (L)
$S $ Accept
Mrs Snitha Shetty, Asst. Professor, Dept. of CSE, AJIET,
Mangaluru
3.Considering the string “10201”, design a shift-reduce parser for the following grammar.
S → 0S0 | 1S1 | 2
Stack Input Buffer Parsing Action
$ 10201$ Shift
$1 0201$ Shift
$10 201$ Shift
$102 01$ Reduce S → 2
$10S 01$ Shift
$10S0 1$ Reduce S → 0 S 0
$1S 1$ Shift
$1S1 $ Reduce S → 1 S 1
$S $ Accept
$ int id , id ; $ Shift
$ int id , id ; $ Reduce T → int
$T id , id ; $ Shift
$ T id , id ; $ Reduce L → id
$TL , id ; $ Shift
$TL, id ; $ Shift
Reduce L → L ,
$ T L , id ;$
id
$TL ;$ Shift
Reduce S → T
$TL; $
L;
$S Mrs Snitha
$ Shetty, Asst. Professor, Dept. ofAccept
CSE, AJIET,
Mangaluru
Introduction to LR(0) Parser and SLR(1) Parser
Types of Parser
In order to construct LR(0) parser canonical collection of LR(0) items will be used.
0 aabb$ Shift S3
03 a abb$ Shift S3
033 aa bb$ Shift S4
033 aab b$ r3 A->b
03 aaA b$ r2 A->aA
0 aA b$ R2 A->aA
02 A b$ Shift S4
02 Ab $ R3 A->b
0 AA $ R1 S->AA
01 S $ Accept
Solution:
E’->E
• Generate the Augmented grammar 1 E->E+T
• Generate the LR(0) items 2 E->T
3 T->T*F
• Apply Closure and goto 4 T-> F
5 F->(E)
6 F->id