0% found this document useful (0 votes)
18 views117 pages

Unit 3

This document covers Syntax Analysis, focusing on the role of parsers, context-free grammar, and the differences between syntax trees and parse trees. It explains derivation types, ambiguous grammar, and parsing techniques including top-down and bottom-up parsing methods. Additionally, it discusses left recursion and left factoring elimination, alongside exercises for practical understanding.

Uploaded by

modoge3607
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views117 pages

Unit 3

This document covers Syntax Analysis, focusing on the role of parsers, context-free grammar, and the differences between syntax trees and parse trees. It explains derivation types, ambiguous grammar, and parsing techniques including top-down and bottom-up parsing methods. Additionally, it discusses left recursion and left factoring elimination, alongside exercises for practical understanding.

Uploaded by

modoge3607
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 117

UNIT-3 Syntax Analysis

❑ Topic to be covered
✔ Role of Parser
✔ Context free grammar
✔ Difference between Syntax tree and Parse tree
✔ Derivation
✔ Ambiguous grammar
❑ Role of Parser

Token Parse
Source Lexical Parse tree end
IR
Parser
tree Rest of front
program analyzer
Get next
token

Symbol table

✔ Parser obtains a string of token from the lexical analyzer and reports syntax error if any
otherwise generates syntax tree.
✔ There are two types of parser:
1. Top-down parser
2. Bottom-up parser
❑ Context free grammar

}
❑ Difference between Syntax tree and Parse tree
Syntax Tree Parse Tree
✔ In Syntax tree, interior nodes are operator, and leaves are ✔ In Parse tree, interior nodes are Nonterminals, and leaves
Operand. are Terminals.
✔ It represents the abstract syntax tree of a program. ✔ It represents the concrete syntax of a program.
✔ Syntax tree contains only meaningful information. ✔ Parse tree contains unusable information also.
✔ Example: ✔ Example:
Grammar: E 🡪 E * E | E + E | id Grammar: E 🡪 E * E | E + E | id
String: id + id * id or a + b * c String: id + id * id or a + b * c

+ E

E * E
id *
E + E id

id id
id id
❑ Derivation
▪ Derivation means to replace a Nonterminal by the body of its production.
▪ Derivation is used to find whether the string belongs to a given grammar or not.
▪ Types of derivations are:
1. Leftmost derivation
2. Rightmost derivation
❖ Leftmost derivation

S - S
Parse tree represents the
structure of derivation S * S a

a a

Leftmost Derivation Parse tree


❖ Rightmost derivation

S * S

a S - S

a a
Rightmost Derivation Parse Tree
❑ Exercise
1. S🡪A1B
𝜖 |𝜖
A0A
B🡪0B | 1B | 𝜖
String: 1001. Perform leftmost and rightmost derivation.

2. E🡪E+E | E*E | id | (E) | -E


String : id + id * id. Perform leftmost and rightmost derivation
❑ Ambiguous grammar
▪ Ambiguous grammar is one that produces more than one leftmost or more then one rightmost
derivation for the same sentence.
▪ Grammar: S🡪S+S | S*S | (S) | a Output string: a+a*a

S S
S S
🡪S*S S 🡪S+S S + S
* S
🡪S+S*S 🡪a+S
🡪a+S*S S + S a 🡪a+S*S a S * S
🡪a+a*S 🡪a+a*S
🡪a+a*a
a a 🡪a+a*a
a a

✔ Here, Two leftmost derivation for string a+a*a is possible hence, above grammar is
ambiguous.
❑ Exercise
Check whether following grammars are ambiguous or not:
1. S🡪 aS | Sa | 𝜖 (string: aaaa)
2. S🡪 aSbS | bSaS | 𝜖 (string: abab)
3. S🡪SS+ | SS* | a (string: aa+a*)
4. Show that the CFG with productions: S 🡪 a | Sa | bSS | SSb | SbS is ambiguous. (Assume
string)
THANK YOU
❑ Topic to be covered

✔ Classification of parsing
✔ Left Recursion
✔ Left Factoring
❑ Parsing
▪ Parsing is a technique that takes input string and produces output either a
parse tree if string is valid sentence of grammar, or an error message
indicating that string is not a valid.

▪ Types of parsing are:


1. Top down parsing: In top down parsing parser build parse tree from top to
bottom.
2. Bottom up parsing: Bottom up parser starts from leaves and work up to the
root.
❑ Classification of parsing methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ Left recursion
❖ Left recursion elimination
❑ Why to remove Left Recursion ?

✔ It is possible for top down parser to go in infinite loop.


S🡪 ( L ) | a
L🡪 L , S | S ❖ Suppose the grammar is S🡪Sa.
❖ The string: aaa

S a

S a

S a

S a
infinite loop
❖ Examples: Left recursion elimination
Rule to remove Left Recursion

E🡪E+T | T
T🡪T*F | F
F🡪id | ε

After removing left recursion E🡪E+T | T


The grammar is E🡪TE’
E’🡪+TE’ | ε
E🡪TE’
T🡪T*F | F
E’🡪+TE’ | ε
T🡪FT’
T🡪FT’
T’🡪*FT’ | ε
T’🡪*FT’ | ε
F🡪id | ε
❖ Examples: Left recursion elimination
S🡪 Aa | b
A🡪 Ac | Sd | ε
Here, Non terminal S is left recursive because:
S🡪 Aa 🡪 Sda
S🡪 Aa | b
To remove indirect left
recursion replace S A🡪 Ac
Aad
Sd | bd A🡪Ac | Aad | bd | ε
with productions of S
A🡪
A🡪 ε
Now, remove left recursion
S🡪 Aa | b
A🡪Ac | Aad | bd | ε S🡪 Aa | b
A🡪 bdA’ | A’
A’🡪 cA’ | adA’ | ε
❖ Exercise: Remove Left Recursion from following Grammar

1. A🡪Abd | Aa | a
B🡪Be | b

2. A🡪AB | AC | a | b

3. S🡪A | B
A🡪ABC | Acd | a | aa
B🡪Bee | b
❑ Left Factoring
❑ Left Factoring Elimination

| δ
❑ Example: Left Factoring Elimination
❖ S🡪aAB | aCD
S 🡪 aS’
S’ 🡪 AB | CD

❖ A🡪 xByA | xByAzA | a

A 🡪 xByAA’ | a
A’𝜖𝜖 | zA
❖ A🡪 aAB | aA |a
A 🡪 aA’
A’🡪 AB | A | 𝜖
A’🡪 AA’’ | 𝜖
A’’🡪 B | 𝜖
❖ Exercise: Remove Left Factoring from following Grammar

1. S🡪iEtS | iEtSeS | a
2. A🡪 ad | a | ab | abc | x
THANK YOU
❑ Topic to be covered

✔ Top-Down parsing
⮚ Backtracking
⮚ First and Follow
⮚ Parsing without backtracking (predictive parsing /(LL(1)))
❑ Classification of parsing methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ Backtracking
✔ In backtracking, expansion of nonterminal symbol we choose one alternative
and if any mismatch occurs then we try another alternative.
✔ Grammar: S🡪 cAd
A🡪 ab | a
Input string(w): cad
cad We have produced a parse tree for the
string:cad. We halt/ stop and announce
successful completion of parsing

S S S

c A d c A d c A d
Make prediction Make prediction

a b Backtrack a
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ LL(1) parser (Predictive parser / Table driven parser)
✔ LL(1) is non recursive top down parser.
1. First L indicates input is scanned from left to right.
2. The second L means it uses leftmost derivation for input string
3. 1 means it uses look-ahead of only one input symbol to predict the parsing process.

a + b $ Input

X
Y Predictive
Stack parsing OUTPUT
Z
program
$

Parsing table M

Fig: Model of Non-recursive Predictive Parser


❑ LL(1) parser (Predictive parser / Table driven parser)

❖ Steps to construct LL(1) parser

1. Remove left recursion (if any).


2. Perform left factoring (if any).
3. Compute FIRST and FOLLOW of non terminals.
4. Construct predictive parsing table.
5. Parse the input string using parsing table.
❑ Rules to compute First of non terminal
❑ Compute First of non terminal
E🡪E+T | T
T🡪T*F | F
F🡪(E) | id

Step 3: Find First and Follow of Non terminal


Step 1: Remove left recursion
E🡪 TE’ NT First

E’🡪 +TE’ | ϵ E { (, id }

𝜖
T🡪 FT’ E’ { +,
{ (,}
id 𝜖} }
T’🡪*FT’ | ϵ T
F🡪 (E) | id T’ { *,
F { (, id }
Step 2: Perform left factoring
(No Left factoring in above grammar)
❑ Rules to compute Follow of non terminal
❑ Compute Follow of non terminal
E🡪E+T | T
T🡪T*F | F
F🡪(E) | id

Step 3: Find First and Follow of Non terminal


Step 1: Remove left recursion
E🡪 TE’ NT First Follow
E’🡪 +TE’ | ϵ E { (,id } { $, ) }
E’ { +, 𝜖 } { $, ) }
T🡪 FT’
T { (,id } { +, $, ) }
T’🡪*FT’ | ϵ
T’ { *, 𝜖 } { +, $, ) }
F🡪 (E) | id F { (,id } {*, +, $, ) }

Step 2: Perform left factoring


(No Left factoring in above grammar)
❑ Construct predictive parsing table
NT First Follow
E🡪 TE’
E { (,id } { $,) }
{ +, 𝜖 } { $,) }
E’🡪 +TE’ | ϵ
T🡪 FT’ E’
T’🡪*FT’ | ϵ T { (,id } { +,
{ *, 𝜖 }
F🡪 (E) | id $,) }
T’ { +,
$,) }
F { (,id } {*,+,
$,)} Here, the Grammar is LL(1) because the
Step 4: Construct predictive parsing table predictive parsing table has no multiply-
defined entries
Input Symbols
NT
id + * ( ) $

E E🡪TE’ E🡪TE’
E’ E’🡪+TE’ E’𝜖 E’𝜖
T T🡪FT’ T🡪FT’
T’ T’🡪𝜖 T’🡪*FT’ T’🡪𝜖 T’🡪𝜖
F F🡪id F🡪(E)
❑ Construct predictive parsing table
Step 5: Parse the string : id + id * id $

STACK INPUT OUTPUT STACK INPUT OUTPUT


$E id+id*id$ $E’T’F id$
$E’T id+id*id$ E🡪TE’ $E’T’id id$ F🡪id
$E’T’F id+id*id$ T🡪FT’ $E’T’ $
$E’T’id id+id*id$ F🡪id $E’ $ T’🡪𝜖
$E’T’ +id*id$ $ $ E’🡪𝜖 String Accepted
$E’ +id*id$ T’🡪𝜖
Input Symbol
$E’T+ +id*id$ E’🡪+TE’ NT
id + * ( ) $
$E’T id*id$
$E’T’F id*id$ T🡪FT’ E E🡪TE’ Error Error E🡪TE’ Error Error
$E’T’id id*id$ F🡪id E’ Error E’🡪+TE’ Error Error E’🡪𝜖 E’🡪𝜖

$E’T’ *id$ T T🡪FT’ Error Error T🡪FT’ Error Error

$E’T’F* *id$ T’🡪*FT’ T’ Error T’🡪𝜖 T’🡪*FT’ Error T’🡪𝜖 T’🡪𝜖


F F🡪id Error Error F🡪(E) Error Error
❑ Exercise: Check whether the following grammar is LL(1)
or not

S 🡪 AaAb | BbBa S🡪 aAB | bA | 𝜖 S🡪iCtSA | a


A𝜖 A🡪 aAb | 𝜖 A🡪 eS | 𝜖
B🡪𝜖 B🡪 bB | 𝜖 C🡪 b

S🡪 (L) | a E🡪 TA S🡪 a | ^ | (R)
L🡪 L,S | S A🡪 +TA | 𝜖 T🡪 S, T | S
T🡪 VB R🡪 T
B🡪 *VB |𝜖
V🡪 id | (E)
Implement the following grammar using Table Driven parser and check whether it is LL(1) or not.
S 🡪aBDh
B 🡪cC Step 3: Find First and Follow of Non terminal
C 🡪 bC | ^ NT First Follow
D 🡪EF S { a } { $ }
E🡪g|^ B { c } { g, f , h }
F🡪f|^
C { b, ^ } {g, f , h }
D { g, f, ^ } {h}
Step:1 Remove left recursion
🡪 No left recursion in grammar E { g, ^ } {f, h }
Step:2 Perform left factoring F { f, ^ } {h}
🡪 No left factored grammar
Implement the following grammar using Table Driven parser and check whether it is LL(1) or not.
S 🡪aBDh NT First Follow
B 🡪cC S { a } { $ }
C 🡪 bC | ^ B { c } { g, f , h }
Here, the Grammar is LL(1) because the predictive
D 🡪EF parsing table has no multiply-defined entries C { b, ^ } {g, f , h }
E🡪g|^ D { g, f, ^ } {h}
F🡪f|^ E { g, ^ } {f, h }
F { f, ^ } {h}
Step 4: Construct predictive parsing table
Input Symbols
NT
a b c g f h $

S S 🡪aBDh
B B 🡪cC
C C 🡪 bC C🡪 ^ C🡪 ^ C🡪 ^
D D 🡪EF D 🡪EF D🡪 ^
E E🡪g E🡪^ E🡪^
Check following grammar is LL (1) or not?

S🡪iCtSA | a Step 3: Find First and Follow of Non terminal


A🡪 eS | 𝜖 NT First Follow
C🡪 b S { i, a } { e, $ }
A { e, 𝜖 } {e, $ }
Step:1 Remove left recursion
C { b } {t}
🡪 No left recursion in grammar
Step:2 Perform left factoring
🡪 No left factored grammar
Here, the Grammar is NOT LL(1) because the
predictive parsing table has multiply-defined |
Step 4: Construct predictive parsing table multiple entries in same cell
Input Symbols
NT
a b e i t $

S S🡪 a S🡪iCtSA
A A🡪 eS A🡪 𝜖
A🡪 𝜖
C C🡪 b
THANK YOU
❑ Topic to be covered

✔ Top-Down parsing
⮚ Recursive descent parsing
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ Recursive descent parsing

✔ A top down parsing that executes a set of recursive procedure to


process the input without backtracking is called recursive descent
parser.
✔ There is a procedure for each non terminal in the grammar.
✔ Consider RHS of any production rule as definition of the procedure.
✔ As it reads expected input symbol, it advances input pointer to next
position.
❑ Example: Recursive descent parsing
Procedure T
Procedure E Proceduce Match(token t)
{
{ {
if lookahead=’*’
if lookahead=num if lookahead=t
{
{
Match(‘*’);
if lookahead=next_token;
Match(num); else
lookahead=num
T(); Error();
{
} }
else
Match(num);
Error(); Procedure Error
T();
if lookahead=$ {
}
{ print(“Error”);
else
Declare }
success;
Error();
}
else Grammar :
}
Error();
else
E🡪 num T
}
NULL T🡪 * num T| 𝜖
}
Input String 3 * 4 $ Success
❑ Example: RecursiveProcedure
descent T
parsing
Procedure E { Proceduce Match(token t)
{ if lookahead=’*’ {
if lookahead=num { if lookahead=t
{ Match(‘*’);
if lookahead=next_token;
Match(num); lookahead=num else
T(); { Error();
} }
else Match(num);
Error(); Procedure Error
if lookahead=$ T(); {
{ } print(“Error”);
Declare else }
success;
} Error();
else
Error(); } E🡪num T
} else T🡪* num T| 𝜖
NULL
3 * 4 $ }
Success 3 4 * $ Error
❑ i+i$
1. X🡪 charY
Y🡪 +charY | 𝜖
❑ Exercise
1. X🡪 charY
Y🡪 +charY | 𝜖
THANK YOU
❑ Topic to be covered

✔ Bottom-up parsing
⮚ Handle and Handle pruning
⮚ Shift Reduce parser
⮚ Operator precedence parsing
▪ Operator precedence function
Parsing methods

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ Handle & Handle pruning
✔ Handle: A “handle” is a substring of the string that matches the right side of a production, and we
can reduce such string by a non terminal on left hand side production.
✔ Handle pruning: The process of discovering a handle and reducing it to appropriate left hand side
non terminal is known as handle pruning.

E🡪E+E Grammar:
E🡪E*E String: id1+id2*id3E🡪E+T | T
E🡪id T🡪T*F | F
F🡪id Right sentential Handle Production
Rightmost Derivation In Reverse form
id1+id2*id3 id1 E🡪id
E String: id+id*idE+id2*id3 id2 E🡪id
E+E E+E*id3 id3 E🡪id
E+E*E E+E*E E*E E🡪E*E
E+E*id3 E+E E+E E🡪E+E
E+id2*id3
E
id1+id2*id3
❑ Shift reduce parser
✔ The shift reduce parser performs following basic operations:

1. Shift: Moving of the symbols from input buffer onto the stack, this action is called
shift.
2. Reduce: If handle appears on the top of the stack then reduction of it by appropriate
rule is done. This action is called reduce action.
3. Accept: If stack contains start symbol only and input buffer is empty at the same time
then that action is called accept.
4. Error: A situation in which parser cannot either shift or reduce the symbols, it cannot
even perform accept action then it is called error action.
❑ Example: Shift reduce parser
Stack Input Buffer Action
Grammar: $ id+id*id$ Shift
E🡪E+T | T $id +id*id$ Reduce F🡪id
T🡪T*F | F $F +id*id$ Reduce T🡪F
F🡪id $T +id*id$ Reduce E🡪T
$E +id*id$ Shift
String: id+id*id $E+ id*id$ Shift
$E+id *id$ Reduce F🡪id
$E+F *id$ Reduce T🡪F
$E+T *id$ Shift
$E+T* id$ Shift
$E+T*id $ Reduce F🡪id
$E+T*F $ Reduce T🡪T*F
$E+T $ Reduce E🡪E+T
$E $ Accept
❑ Exercise:

Consider the grammar


E🡪E+T|T
T🡪T*F|F
F 🡪 (E) | id
Perform shift reduce parsing of input string: a * ( b + c )
Parsing methods

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
Parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ Operator precedence parsing

✔ Operator Grammar: A Grammar in which there is no Є in RHS of any


production or no adjacent non terminals is called operator grammar.
Example: E🡪 EAE | (E) | id
A🡪 + | * | -
✔ Above grammar is not operator grammar because right side EAE has
consecutive non terminals.
❑ Precedence & associativity of operators
❑ Steps of operator precedence parsing
1. Find Leading and trailing of non terminal
2. Establish relation
3. Creation of table
4. Parse the string
❑ Example: Operator precedence parsing

E🡪 E+T| T
T🡪 T* F| F
F🡪 id Assign precedence operator between terminals
String: id+id*id
String: id+id*id

$ id+id*id $
$ <. id+id*id$
+ * id $
$ <. id .> +id*id$
+ .
> <. <. .
>
$ <. id .> + <. id*id$
* .
> .
> <. .
>
$ <. id .> + <. id .> *id$
id .
> .
> .
>
$ <. id .> + <. id .> *<. id$
$ <. <. <.

$ <. id .> + <. id .> *<. id .> $


Table: Operator Precedence relation
❑ Example: Operator precedence parsing
Parse the string using precedence table E🡪 E+T| T
1. Scan the input string until first .> is encountered. T🡪 T* F| F
2. Scan backward until <. is encountered. F🡪 id
3. The handle is string between <. and .>

