(b) 写一个翻译方案,它打印出每个 a 在句子中是第几个字符。例如,当句子是 (a, (a, (a, a),(a)))时,打印的结果是 2 5 8 10 14。
使用综合属性out表示当前文法符号推出的字符总数,基础属性before表示该文法符号前有多少个字符,则翻译方案如下:
S ′ → { S . b e f o r e = 0 } S S → { L . b e f o r e = S . b e f o r e } ( L ) { S . o u t = L . o u t + 2 } S → a { S . o u t = 1 ; p r i n t ( S . b e f o r e + 1 ) } L → { L 1 . b e f o r e = L . b e f o r e , S . b e f o r e = L . b e f o r e + L 1 . o u t + 1 } L 1 , S { L . o u t = L 1 . o u t + S . o u t + 1 } L → { S . b e f o r e = L . b e f o r e } S { L . o u t = S . o u t } \begin{aligned} S'&→\{S.before=0\}S\\ S&→\{L.before = S.before\} \\&\ \ \ \ \ \ \ \ \ \ \ (L) \\&\ \ \ \ \ \ \ \ \ \ \ \{S.out=L.out+2\}\\ S&→a\{S.out=1;print(S.before+1)\}\\ L&→\{L_1.before=L.before, \\&\ \ \ \ \ \ \ \ \ \ \ S.before=L.before+L_1.out+1\} \\&\ \ \ \ \ \ \ \ \ \ \ L_1,S \\&\ \ \ \ \ \ \ \ \ \ \ \{L.out=L_1.out+S.out+1\}\\ L&→\{S.before=L.before\}S\{L.out=S.out\} \end{aligned} S′SSLL→{S.before=0}S→{L.before=S.before} (L) {S.out=L.out+2}→a{S.out=1;print(S.before+1)}→{L1.before=L.before, S.before=L.before+L1.out+1} L1,S {L.out=L1.out+S.out+1}→{S.before=L.before}S{L.out=S.out}
同理上述翻译方案栈代码如下:
S ′ → { S . b e f o r e = P . s } P S P → ϵ { P . s = 0 } S t a c k [ n t o p ] = 0 S → ( { Q . i = S . b e f o r e , L . b e f o r e = Q . s } Q L ) { S . o u t = L . o u t + 2 } Q → ϵ { Q . s = Q . i + 1 } S t a c k [ n t o p ] = S t a c k [ t o p − 1 ] + 1 S → a { p r i n t ( S . b e f o r e ) } p r i n t ( S t a c k [ t o p − 1 ] ) L → { L 1 . b e f o r e = L . b e f o r e , R . i = L . b e f o r e + L 1 . o u t + 1 , S . b e f o r e = R . s } L 1 , R S { L . o u t = L 1 . o u t + S . o u t + 1 } S t a c k [ n t o p ] = S t a c k [ t o p − 3 ] + S t a c k [ t o p ] + 1 R → ϵ { R . s = R . i } S t a c k [ n t o p ] = S t a c k [ t o p − 2 ] + S t a c k [ t o p − 1 ] + 1 L → { S . b e f o r e = L . b e f o r e } S { L . o u t = S . o u t } S t a c k [ n t o p ] = S t a c k [ t o p ] \begin{aligned} S'&→\{S.before=P.s\}PS\\ P&→\epsilon\{P.s=0\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop]=0\\ S&→(\{Q.i = S.before,L.before = Q.s\} \\&\ \ \ \ \ \ \ \ \ \ \ QL) \\&\ \ \ \ \ \ \ \ \ \ \ \{S.out=L.out+2\} \\ Q&→\epsilon\{Q.s=Q.i+1\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop]= Stack[top-1]+1\\ S&→a\{print(S.before)\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ print(Stack[top-1])\\ L&→\{L_1.before=L.before, \\&\ \ \ \ \ \ \ \ \ \ \ R.i=L.before+L_1.out+1, \\&\ \ \ \ \ \ \ \ \ \ \ S.before=R.s\} \\&\ \ \ \ \ \ \ \ \ \ \ L_1,RS \\&\ \ \ \ \ \ \ \ \ \ \ \{L.out=L_1.out+S.out+1\} \ \ \ \ \ \ \ \ \ Stack[ntop]=Stack[top-3]+Stack[top]+1 \\ R&→\epsilon\{R.s=R.i\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop] = Stack[top-2]+Stack[top-1]+1\\ L&→\{S.before=L.before\}S \\& \ \ \ \ \ \ \ \{L.out=S.out\} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop]=Stack[top] \end{aligned} S′PSQSLRL→{S.before=P.s}PS→ϵ{P.s=0} Stack[ntop]=0→({Q.i=S.before,L.before=Q.s} QL) {S.out=L.out+2}→ϵ{Q.s=Q.i+1} Stack[ntop]=Stack[top−1]+1→a{print(S.before)} print(Stack[top−1])→{L1.before=L.before, R.i=L.before+L1.out+1, S.before=R.s} L1,RS {L.out=L1.out+S.out+1} Stack[ntop]=Stack[top−3]+Stack[top]+1→ϵ{R.s=R.i} Stack[ntop]=Stack[top−2]+Stack[top−1]+1→{S.before=L.before}S {L.out=S.out} Stack[ntop]=Stack[top]
针对以下文法
E → E ’ > ’ E ∣ E ’ < ’ E ∣ n u m b e r E → E ’>’ E\\ \ \ \ \ \ \ \ \ \ \ | E ’<’ E\\ \ \ \ \ \ \ \ \ \ \ | number E→E’>’E ∣E’<’E ∣number
(c)设计语法制导定义,使之能计算诸如 1 < 2 < 3 的表达式值为 True;而计算表达式 1 < 5 >
3 的值也为 True。
用综合属性 lef t 和 right 表示 E 推出的字符序列中最左和最右的数字,综合属性 bool 表
示 E 推出的表达式的布尔值,语法制导的定义如下:
给出生成如下 C 风格for语句中间代码的翻译方案;假定代码生成的顺序不变。
S → f o r ( E 1 ; E 2 ; E 3 ) S 1 S → for(E1; E2; E3) S1 S→for(E1;E2;E3)S1
首先给出中间代码的结构
据此给出一个翻译方案如下
有如下文法与翻译方案
D→T { L.in := T.type } L
T→int { T.type := integer }
T→real { T.type := real }
L→ { L1.in := L.in }
L1 , id {addtype(id.entry, L.in) }
L→id { addtype(id.entry, L.in) }
对于输入串:int p,q,r
分析过程如下: