0% found this document useful (0 votes)
46 views23 pages

Operator Precendance

Uploaded by

fitara8437
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)
46 views23 pages

Operator Precendance

Uploaded by

fitara8437
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/ 23

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.
• Ex/
• A + | * | -
• Above grammar is not operator grammar because right side EAE has
consecutive non terminals.
• In operator precedence parsing we define following disjoint relations:
Relation Meaning
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
3. Creation of table
4. 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: EE+T | T
TT*F | F
Fid
Non terminal Leading Trailing
E {+,*,id} {+,*,id}
T {*,id} {*,id}
F {id} {id}
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: Establish Relation Step3: 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}

Step2: Establish Relation Step3: 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: Establish Relation Step 3: Creation of Table


+ * id $
1. $<. Leading (start symbol) + .
> <. <. .
>
2. $ <. * .
> .
> <. .
>
3. Trailing (start symbol) .> $ id .
> .
> .
>
$ <. <. <.
Example: Operator precedence parsing
Step 4: Parse the string using precedence table
1. Scan the input string until first .> is encountered.
2. Scan backward until <. is encountered.
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
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.
Operator precedence function
1. Create functions fa and ga for each a that is terminal or $. E 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 f a 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 fa 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 fa 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 fa 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 fa 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$

You might also like