$ <. id .> + <. id .> * <. id .> $ Handle id is obtained between <. and .>
Reduce this by F🡪id
$ F + <. id .> * <. id .> $ Handle id is obtained between <. and .>
Reduce this by F🡪id
$ F + F * <. id .> $ Handle id is obtained between <. and .>
Reduce this by F🡪id
$F+F*F$ Perform appropriate reductions of all nonterminals.
$E+T*F$ Remove all non terminals.
$ + * $ Place relation between operators
$ <. + <. * >$ The * operator is surrounded by <. and .>. This indicates *
becomes handle so reduce by T🡪T*F.
$ <. + >$ + becomes handle. Hence reduce by E🡪E+T.

$ $ Parsing Done
❑ Operator precedence function

.
❑ Operator precedence function

E🡪 E+T | T
T🡪 T*F | F
F🡪 id

f+ f* fid f$

g+ g* gid g$
❑ Operator precedence function
2. Partition the symbols in as many as groups possible, in such a way that f a and gb are in the
.
same group if a = b.

gid fid

f* g*

g+ f+

f$ g$
❑ Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb

g
+ * id $
gid fid + .
> <. <. .
>
f * .
> .
> <. .
>
id .
> .
> .
>
f* g*
$ <. <. <.

g+ f+ f+ .> g+ f+ 🡪 g+
f* .> g+ f* 🡪 g+
fid .> g+ fid 🡪 g+
f$ g$ f$ <. g+ f$ 🡨 g+
❑ Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb

g
+ * id $
gid fid
+ .
> <. <. .
>
f * .
> .
> <. .
>
f* g*
id .
> .
> .
>
$ <. <. <.

g+ f+ f+ <. g* f+ 🡨 g*
f* .> g* f* 🡪 g*
fid .> g* fid 🡪 g*
f$ g$ f$ <. g* f$ 🡨 g*
❑ Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb

g
gid fid + * id $
+ .
> <. <. .
>
f * .
> .
> <. .
>
f* g* id .
> .
> .
>
$ <. <. <.

g+ f+
f+ <. gid f+ 🡨 gid
f* <. gid f* 🡨 gid
f$ g$ f$ <. gid f$ 🡨 gid
❑ Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb

g
gid fid + * id $
+ .
> <. <. .
>
f * .
> .
> <. .
>
f* g* id .
> .
> .
>
$ <. <. <.

g+ f+
f+ .> g$ f+ 🡪 g$
f* > g$ f* 🡪 g$
f$ g$ fid > g$ fid 🡪 g$
❑ Operator precedence function

+ * id $
f 2
gid fid
g

f* g* 4. If the constructed graph


has a cycle then no
precedence functions
g+ f+ exist. When there are no
cycles collect the length of
the longest paths from the
f$ g$
groups of fa and gb
respectively.
❑ Operator precedence function

gid fid + * id $
f 2
g 1
f* g*

g+ f+

f$ g$
❑ Operator precedence function

gid fid
+ * id $
f 2 4
f* g* g 1

g+ f+

f$ g$
❑ Operator precedence function

gid fid
+ * id $
f 2 4

f* g* g 1 3

g+ f+

f$ g$
❑ Operator precedence function

+ * id $
gid fid
f 2 4 4
g 1 3
f* g*

g+ f+

f$ g$
❑ Operator precedence function

gid fid
+ * id $
f 2 4 4
f* g* g 1 3 5

g+ f+

f$ g$
❑ Operator precedence function

gid fid
+ * id $
f 2 4 4 0
f* g*
g 1 3 5 0

g+ f+

f$ g$
❑ Operator precedence function
+ * id $
+ * id $
+ .
> <. <. .
> 2 4 4 0
f
* .
> .
> <. .
> 1 3 5 0
g
id .
> .
> .
>
$ <. <. <. Table: Operator precedence function table

Table: Operator precedence table


✔ Both precedence table and function table gives same relation about operator
For example:
According to operator precedence table: + < *
According to operator function table: + < *
f(+)🡪 2 3 🡨g(*)

✔ The advantage of constructing function table is size or space. The size of precedence table is O(n2), if there is
4 operator 16 cell are used for computing in operator precedence table. But the size of function table is 2n,
✔ It is more efficient in terms of space compare to operator precedence.
✔ Disadvantage of function table is, unlike operator precedence matrix, it does not detect error like:
f(id) g(id). In precedence function table is shows that it is right associative while in operator precedence
matrix it detects an error.
Stack Input Buffer Action
❑ Example: Shift reduce parser $ id * ( id + id ) $ Shift
$ id * ( id + id ) $ Reduce F🡪 id
Given the grammar, evaluate the string
$F * ( id + id ) $ Reduce T 🡪 F
a * ( b + c ) using Shift Reduce parser $T * ( id + id ) $ Shift
$T* ( id + id ) $ Shift
$T*( id + id ) $ Shift
E🡪E+T|T
$ T * ( id + id ) $ Reduce F🡪 id
T🡪T*F|F $T*(F + id ) $ Reduce T 🡪 F
$T*(T + id ) $ Reduce E 🡪 T
F 🡪 (E) | id $T*(E + id ) $ Shift
$T*(E+ id ) $ Shift
$ T * ( E + id )$ Reduce F🡪 id
String: a * ( b + c ) = id * ( id + id )
$T*(E+F )$ Reduce T 🡪 F
$T*(E+T )$ Reduce E 🡪 E + T
$T*(E )$ Shift
$ T * ( E) $ Reduce F🡪 (E)
Construct a precedence graph, precedence table for operator precedence parser to
be used for parsing a string consisting of id, - , * , $. Parse following string.
$ id - id * id * id $
THANK YOU
❑ Topic to be covered

