1.文法G(Z):Z->aZb|ab定义的是什么样的语言?
文法G有两条产生式规则:
(1)Z->aZb
(2)Z->ab
通过对第一个产生式使用n-1次,然后使用第二个产生式一次,得到Z->aZb->aaZbb->aaaZbbb->aaaZbbb->......an-1bn-1->anbn
2.写出教材22页例2.2中标识符的文法四元组形式。
设I为标识符,L为字母,D为数字
VN为非空非终结符号,VT为非空终结符号,P为产生式集合,S为开始符号
P = { I -> L
I -> IL
I -> ID
......
L -> a
D -> 0
D -> 1
......
D -> 9 }
S = I
G:S ->L | IL | ID
L -> a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z
D -> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
3.写出下列表达式的最左推导、最右推导和语法树。
G(E):
E=> E + T | T
T=>T * F | F
F=>(E)| i
-
i*i+i
-
i+i*i
-
i+(i+i)
注意观察最左和最右推导过程的不同,以及语法树的异同。
i*i+i
最左推导:
E => E+T => T+T => T*F+T => F*F+T => i*F+T => i*i+T => i*i+F => i*i+i
最右推导:
E => E+T => E+F => E+i => T+i =>T*F+i => T*i+i => F*i+i => i*i+i
语法树:
i+i*i
最左推导:
E => E+T => T+T => F+T => i+T => i+T*F => i+F*F =>i+i*i
最右推导:
E => E+T =>E+T*F => E+T*i => E+F*i => E+i*i => T+i*i => F+i*i => i+i*i
语法树:
i+(i+i)
最左推导:
E => E+T => T+T => F+T => i+T => i+F => i+(E) => i+(E+T) => i+(T+T) => i+(F+T) => i+(i+T) => i+(i+F) => i+(i+i)
最右推导:
E => E+T => E+F => E+(E) => E+(E+T) => E+(E+F) => E+(E+i) => E+(T+i) => E+(F+i) => E+(i+i) => T+(i+i) => F+(i+i) => i+(i+i)
语法树: