编译原理 14 算符优先分析
1、
栈 | 关系 | 输入串 | 动作 | |
1 | # | < | (i+i)*i# | 移进 |
2 | #( | < | i+i)*i# | 移进 |
3 | #(i | > | +i)*i# | 规约 |
4 | #(N | < | +i)*i# | 移进 |
5 | #(N+ | < | i)*i# | 移进 |
6 | #(N+i | > | )*i# | 规约 |
7 | #(N+N | > | )*i# | 规约 |
8 | #(N | = | )*i# | 移进 |
9 | #(N) | > | *i# | 规约 |
10 | #N | < | *i# | 移进 |
11 | #N* | < | i# | 移进 |
12 | #N*i | > | # | 规约 |
13 | #N*N | > | # | 规约 |
14 | #N | # | 接受 |
2、
(1)
S->a | ^ | (T)
T->T,S | S
FIRSTVT(S)={a,^,(}
FIRSTVT(T)={, ,a,^,(}
LASTVT(S)={a,^,)}
LASTVT(T)={,,a,^,)}
(2)
(3)
|
a |
^ |
( |
) |
, |
# |
a |
|
|
|
> |
> |
> |
^ |
|
|
|
> |
> |
> |
( |
< |
< |
< |
= |
< |
|
) |
|
|
|
> |
> |
> |
, |
< |
< |
< |
> |
> |
|
# |
< |
< |
< |
|
|
= |
(4)
符合
(5)
栈 | 关系 | 输入串 | 动作 |
# | < | (a,(a,a))# | 移进 |
#( | < | a,(a,a))# | 移进 |
#(a | > | ,(a,a))# | 规约 |
#(N | < | ,(a,a))# | 移进 |
#(N | < | (a,a))# | 移进 |
#(N,( | < | a,a))# | 移进 |
#(N,(a | > | ,a))# | 规约 |
#(N,(N | < | ,a))# | 移进 |
#(N,(N | < | a))# | 移进 |
#(N,(N,a | > | ))# | 规约 |
#(N,(N,N | > | ))# | 规约 |
#(N,(N | < | ))# | 移进 |
#(N,(N) | = | )# | 规约 |
#(N,(N | > | )# | 规约 |
#(N | = | )# | 移进 |
#(N) | > | # | 规约 |
#(N | # | 接受 |
3.
void Isleft( )
{
Stack s;
k=1;
S[k]=’#’;
do{
a=S[k+1];
if (S[k]∈VT) j=k;
else j=k-1;
while(S[j]>a)
{
do{
Q=S[j];
if(S[j-1] ∈VT) j=j-1;
else j=j-2;
}while(S[j]>Q);
k=j+1;
S[k]=N;
}
if(S[j]<a || S[j]=a)
{
k=k+1;
S[k]=a;
}
}while(a!=’#’);
}
4.
(1)
abcd-*+ecd-n↑/+
三元式:(1) (- c,d)
(2) (↑ (1),n)
(3) (/ e,(2))
(4) (* b,(1))
(5) (+ a,(4))
(6) (+ (4),(3))
四元式:(1) (- c,d,t1)
(2) (↑ t1,n,t2)
(3) (/ e,t2,t3)
(4) (* b,t1,t4)
(5) (+ a,t4,t5)
(6) (+ t5,t3,t6)