✔ LR Parsing
⮚ SLR
Parsing methods

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
Parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ LR Parser
✔ LR parsing is most efficient method of bottom up parsing which can be used to parse large class of
context free grammar.
✔ The technique is called LR(k) parsing:
1. The “L” is for left to right scanning of input symbol,
2. The “R” for constructing right most derivation in reverse,
3. The “k” for the number of input symbols of look ahead that are used in making parsing
decision.
a + b $ INPUT

X
Y LR parsing
Stack program OUTPUT
Z
$

Parsing Table
Action Goto
Parsing methods

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ Steps to construct SLR(Simple LR) parser
1. Augmented Grammar
2. Construct Canonical set of LR(0) items
3. Compute Follow function
4. Construct SLR parsing table
5. Parse the input string
❑ Steps to construct SLR(Simple LR) parser
1. Augmented Grammar:
Augmented grammar is a grammar G with new introduced non-terminal S’ and production S’🡪 S. The
production S’🡪 S is introduce to indicate the parsing to announce acceptance of input.
2. Canonical set of LR(0) items:
A LR(0) item of a Grammar G is a production of G with a dot { . } at some position of the right side.
Thus production A🡪XYZ yields the four items.
A🡪 .XYZ It means, in right hand side we not seen anything(terminal or non-terminal)
A🡪 X.YZ It means, on right hand side we seen X
A🡪 XY.Z It means, on right hand side we seen X and Y
A🡪 XYZ. It means, on right hand side we seen everything, so now XYZ in the input can be reduced by
A because everything is seen on right hand side by the parser

✔ The production A🡪 𝜖 generates A🡪 .


❑ Steps to construct SLR(Simple LR) parser
✔ Closure Operation:
If I is a set of items of a grammar G then closure(I) is a set of items constructed from I by
the two rules:
i. Initially, every item in I is added to closure(I).
ii. If A🡪𝛼.B𝛽 is in closure(I) and B🡪y is a production, then add the item B🡪.y to I,
if it is not all ready there. Apply this rule until no more new items can be added to
closure(I).

✔ goto operation:
goto(I,X) is defined to be closure of the set of all items [A🡪 𝛼X. 𝛽] such that [A🡪 𝛼.X 𝛽]
is in I
❑ Example: Construct SLR parsing table for the given grammar
E🡪E + T | T
T🡪 T * F | F
F🡪 (E)
F🡪 id

Step 1: Augmented Grammar

E’🡪 E r0
E🡪E + T r1
E🡪 T r2
T🡪 T * F r3
T🡪 F r4
F🡪 (E) r5
F🡪 id r6
Step 2: Canonical collection of LR(0) items goto (I0 , id) = F🡪 id. I5 goto (I6 , T) = E🡪 E + T. I9
I0 = E’🡪 .E T🡪 T. * F
goto (I1 , +) = E🡪 E +. T I6
E🡪 .E + T goto (I6 , F) = T🡪 F. I3
E🡪 .T T🡪 .T * F
T🡪 .T * F T🡪 .F goto (I6 , ( ) = F🡪 (.E) I4
T🡪 .F F🡪 .(E)
F🡪 .(E) F🡪 .id goto (I6 , id) = F🡪 id. I5
F🡪 .id goto (I2 , *) = T🡪 T *.F I7
goto (I0 , E) = E’🡪 E. I1 goto (I7 , F) = T🡪 T * F. I10
F🡪 .(E)
E🡪 E. + T F🡪 .id goto (I7 , ( ) = F🡪 (.E) I4
goto (I0 , T) = E🡪 T. I2 I8
goto (I4 , E ) = F🡪 (E.)
T🡪 T. * F E🡪 E. + T goto (I7 , id) = F🡪 id. I5
goto (I0 , F) = T🡪 F. I3 goto (I4 , T) = E🡪 T. I2
goto (I8 , ) ) = F🡪 (E). I11
goto (I0 , ( ) = F🡪 (.E) T🡪 T. * F
I4
E🡪 .E + T goto (I8 , + ) = E🡪 E +. T I6
goto (I4 , F) = T🡪 F. I3
E🡪 .T
T🡪 .T * F goto (I4 , ( ) = F🡪 (.E) I4
goto (I9 , *) = T🡪 T *.F I7
T🡪 .F
F🡪 .(E)
goto (I4 , id) = F🡪 id. I5
Step 3: Compute follow function
Note: In bottom-up parsing, no need to remove left recursion and left factoring in any of the grammar.
Because bottom-up parsing handles the left recursive and left factored grammar so Directly find the follow
function

Grammar:

E🡪E + T | T NT FOLLOW

T🡪 T * F | F E { $, +, ) }
F🡪 (E) T { $, +, ), * }
F🡪 id F { $, +, ), * }
Step 4: SLR parsing table (r)educe: production rule number
(S)hift: State Number

