Operator Precendance
Operator Precendance
$ $ 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*
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$