海外公司网站 国内做备案,广告公司网站首页,产品代理加盟区域代理,上海网站被查系列文章戳这里#x1f447;
什么是上下文无关文法、最左推导和最右推导如何判断二义文法及消除文法二义性何时需要消除左递归什么是句柄、什么是自上而下、自下而上分析什么是LL(1)、LR(0)、LR(1)文法、LR分析表LR(0)、SLR(1)、LR(1)、LALR(1)文法之间的关系编译原理第三章习…系列文章戳这里
什么是上下文无关文法、最左推导和最右推导如何判断二义文法及消除文法二义性何时需要消除左递归什么是句柄、什么是自上而下、自下而上分析什么是LL(1)、LR(0)、LR(1)文法、LR分析表LR(0)、SLR(1)、LR(1)、LALR(1)文法之间的关系编译原理第三章习题词法分析、构建DFA、上下文无关文法、LL(1)分析、提取正规式证明LL(1)、SLR(1)、LALR(1)文法翻译方案、属性栈代码 编译原理—翻译方案、属性栈代码系列文章戳这里属性栈代码举个栗子引入标记非终结符模拟继承属性的计算文法如下S→(L)∣aL→L,S∣SS → (L) | a\\ L → L, S | S S→(L)∣aL→L,S∣S(a) 写一个翻译方案它输出每个 a 的嵌套深度。例如对于句子 (a, (a, a))输出的结果是 1 2 2。文法符号SL继承属性depth表示嵌套深度则翻译方案如下S′→{S.depth0}SS→({L.depthS.depth}L)S→a{print(S.depth)}L→{L1.depthL.depth,S.depthL.depth}L1,SL→{S.depthL.depth}S\begin{aligned} S→\{S.depth0\}S\\ S→(\{L.depth S.depth\}L)\\ S→a\{print(S.depth)\}\\ L→\{L_1.depthL.depth,S.depthL.depth\}L_1,S\\ L→\{S.depthL.depth\}S \end{aligned} S′SSLL→{S.depth0}S→({L.depthS.depth}L)→a{print(S.depth)}→{L1.depthL.depth,S.depthL.depth}L1,S→{S.depthL.depth}S属性栈代码由于
属性栈上仅能存放综合属性后文有详细介绍所以需要引入标记非终结符P、Q、R及其综合属性s继承属性i模拟继承属性的计算则栈代码如下S′→{S.depthP.s}PSP→ϵ{P.s0}Stack[ntop]0S→({Q.iS.depth,L.depthQ.s}QL)Q→ϵ{Q.sQ.i1}Stack[ntop]Stack[top−1]1S→a{print(S.depth)}print(Stack[top−1])L→{L1.depthL.depth,R.iL.depth,S.depthR.s}L1,RSR→ϵ{R.sR.i}Stack[ntop]Stack[top−2]L→{S.depthL.depth}S\begin{aligned} S→\{S.depthP.s\}PS\\ P→\epsilon\{P.s0\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop]0\\ S→(\{Q.iS.depth,L.depth Q.s\}QL)\\ Q→\epsilon\{Q.sQ.i1\}\ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop] Stack[top-1]1\\ S→a\{print(S.depth)\}\ \ \ \ \ \ \ \ \ \ print(Stack[top-1])\\ L→\{L_1.depthL.depth,R.iL.depth,S.depthR.s\}L_1,RS\\ R→\epsilon\{R.sR.i\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop] Stack[top-2]\\ L→\{S.depthL.depth\}S \end{aligned} S′PSQSLRL→{S.depthP.s}PS→ϵ{P.s0} Stack[ntop]0→({Q.iS.depth,L.depthQ.s}QL)→ϵ{Q.sQ.i1} Stack[ntop]Stack[top−1]1→a{print(S.depth)} print(Stack[top−1])→{L1.depthL.depth,R.iL.depth,S.depthR.s}L1,RS→ϵ{R.sR.i} Stack[ntop]Stack[top−2]→{S.depthL.depth}S(b) 写一个翻译方案它打印出每个 a 在句子中是第几个字符。例如当句子是 (a, (a, (a, a),(a)))时打印的结果是 2 5 8 10 14。 使用综合属性out表示当前文法符号推出的字符总数基础属性before表示该文法符号前有多少个字符则翻译方案如下 S′→{S.before0}SS→{L.beforeS.before}(L){S.outL.out2}S→a{S.out1;print(S.before1)}L→{L1.beforeL.before,S.beforeL.beforeL1.out1}L1,S{L.outL1.outS.out1}L→{S.beforeL.before}S{L.outS.out}\begin{aligned} S→\{S.before0\}S\\ S→\{L.before S.before\} \\\ \ \ \ \ \ \ \ \ \ \ (L) \\\ \ \ \ \ \ \ \ \ \ \ \{S.outL.out2\}\\ S→a\{S.out1;print(S.before1)\}\\ L→\{L_1.beforeL.before, \\\ \ \ \ \ \ \ \ \ \ \ S.beforeL.beforeL_1.out1\} \\\ \ \ \ \ \ \ \ \ \ \ L_1,S \\\ \ \ \ \ \ \ \ \ \ \ \{L.outL_1.outS.out1\}\\ L→\{S.beforeL.before\}S\{L.outS.out\} \end{aligned} S′SSLL→{S.before0}S→{L.beforeS.before} (L) {S.outL.out2}→a{S.out1;print(S.before1)}→{L1.beforeL.before, S.beforeL.beforeL1.out1} L1,S {L.outL1.outS.out1}→{S.beforeL.before}S{L.outS.out} 同理上述翻译方案栈代码如下 S′→{S.beforeP.s}PSP→ϵ{P.s0}Stack[ntop]0S→({Q.iS.before,L.beforeQ.s}QL){S.outL.out2}Q→ϵ{Q.sQ.i1}Stack[ntop]Stack[top−1]1S→a{print(S.before)}print(Stack[top−1])L→{L1.beforeL.before,R.iL.beforeL1.out1,S.beforeR.s}L1,RS{L.outL1.outS.out1}Stack[ntop]Stack[top−3]Stack[top]1R→ϵ{R.sR.i}Stack[ntop]Stack[top−2]Stack[top−1]1L→{S.beforeL.before}S{L.outS.out}Stack[ntop]Stack[top]\begin{aligned} S→\{S.beforeP.s\}PS\\ P→\epsilon\{P.s0\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop]0\\ S→(\{Q.i S.before,L.before Q.s\} \\\ \ \ \ \ \ \ \ \ \ \ QL) \\\ \ \ \ \ \ \ \ \ \ \ \{S.outL.out2\} \\ Q→\epsilon\{Q.sQ.i1\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop] Stack[top-1]1\\ S→a\{print(S.before)\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ print(Stack[top-1])\\ L→\{L_1.beforeL.before, \\\ \ \ \ \ \ \ \ \ \ \ R.iL.beforeL_1.out1, \\\ \ \ \ \ \ \ \ \ \ \ S.beforeR.s\} \\\ \ \ \ \ \ \ \ \ \ \ L_1,RS \\\ \ \ \ \ \ \ \ \ \ \ \{L.outL_1.outS.out1\} \ \ \ \ \ \ \ \ \ Stack[ntop]Stack[top-3]Stack[top]1 \\ R→\epsilon\{R.sR.i\}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop] Stack[top-2]Stack[top-1]1\\ L→\{S.beforeL.before\}S \\ \ \ \ \ \ \ \ \{L.outS.out\} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Stack[ntop]Stack[top] \end{aligned} S′PSQSLRL→{S.beforeP.s}PS→ϵ{P.s0} Stack[ntop]0→({Q.iS.before,L.beforeQ.s} QL) {S.outL.out2}→ϵ{Q.sQ.i1} Stack[ntop]Stack[top−1]1→a{print(S.before)} print(Stack[top−1])→{L1.beforeL.before, R.iL.beforeL1.out1, S.beforeR.s} L1,RS {L.outL1.outS.out1} Stack[ntop]Stack[top−3]Stack[top]1→ϵ{R.sR.i} Stack[ntop]Stack[top−2]Stack[top−1]1→{S.beforeL.before}S {L.outS.out} Stack[ntop]Stack[top]
属性栈代码
如何在自底向上分析中计算继承属性 属性栈上仅能存放综合属性分析栈中符号的继承属性 属性copy规则 如果A→XYA→XYA→XY有语义规则Y.i:X.sY.i : X.sY.i:X.s 翻译方案可写为 A→X{Y.i:X.s}YA → X \{ Y.i : X.s \} YA→X{Y.i:X.s}Y 从句柄下面取继承属性!
举个栗子
有如下文法与翻译方案 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 分析过程如下 引入标记非终结符模拟继承属性的计算