Action goto
State id + * ( ) $ E T F
I0 S5 S4 1 2 3
I1 S6 Accept

I2 r2 S7 r2 r2
I3 r4 r4 r4 r4
I4 S5 S4 8 2 3
I5 r6 r6 r6 r6
I6 S5 S4 9 3
I7 S5 S4 10
I8 S6 S11
I9 r1 S7 r1 r1
I10 r3 r3 r3 r3
Step 5: Parse the input string: id * id + id
Stack Input Action
0 id * id + id $ Shift
0 id 5 * id + id $ Reduce F🡪id
0F3 * id + id $ Reduce T🡪F
0T2 * id + id $ Shift
0T2*7 id + id $ Shift
0 T 2 * 7 id 5 + id $ Reduce F🡪id
0 T 2 * 7 F 10 + id $ Reduce T🡪T * F
0T2 + id $ Reduce E🡪T
0E1 + id $ Shift
0E1+6 id $ Shift
0 E 1 + 6 id 5 $ Reduce F🡪id
0E1+6F3 $ Reduce T🡪F
0E1+6T9 $ Reduce E🡪E + T
0E1 $ Accept
❑ Exercise
❖ Develop SLR parsing table for the following grammar:

1. S🡪Aa | bAc | bBa


A🡪d
B🡪d

2. S🡪AaAb
S🡪BbBa
A🡪 𝜖
B🡪 𝜖

3. S🡪 0S0 | 1S1 |10


THANK YOU
S 🡪bAc | Bc | bBa
A🡪 d
B🡪d
❑ Topic to be covered

✔ LR Parsing
⮚ CLR
Parsing methods

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ CLR(Canonical LR) parser
✔ Canonical LR parsing is the more general technique for constructing an LR
parser. In this method more information is carried in the state to avoid
invalid reduction.
✔ In CLR, the lookaheads are symbols used by parser to ‘look-ahead’ in the
input buffer to decide whether or not reduction is to be done. That is, we
have to work with the item of the form A𝛼𝛽 .X𝛽 , a

✔ The item is called as LR(1) item because the length of lookahead is one.
Therefore, an item without a look-ahead is LR(0) items.
❑ CLR(Canonical LR) parser

✔ LR(1) items contains two parts:


(i) LR(0) item and (ii) the look-ahead associated with an item.
SLR CLR
Augmented grammar:
For example: S’🡪 S ----------- r0
S 🡪 CC ------------ r1
Grammar C 🡪 cC ------------ r2
C🡪d ---------- r3
Canonical collection of LR (0) items Canonical collection of LR (1) items
S🡪CC
C🡪 cC | d S’🡪 .S S’🡪 .S ,$
S 🡪 .CC S 🡪 .CC ,$
C 🡪 .cC C 🡪 .cC ,c|d
C 🡪 .d C 🡪 .d ,c|d
❑ Steps to construct CLR(Canonical LR) parser
1. Augmented Grammar
2. Construct Canonical set of LR(1) items
3. Construct CLR parsing table
4. Parse the input string
❑ Example: Construct CLR parsing table for the given grammar

S🡪CC
C🡪 cC | d

Step 1: Augmented Grammar

S’🡪 S r0
S 🡪 CC r1
C 🡪 cC r2
C🡪d r3
Step 2: Canonical collection of LR(1) items

I0 = S’🡪 .S , $ goto (I0 , d) = C🡪 d. , c | d I4 goto (I3 , d) = C🡪 d. , c | d I4

S🡪 .CC , $ goto (I6 , C) = C🡪 cC. , $ I9


C🡪 .cC , c|d goto (I2 , C) = S🡪 CC. , $ I5
C🡪 .d , c | d goto (I6 , c ) = C🡪 c.C , $ I6
C🡪 .cC , $
goto (I2 , c ) =C🡪 c.C , $ I6 C🡪 .d , $
goto (I0 , S) = S’🡪 S. , $ I1
C🡪 .cC , $
C🡪 .d , $
goto (I0 , C) = S🡪 C.C , $ I2 goto (I6 , d) = C🡪 d. , $ I7
C🡪 .cC , $
C🡪 .d , $ goto (I2 , d) = C🡪 d. , $ I7

goto (I0 , c ) = C🡪 c.C , c | d I3 goto (I3 , C) = C🡪 cC. , c | d I8


C🡪 .cC , c | d
C🡪 .d , c | d goto (I3 , c ) = C🡪 c.C , c | d I3
C🡪 .cC , c | d
C🡪 .d , c | d
(r)educe: production rule number
Step 3: CLR parsing table
(S)hift: State Number

