Priority algorithm analysis
1. Known operator precedence relation matrix as follows:
+ |
* |
i |
( |
) |
# |
|
+ |
> |
< |
< |
< |
> |
> |
* |
> |
> |
< |
< |
> |
> |
i |
> |
> |
> |
> |
||
( |
< |
< |
< |
< |
= |
|
) |
> |
> |
> |
> |
||
# |
< |
< |
< |
< |
= |
Written symbol string (i + i) * i # analysis of operator precedence.
2. Connect jobs (P121 Exercise 1), to complete 4), 5) two steps.
1) calculating and FIRSTVT LASTVT.
2) find a three relationships right.
3) construction operator precedence table.
4) whether the operator priority to grammar?
5) given input string (a, (a, a)) # of operator precedence analysis.
grammar:
S->a | ^ | (T)
T->T,S | S
(1)
FIRSTVT(S)={a,^,(}
FIRSTVT(T)={, ,a,^,(}
LASTVT(S)={a,^,)}
Lastva (T) = {,, a, ^,)}
(2)
Symbol =
(T)
#S#
Symbols <
#S
(T
,S
Symbols>
S#
T)
T,
(3)
FIRSTVT(S)={a,^,(}
Symbols:
#S
,S
FIRSTVT(T)={, ,a,^,(}
Symbols:
(T
LASTVT(S)={a,^,)}
Symbols:
S#
Lastva (T) = {,, a, ^,)}
T)
T,
|
a |
^ |
( |
) |
, |
# |
a |
|
|
|
> |
> |
> |
^ |
|
|
|
> |
> |
> |
( |
< |
< |
< |
= |
< |
|
) |
|
|
|
> |
> |
> |
, |
< |
< |
< |
> |
> |
|
# |
< |
< |
< |
|
|
= |
(4) is the operator precedence grammar
(5) input string (a, (a, a)) # of operator priority analysis
|
Stack |
relationship |
Input string |
action |
1 |
# |
< |
(a,(a,a))# |
Moved into |
2 |
#( |
< |
a,(a,a))# |
Moved into |
3 |
#(a |
> |
,(a,a))# |
Reduction |
4 |
#(N |
< |
,(a,a))# |
Moved into |
5 |
#(N, |
< |
(a,a))# |
Moved into |
6 |
#(N,( |
< |
a,a))# |
Moved into |
7 |
#(N,(a |
> |
,a))# |
Reduction |
8 |
#(N,(N |
< |
,a))# |
Moved into |
9 |
#(N,(N, |
< |
a))# |
Moved into |
10 |
#(N,(N,a |
> |
))# |
Reduction |
11 |
#(N,(N,N |
> |
))# |
Reduction |
12 |
#(N,(N |
= |
))# |
Moved into |
13 |
#(N,(N) |
> |
)# |
Reduction |
14 |
#(N,N |
> |
)# |
Reduction |
15 |
#(N |
= |
)# |
Moved into |
16 |
#(N) |
> |
# |
Reduction |
17 |
#N |
|
# |
accept |
3. Try parser written bottom-up.
Expression can be written only partially.
4. Write a + b * (cd) + e / (cd) ↑ n the reverse Polish notation, ternary type, quaternion type.
answer:
Reverse Polish Notation: abcd- * ecd-n ↑ / ++
Three yuan type:
(1) (- , c ,d)
(2) (* , b , (1))
(3) (↑ ,(1) ,n)
(4) (/ , e ,(3))
(5) (+ , a, (2))
(6) (+ , (5) , (4))
Quaternion type:
(- , c ,d, (1))
(* , b , (1), (2))
(↑ ,(1) ,n, (3))
(/ , e ,(3),(4))
(+ , a, (2), (5))
(+ , (5) , (4),(6))