Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views40 pages

UNIT 2 3 Operator

Module 2 covers syntax analysis, focusing on bottom-up parsing techniques such as shift-reduce parsing and operator precedence parsing. It explains key concepts like reductions, handles, and leading/trailing symbols, along with examples illustrating the parsing process. The module also discusses the construction of precedence functions to optimize parsing efficiency.

Uploaded by

VETRIVEL S
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)
6 views40 pages

UNIT 2 3 Operator

Module 2 covers syntax analysis, focusing on bottom-up parsing techniques such as shift-reduce parsing and operator precedence parsing. It explains key concepts like reductions, handles, and leading/trailing symbols, along with examples illustrating the parsing process. The module also discusses the construction of precedence functions to optimize parsing efficiency.

Uploaded by

VETRIVEL S
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/ 40

Module 2 – Syntax Analysis

Bottom Up Parsing
Reductions
• The process of “reducing" a string w to the start symbol of
the grammar.
• At each reduction step, a specific substring matching the
body of a production is replaced by the nonterminal at the
LHS/Head of that production.
• The key decisions during bottom-up parsing are about when
to reduce and about what production to apply, as the parse
proceeds.
• The reductions will be discussed in terms of the sequence of
strings
id * id; F * id; T * id; T * F; T; E
Bottom-Up Parsing
• A bottom-up parsing corresponds to the construction of
a parse tree for an input string beginning at the leaves
(the bottom) and working up towards the root (the top).

• Ex: A bottom-up parse for id * id

Grammar:
E-->E + T | T
T-->T * F | F
F-->(E) | id
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
Parsing) SLR
LL(1)
CLR
Recursiv
e LALR
descent
Handle & Handle pruning
• Handle: A “handle” of a string is a substring of the string that
matches the right side of a production, and whose reduction to
the non-terminal of the production is one step along the reverse
of rightmost derivation.
• Handle pruning: The process of discovering a handle and
reducing
EE+E it to appropriate left hand side non-terminal is known as
handle
EE*E pruning.
String: id1+id2*id3
Eid
Right sentential form Handle Production
Rightmost Derivation
id1+id2*id3 id1 Eid
E
E+id2*id3 id2 Eid
E+E
E+E*id3 id3 Eid
E+E*E
E+E*id3 E+E*E E*E EE*E
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/production 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
action.
4. Error: A situation in which parser cannot either shift or
reduce the symbols, it cannot even perform accept action
Example: Shift reduce parser
Grammar: Stack Input Buffer Action
EE+T | T
$ id+id*id$ Shift
TT*F | F
$id +id*id$ Reduce Fid
Fid
$F +id*id$ Reduce TF
String:
id+id*id $T +id*id$ Reduce ET
$E +id*id$ Shift
$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
Example 2
Consider the grammar
E –> 2E2
E –> 3E3
E –> 4
Perform Shift Reduce parsing for input string “32423”.
Example 2 - Solution
Example 3
Consider the grammar
S –> ( L ) | a
L –> L , S | S
Perform Shift Reduce parsing for input string “( a, ( a,
a ) ) “.
Example 3 - Solution
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursi
ve LALR

descen
Operator precedence parsing
• An operator precedence parser is a bottom-up parser that
interprets an operator grammar
• 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.
• In operator precedence
Relation parsing Meaningwe define following disjoint
relations: a<.b a “yields precedence to” b
a=b a “has the same precedence as” b
a.>b a “takes precedence over” b
Precedence & associativity of operators
Operator Precedence Associative
↑ 1 right
*, / 2 left
+, - 3 left
Steps of operator precedence parsing
1. Find Leading and trailing of non terminal
2. Establish relation and Creation of table
3. Parse the string
Leading & Trailing
Leading:- Leading of a non-terminal is the first terminal or
operator in production of that non-terminal.
Trailing:- Trailing of a non-terminal is the last terminal or
operator in production of that non-terminal.
Example 1: EE+T | T Rule:
TT*F | F 1. If A αaβ and (α is a single
variable or ε) then Leading (A) =
Fid a
2. If A Ba|β and β is a variable (NT)
then Leading (A) = Leading (β)
Non terminal Leading Trailing
E {+,*,id} {+,*,id}
T {*,id} {*,id}
F {id} {id}
Leading & Trailing
Example 2: S-->a | ^ | (T)
T-->T,S | S

Non terminal Leading Trailing