Action goto
State c d $ S C
I0 S3 S4 1 2
I1 Accept

I2 S6 S7 5
I3 S3 S4 8
I4 r3 r3
I5 r1
I6 S6 S7 9
I7 r3
I8 r2 r2
I r2
Step 4: Parse the input string: cdd

Stack Input Action


0 cdd $ Shift
0c3 dd $ Shift
0c3d4 d$ Reduce C🡪d
0c3C8 d$ Reduce C🡪cC
0C2 d$ Shift
0C2d7 $ Reduce C🡪d
0C2C5 $ Reduce S🡪CC
0S1 $ Accept
❑ Exercise
❖ Develop CLR parsing table for the following grammar:

1. S🡪Aa | bAc | dc | bda


A🡪d

2. S🡪 CC
C🡪 aC | d
THANK YOU
❑ Topic to be covered

✔ LR Parsing
⮚ LALR
Parsing methods

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
❑ LALR(Look-ahead LR) parser

✔ LALR is often used in practice because the table obtain by it are considerably
smaller then canonical LR.
✔ To construct LALR parser:
(i) Obtain the canonical collection of sets of LR(1) items.
(ii) If more then one set of LR(1) item exists in canonical collection obtained that
have identical (common) cores of LR(0)s, but which have different look-
aheads, then combine these sets of LR(1)items to obtain a reduced collection
of sets of LR(1) items.
(iii) Construct parsing table by using reduced collection as follows:
(a) For each Ii in C do for every terminal symbol a do if goto(Ii,a) = Ij then
make action [Ii, a] = Sj // for shift
❑ LALR(Look-ahead LR) parser

(b) For every state Ii in C where underlying sets of items contains an


item of the form A🡪 𝛼. ,a do make action [Ii, a] = Rk , where k is
the number of position.
(c) Make [Ii, $] = accept if Ii contains an item S’🡪 S. , $
❑ Steps to construct LALR(Look-ahead LR) parser
1. Augmented Grammar
2. Construct Canonical set of LR(1) items
3. Construct CLR parsing table
4. Check whether the grammar is LALR or NOT
5. Construct LALR parsing table
6. Parse the input string
❑ Example: Construct LALR parsing table for the given grammar

Grammar:
Question asked in GTU:
S🡪CC (i) Check whether the grammar is LALR or not.
C🡪 cC | d (ii) Construct LALR(1) parsing table for the following
grammar.

Step 1: Augmented Grammar

S’🡪 S r0
S 🡪 CC r1
C 🡪 cC r2
C🡪d r3
Step 2: Canonical collection of LR(1) items

I0 = S’🡪 .S , $ goto (I0 , d) = C🡪 d. , c | d I4 goto (I3 , d) = C🡪 d. , c | d I4

S🡪 .CC , $ goto (I6 , C) = C🡪 cC. , $ I9


C🡪 .cC , c|d goto (I2 , C) = S🡪 CC. , $ I5
C🡪 .d , c | d goto (I6 , c ) = C🡪 c.C , $ I6
C🡪 .cC , $
goto (I2 , c ) =C🡪 c.C , $ I6 C🡪 .d , $
goto (I0 , S) = S’🡪 S. , $ I1
C🡪 .cC , $
C🡪 .d , $
goto (I0 , C) = S🡪 C.C , $ I2 goto (I6 , d) = C🡪 d. , $ I7
C🡪 .cC , $
C🡪 .d , $ goto (I2 , d) = C🡪 d. , $ I7

goto (I0 , c ) = C🡪 c.C , c | d I3 goto (I3 , C) = C🡪 cC. , c | d I8


C🡪 .cC , c | d
C🡪 .d , c | d goto (I3 , c ) = C🡪 c.C , c | d I3
C🡪 .cC , c | d
C🡪 .d , c | d
Step 3: CLR parsing table

Step 4: Check whether the grammar is LALR or NOT

✔ If we see the canonical collection in the state (I3, I6), (I4, I7) and (I8, I9) have same LR(0)
items but different look-ahead symbols. Therefore the given grammar is LALR(1)
Step 5: LALR Parsing Table

Action goto Action goto


State c d $ S C State c d $ S C
I0 S3 S4 1 2 I0 S36 S47 1 2
I1 Accept I1 Accept

I2 S6 S7 5 I2 S36 S47 5
I3 S3 S4 8 I36 S36 S47 89
I4 r3 r3 I47 r3 r3 r3
I5 r1 I5 r1
I6 S6 S7 9 II896 S36
r2 S47
r2 r2 9
LALR Parsing Table
I7 r3 I7 r3
I8 r2 r2 I89 r2 r2 r2
I9 r2
CLR Parsing Table
I9 (I4, I7) and (I8, I9)r2
(I3, I6), have same LR(0) items
❑ Exercise
❖ Given grammar is CLR or LALR justify your answer. If LALR then construct
the parsing table for LALR.

1. S🡪Aa | bAc | dc | bda


A🡪d

2. S🡪 CC
C🡪 aC | d
THANK YOU

You might also like