Rule:
1. If A αaβ and (α is a single
S {a,^, (} {a,^,)}
variable or ε) then Leading (A) =
T {,,a,^, (} {,,a,^,)} a
2. If A Ba|β and β is a variable
(NT) then Leading (A) = Leading
(β)
Rules to establish a relation
1. For a = b, , where is or a single non-terminal [e.g : (E)]
2. a <.b [e.g : +T]
3. a .>b [e.g : E+]
4. $ <. Leading (start symbol)
5. Trailing (start symbol) .> $
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+T|T
Nonterminal Leading Trailing
T T*F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}

Step 2 (a): Establish Relation Step2(b): Creation of Table


+ * id $
1. a <.b + .
> <. <. .
>
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+ T| T
Nonterminal Leading Trailing
T T*F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}

Step 2 (a): Establish Relation Step2(b): Creation of Table


+ * id $
1. a .>b + .
> <. <. .
>
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+ T| T
Nonterminal Leading Trailing
T T*F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}

Step 2 (a): Establish Relation Step2(b): Creation of Table


+ * id $
1. $<. Leading (start symbol) + .
> <. <. .
>
2. $ <. * .
> .
> <. .
>
3. Trailing (start symbol) .> $ id .
> .
> .
>
Accept
$ <. <. <.
Example: Operator precedence parsing
Step 3: Parse the string using precedence table

+ * id $
+ .
> <. <. .
>
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
Precedence function
• Disadvantage of precedence table
• If we have ‘n’ operators then size of table will be n*n and
complexity will be O(n2).
• In order to decrease the size of table, we use operator function
table.

• Precedence function
• Used to store precedence relation into encoded form
• Two functions ‘f’ and ‘g’ are used to map terminal symbols into
integers
• There exists symbols ‘fa’ and ‘ga’ for each terminal ‘a’
• The precedence relations between the symbols are
implemented by numerical comparison
Operator precedence function
Algorithm for constructing precedence functions
1. Create functions and for each ’ that is terminal or .
2. Partition the symbols in as many as groups possible, in such a way that
and are in the same group if .
3. Create a directed graph whose nodes are in the groups, next for each
symbols do:
a) if , place an edge from the group of to the group of
b) if , place an edge from the group of to the group of
4. If the constructed graph has a cycle then no precedence functions exist.
When there are no cycles collect the length of the longest paths from the
groups of and respectively.
• fa​= a number assigned to terminal a when it appears on the stack.
• ga​= a number assigned to terminal a when it appears in the input.
Operator precedence function
1. Create functions fa and ga for each a that is terminal orE$.
E+T | T
T T*F | F
F id

f+ f* fid f$

g+ g* gid g$
Operator precedence function
• Partition
. the symbols in as many as groups possible, in such a
way that fa and gb are in the same group if a = b.
+ * id $
+ .
> <. <. .
>
gid fid
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
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 * .
> .
> <. .
>
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 * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.

g+ f+
f+ <. gid f+  gid
f* <. gid f*  gid
f$ <. gid f$  gid
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$
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
+ * id $
f 2
gid fid
g 1

f* g*

g+ f+

f$ g$
Operator precedence function
+ * id $
f 2 4
gid fid
g 1

f* g*

g+ f+

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

f* g*

g+ f+

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

f* g*

g+ f+

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

f* g*

g+ f+

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

f* g*

g+ f+

f$ g$

The complexity is reduced to


O(2*n).
Example 2: Operator precedence
parsing
Construct Precedence parsing table for the following
grammar:
S-->a | ^ | (T)
T-->T,S | S
Soln:
Step 1: Find Leading & Trailing of
NT

Non terminal Leading Trailing


S {a,^, (} {a,^,)}
T {,,a,^, (} {,,a,^,)}
Example 2: Operator precedence
parsing
S-->a | ^ | (T)
T-->T,S | S
Step 2(a): Establish Relation Step 2(b): Creation of Table

a ^ ( ) , $

a .> .> .>

^ .> .> .>

( <. <. <. = <.

) .> .> .>

, <. <. <. .> .>

$ <. <. <.


Example 2: Operator precedence parsing
Step 3: Parse the string using
precedence table

a ^ ( ) , $

a .> .> .>

^ .> .> .>

( <. <. <. = <.

) .> .> .>

, <. <. <. .> .>

$ <. <. <.

You might also like