编译原理龙书课后部分答案(英文版)

合集下载

编译原理课后习题答案

编译原理课后习题答案

第一章1.典型的编译程序在逻辑功能上由哪几部分组成?答:编译程序主要由以下几个部分组成:词法分析、语法分析、语义分析、中间代码生成、中间代码优化、目标代码生成、错误处理、表格管理。

2. 实现编译程序的主要方法有哪些?答:主要有:转换法、移植法、自展法、自动生成法。

3. 将用户使用高级语言编写的程序翻译为可直接执行的机器语言程序有哪几种主要的方式?答:编译法、解释法。

4. 编译方式和解释方式的根本区别是什么?答:编译方式:是将源程序经编译得到可执行文件后,就可脱离源程序和编译程序单独执行,所以编译方式的效率高,执行速度快;解释方式:在执行时,必须源程序和解释程序同时参与才能运行,其不产生可执行程序文件,效率低,执行速度慢。

第二章1.乔姆斯基文法体系中将文法分为哪几类?文法的分类同程序设计语言的设计与实现关系如何?答:1)0型文法、1型文法、2型文法、3型文法。

2)2. 写一个文法,使其语言是偶整数的集合,每个偶整数不以0为前导。

答:Z→SME | BS→1|2|3|4|5|6|7|8|9M→ε | D | MDD→0|SB→2|4|6|8E→0|B3. 设文法G为:N→ D|NDD→ 0|1|2|3|4|5|6|7|8|9请给出句子123、301和75431的最右推导和最左推导。

答:N⇒ND⇒N3⇒ND3⇒N23⇒D23⇒123N⇒ND⇒NDD⇒DDD⇒1DD⇒12D⇒123N⇒ND⇒N1⇒ND1⇒N01⇒D01⇒301N⇒ND⇒NDD⇒DDD⇒3DD⇒30D⇒301N⇒ND⇒N1⇒ND1⇒N31⇒ND31⇒N431⇒ND431⇒N5431⇒D5431⇒75431N⇒ND⇒NDD⇒NDDD⇒NDDDD⇒DDDDD⇒7DDDD⇒75DDD⇒754DD⇒7543D⇒75431 4. 证明文法S→iSeS|iS| i是二义性文法。

答:对于句型iiSeS存在两个不同的最左推导:S⇒iSeS⇒iiSesS⇒iS⇒iiSeS所以该文法是二义性文法。

编译原理教程课后习题答案

编译原理教程课后习题答案

第二章 词法分析2.1 完成下列选择题: (1) 词法分析器的输出结果是 。

a. 单词的种别编码 b. 单词在符号表中的位置 c. 单词的种别编码和自身值 d. 单词自身值 (2) 正规式M1和M2等价是指 。

a. M1和M2的状态数相等 b. M1和M2的有向边条数相等 c. M1和M2所识别的语言集相等 d. M1和M2状态数和有向边条数相等 (3) DFA M(见图2-1)接受的字集为 。

a. 以0开头的二进制数组成的集合 b. 以0结尾的二进制数组成的集合 c. 含奇数个0的二进制数组成的集合 d. 含偶数个0的二进制数组成的集合 【解答】 (1) c (2) c (3) d图2-1 习题的DFA M2.2 什么是扫描器?扫描器的功能是什么? 【解答】 扫描器就是词法分析器,它接受输入的源程序,对源程序进行词法分析并识别出一个个单词符号,其输出结果是单词符号,供语法分析器使用。

通常是把词法分析器作为一个子程序,每当词法分析器需要一个单词符号时就调用这个子程序。

每次调用时,词法分析器就从输入串中识别出一个单词符号交给语法分析器。

2.3 设M=({x,y}, {a,b}, f, x, {y})为一非确定的有限自动机,其中f 定义如下: f(x,a)={x,y} f {x,b}={y} f(y,a)=Φ f{y,b}={x,y} 试构造相应的确定有限自动机M ′。

【解答】 对照自动机的定义M=(S,Σ,f,So,Z),由f 的定义可知f(x,a)、f(y,b)均为多值函数,因此M 是一非确定有限自动机。

先画出NFA M 相应的状态图,如图2-2所示。

图2-2 习题的NFA M用子集法构造状态转换矩阵,如表表2-1 状态转换矩阵1b将转换矩阵中的所有子集重新命名,形成表2-2所示的状态转换矩阵,即得到 M ′=({0,1,2},{a,b},f,0,{1,2}),其状态转换图如图2-3所示。

编译原理教程课后习题答案——第三章

编译原理教程课后习题答案——第三章

第三章语法分析3.1 完成下列选择题:(1) 文法G:S→xSx|y所识别的语言是。

a. xyxb. (xyx)*c. xnyxn(n≥0)d. x*yx*(2) 如果文法G是无二义的,则它的任何句子α。

a. 最左推导和最右推导对应的语法树必定相同b. 最左推导和最右推导对应的语法树可能不同c. 最左推导和最右推导必定相同d. 可能存在两个不同的最左推导,但它们对应的语法树相同(3) 采用自上而下分析,必须。

a. 消除左递 a. 必有ac归b. 消除右递归c. 消除回溯d. 提取公共左因子(4) 设a、b、c是文法的终结符,且满足优先关系ab和bc,则。

b. 必有cac. 必有bad. a~c都不一定成立(5) 在规范归约中,用来刻画可归约串。

a. 直接短语b. 句柄c. 最左素短语d. 素短语(6) 若a为终结符,则A→α·aβ为项目。

a. 归约b. 移进c. 接受d. 待约(7) 若项目集Ik含有A→α·,则在状态k时,仅当面临的输入符号a∈FOLLOW(A)时,才采取“A→α·”动作的一定是。

a. LALR文法b. LR(0)文法c. LR(1)文法d. SLR(1)文法(8) 同心集合并有可能产生新的冲突。

a. 归约b. “移进”/“移进”c.“移进”/“归约”d. “归约”/“归约”【解答】(1) c (2) a (3) c (4) d (5) b (6) b (7) d (8) d3.2 令文法G[N]为G[N]: N→D|NDD→0|1|2|3|4|5|6|7|8|9(1) G[N]的语言L(G[N])是什么?(2) 给出句子0127、34和568的最左推导和最右推导。

【解答】(1) G[N]的语言L(G[N])是非负整数。

(2) 最左推导:NNDNDDNDDDDDDD0DDD01DD012D0127NNDDD3D34NNDNDDDDD5DD56D568最右推导:NNDN7ND7N27ND27N127D1270127NNDN4D434NNDN8ND8N68D685683.3 已知文法G[S]为S→aSb|Sb|b,试证明文法G[S]为二义文法。

编译原理 龙书答案

编译原理 龙书答案

第四章部分习题解答Aho:《编译原理技术与工具》书中习题(Aho)4.1 考虑文法S → ( L ) | aL → L, S | Sa)列出终结符、非终结符和开始符号解:终结符:(、)、a、,非终结符:S、L开始符号:Sb)给出下列句子的语法树i)(a, a)ii)(a, (a, a))iii)(a, ((a, a), (a, a)))c)构造b)中句子的最左推导i)S(L)(L, S) (S, S) (a, S) (a, a)ii)S(L)(L, S) (S, S) (a, S) (a, (L)) (a, (L, S)) (a, (S, S)) (a, (a, S) (a, (a, a))iii)S(L)(L, S) (S, S) (a, S) (a, (L)) (a, (L, S)) (a, (S, S)) (a, ((L), S)) (a, ((L, S), S)) (a, ((S, S), S)) (a, ((a, S), S))(a, ((a, a), S)) (a, ((a, a), (L))) (a, ((a, a), (L, S))) (a, ((a, a), (S, S))) (a, ((a, a), (a, S))) (a, ((a, a), (a, a)))d)构造b)中句子的最右推导i)S(L)(L, S) (L, a) (S, a) (a, a)ii)S(L)(L, S) (L, (L)) (L, (L, S)) (L, (L, a)) (L, (S, a)) (L, (a, a)) (S, (a, a)) (a, (a, a))iii)S(L)(L, S) (L, (L)) (L, (L, S)) (L, (L, (L))) (L, (L, (L, S))) (L, (L, (L, a))) (L, (L, (S, a))) (L, (L, (a, a))) (L, (S,(a, a))) (L, ((L), (a, a))) (L, ((L, S), (a, a))) (L, ((L, a), (a,a))) (L, ((S, a), (a, a))) (L, ((a, a), (S, S))) (S, ((a, a), (a,a))) (a, ((a, a), (a, a)))e)该文法产生的语言是什么解:设该文法产生语言(符号串集合)L,则L = { (A1, A2, …, A n) | n是任意正整数,A i=a,或A i∈L,i是1~n之间的整数}(Aho)4.2考虑文法S→aSbS | bSaS |a)为句子构造两个不同的最左推导,以证明它是二义性的S aSbS abS abaSbS ababS ababS aSbS abSaSbS abaSbS ababS ababb)构造abab对应的最右推导S aSbS aSbaSbS aSbaSb aSbab ababS aSbS aSb abSaSb abSab ababc)构造abab对应语法树d)该文法产生什么样的语言?解:生成的语言:a、b个数相等的a、b串的集合(Aho)4.3 考虑文法bexpr→bexpr or bterm | btermbterm→bterm and bfactor | bfactorbfactor→not bfactor | ( bexpr ) | true | falsea)试为句子not ( true or false)构造分析树解:b)试证明该文法产生所有布尔表达式证明:一、首先证明文法产生的所有符号串都是布尔表达式变换命题形式——以bexpr、bterm、bfactor开始的推导得到的所有符号串都是布尔表达式最短的推导过程得到true、false,显然成立假定对步数小于n的推导命题都成立考虑步数等于n 的推导,其开始推导步骤必为以下情况之一bexpr bexpr or btermbexpr btermbterm bterm and bfactorbexpr bfactorbfactor not bfactorbfactor ( bexpr )而后继推导的步数显然<n,因此由归纳假设,第二步句型中的NT推导出的串均为布尔表达式,这些布尔表达式经过or、and、not运算或加括号,得到的仍是布尔表达式因此命题一得证。

编译原理(龙书)课后习题解答(详细)

编译原理(龙书)课后习题解答(详细)

编译原理(龙书)课后习题解答(详细)编译原理(龙书)课后题解答第一章1.1.1 :翻译和编译的区别?答:翻译通常指自然语言的翻译,将一种自然语言的表述翻译成另一种自然语言的表述,而编译指的是将一种高级语言翻译为机器语言(或汇编语言)的过程。

1.1.2 :简述编译器的工作过程?答:编译器的工作过程包括以下三个阶段:(1) 词法分析:将输入的字符流分解成一个个的单词符号,构成一个单词符号序列;(2) 语法分析:根据语法规则分析单词符号序列中各个单词之间的关系,确定它们的语法结构,并生成抽象语法树;(3) 代码生成:根据抽象语法树生成目标程序(机器语言或汇编语言),并输出执行文件。

1.2.1 :解释器和编译器的区别?答:解释器和编译器的主要区别在于执行方式。

编译器将源程序编译成机器语言或汇编语言等,在运行时无需重新编译,程序会一次性运行完毕;而解释器则是边翻译边执行,每次执行都需要进行一次翻译,一次只执行一部分。

1.2.2 :Java语言采用的是解释执行还是编译执行?答:Java一般是编译成字节码的形式,然后由Java虚拟机(JVM)进行解释执行。

但是,Java也有JIT(即时编译器)的存在,当某一段代码被多次执行时,JIT会将其编译成机器语言,提升代码的执行效率。

第二章2.1.1 :使用BNF范式定义简单的加法表达式和乘法表达式答:<加法表达式> ::= <加法表达式> "+" <乘法表达式> | <乘法表达式><乘法表达式> ::= <乘法表达式> "*" <单项式> | <单项式><单项式> ::= <数字> | "(" <加法表达式> ")"2.2.3 :什么是自下而上分析?答:自下而上分析是指从输入字符串出发,自底向上构造推导过程,直到推导出起始符号。

编译原理课后习题答案

编译原理课后习题答案

编译原理课后习题答案编译原理习题答案习题11.1翻译程序:把⽤某种程序设计语⾔(源语⾔)编写的程序(源程序)翻译成与之等价的另⼀种语⾔(⽬标语⾔)的程序(⽬标程序)。

编译程序:⼀种翻译程序,将⾼级语⾔编写的源程序翻译成等价的机器语⾔或汇编语⾔的⽬标程序。

1.2词法分析、语法分析、语义分析和中间代码⽣成、代码优化、⽬标代码⽣成1.3词法分析:根据语⾔的词法规则对构成源程序的符号进⾏扫描和分解,识别出⼀个个的单词。

语法分析:根据语⾔的语法规则,把单词符号串分解成各类语法单位。

语义分析及中间代码⽣成:对语法分析识别出的语法单位分析其含义,并进⾏初步翻译。

代码优化:对中间代码进⾏加⼯变换,以产⽣更⾼效的⽬标代码。

⽬标代码⽣成:将中间代码变换成特定机器上的绝对指令代码、可重定位的指令代码或会变指令代码。

以上5个阶段依次执⾏。

习题22.1 (1)有穷⾮空的符号集合(2)利⽤产⽣是规则A->v将A替换为v时与A的上下⽂⽆关。

(3)略(4)推导是把句型中的⾮终结符⽤⼀个产⽣是规则的右部开替代的过程;直接推导是将⾮终结符的替代结果只⽤了⼀次产⽣式规则。

(5)略(6)⼀个句型的最左直接短语(7)如果⼀个⽂法存在某个句⼦对应两棵不同的语法树或有两个不同的最左(右)推导,则称这个⽂法是⼆义的。

2.2(1)VN ={Z,A,B} VT ={a,b,c,d,e}(2)abbcde,abbbcde是,acde不是。

2.3 (1)L[G]={d|n≥1,m≥0}(2)2.4 (1) A=>B=>c=>fAg=>fBg=>fCg=>feg(2)A=>AaB=>AaC=>Aae=>Bae=>BcCae=>Bceae=>Cceae=>eceae(3)A=>B=>BcC=>BcfAg=>BcfAaBg=>BcfAaCg=>BcfAaeg=>BcfBaeg =>BcfCaeg=>Bcfeaeg=>Ccfeaeg=>ecfeaeg(3)中题⽬有错应为C fCg|e2.5L[G]={a?b?c?|aab,n≥2}2.6 (1)Z→AB A→Aa|ε B→Bb|ε(2)Z→aZb|ab(3)Z→aAb A→aAb|b(4)Z→AB A→aAb|ab B→cB|ε(5)Z→aaAb|ab Z→aaBb|bb A→aaAb|ab B→aaBb|bb2.7 ⼀位数:Z→2|4|6|8两位数:Z→AB A→1|2|3|4|5|6|7|8|9 B→0|2|4|6|8三位以上:Z→ACB A→1|2|3|4|5|6|7|8|9 B→0|2|4|6|8 C→CDD→0|1|2|3|4|5|6|7|8|92.8证明:E=>E+T=>E+T*F短语:T*F E+T*F 直接短语:T*F 句柄:T*F2.9 语法树: E 短语:E*T , (E*T) , F↑(E*T) ,F ,E* F↑(E*T)E *F 直接短语:E*T , FT ↑ F 句柄:FF ( E )E * T2.10(1)语法树(2)直接短语:a , ZZ 句柄:Z( L )L , ZZ ( L )Za2.11最左推导:Z=>ZaB=>BaB=>B+AaB=>A+AaB=>(+)Z*aB=>(+)ZaB*aB =>(+)+aB*aB=>(+)+aA*aB=>(+)+a(*aB=>(+)+a(*aA=>(+)+a(*a(直接短语:(,+句柄:(2.12(1) S=>iSeS=>iiSeS=>iiIeS=>iiIeIS=>iS=>iiSeS=>iiIeS=>iiIeI(2) S=>SaS=>cSaS=>cfaS=>cfafS=>cS=>cSaS=>cfaS=>cfaf(3) E=>EOE=>EOEOE=>iOEOE=>i+EOE=>i+iOE=>i+i-E=>i+i-iE=>EOE=>iOE=>i+E=>i+EOE=>i+iOE=>i+i-E=>i+i-i2.13 Z→aABZ|cCACdA→bAB|aZA|cCCB→bAB|CzbC→cZ|c习题33.1(1)确定的有限⾃动机(2)不确定的有限⾃动机(3)正规集是⼀类特殊的单词集合,正规式是正规集的描述⼯具 3.2 (1) (1|2|3|4|5|6|7|8|9|0)*(1|3|5|7|9) (2) 11(0|1)*00 3.3 证明:b *(a|b)+={a,b,ab,ba,aa,bb …} (a|b)+={a,b,ab,ba,aa,bb …} 3.4 (1)(2)DDDD3.5(1) (2)(3)3.6(1) (01|10) *(01|10)(2) (0(1|00)*)|003.7(1) Z →1AB (2)Z →ABA →(0|1)A A →0A|εA →0|1B →(0|1)B|ε B →0B B →ε3.8 r=a(a|b )*bb3.9 Z →1BB →0Z|0 Z →0Z|ε3.10 3.11DDD习题44.1 (1)若⽂法G[Z]满⾜①⽂法不含左递归②③(2)4.2(1) First(S)={a,d} First(B)={a,d,c,ε}First(A)={a,d,e,c} First(D)={a,d,ε}Follow(S)={#,a,b,d,e} Follow(B)={a,d}Follow(A)={b} Follow(D)={e,a,d,b}(2) 不是4.3 (1) 证明: First(Z)={a,b,c} Follow(S)={#,a,b,c,d} First(A)={a,b,c,d} Follow(A)={ #,a,b,c,d }First(B)={a,d,c} Follow(B)={ a,b,c,d } 是LL(1)⽂法。

编译原理课后习题答案ch9

编译原理课后习题答案ch9

盛威网()专业的计算机学习网站
2
《编译原理》课后习题答案第九章
附加题
问题 1: 利用 Pascal 的作用域规则,试确定在下面的 Pascal 程序中的名字 a 和 b 的每一次出现 所应用的说明。 program m ( input, output ) ; procedure n ( u, v, x, y : integer ) ; var m : record m, n : interger end ; n : record n, m : interger end ; begin with m do begin m := u ; n:= v end ; with n do begin m := x ; n := y end ; writeln ( m.m, m.n, n.m, n.n ) end ; begin m ( 1, 2, 3, 4 ) end. 答案: 图中用蓝色数字下标相应标明。 program m1 ( input, output ) ; procedure n1( u, v, x, y : integer ) ; var m2 : record m3, n2 : interger end ; n3 : record n4, m4 : interger end ; begin with m2 do begin m3 := u ; n2 := v end ; with n3 do begin m4 := x ; n4 := y end ; writeln ( m2.m3, m2.n2, n3.m4, n3.n4 ) end ; begin m1 ( 1, 2, 3, 4 ) end. 问题 2: 当一个过程作为参数被传递时,我们假定有以下三种与此相联系的环境可以考虑,下 面的 Pascal 程序是用来说明这一问题的。 一种是词法环境 (lexical environment) , 如此这样的一个过程的环境是由这一过程定义 之处的各标识符的联编所构成; 一种是传递环境(passing environment) ,是由这一过程作为参数被传递之处的各标识

编译原理龙书课后部分答案(英文版)

编译原理龙书课后部分答案(英文版)

1) What is the difference between a compiler and an interpreter?• A compiler is a program that can read a program in one language - the source language - and translate it into an equivalent program in another language – the target language and report any errors in the source program that it detects during the translation process.• Interpreter directly executes the operations specified in the source program on inputs supplied by the user.2) What are the advantages of:(a) a compiler over an interpretera. The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs.(b) an interpreter over a compiler?b. An interpreter can usually give better error diagnostics than a compiler, because it executes the source program statement by statement.3) What advantages are there to a language-processing system in which the compiler produces assembly language rather than machine language?The compiler may produce an assembly-language program as its output, becauseassembly language is easier to produce as output and is easier to debug.4.2.3 Design grammars for the following languages:a) The set of all strings of 0s and 1s such that every 0 is immediately followed by at least 1.S -> SS | 1 | 01 | ε4.3.1 The following is a grammar for the regular expressions over symbols a and b only, using + in place of | for unions, to avoid conflict with the use of vertical bar as meta-symbol in grammars:rexpr -> rexpr + rterm | rtermrterm -> rterm rfactor | rfactorrfactor -> rfactor * | rprimaryrprimary -> a | ba) Left factor this grammar.rexpr -> rexpr + rterm | rtermrterm -> rterm rfactor | rfactorrfactor -> rfactor * | rprimaryrprimary -> a | bb) Does left factoring make the grammar suitable for top-down parsing?No, left recursion is still in the grammar.c) In addition to left factoring, eliminate left recursion from the original grammar.rexpr -> rterm rexpr’rexpr’ -> + rterm rexpr | εrterm -> rfactor rterm’rterm’ -> rfactor rterm | εrfactor -> rprimary rfactor’rfactor’ -> * rfactor’ | εrprimary -> a | bd) Is the resulting grammar suitable for top-down parsing?Yes.Exercise 4.4.1 For each of the following grammars, derive predictive parsers and show the parsing tables. You may left-factor and/or eliminate left-recursion from your grammars first.A predictive parser may be derived by recursive decent or by the table driven approach. Either way you must also show the predictive parse table.a) The grammar of exercise 4.2.2(a).4.2.2 a) S -> 0S1 | 01This grammar has no left recursion. It could possibly benefit from left factoring. Here is the recursive decent PP code.s() {match(‘0’);if (lookahead == ‘0’)s();match(‘1’);}OrLeft factoring the grammar first:S -> 0S’S’ -> S1 | 1s() {match(‘0’); s’();}s’() {if (lookahead == ‘0’)s(); match(‘1’);elsematch(‘1’);}Now we will build the PP tableS -> 0S’S’ -> S1 | 1First(S) = {0}First(S’) = {0, 1}Follow(S) = {1, $}The predictive parsing algorithm on page 227 (fig4.19 and 4.20) can use this table for non-recursive predictive parsing.b) The grammar of exercise 4.2.2(b).4.2.2 b) S -> +SS | *SS | a with string +*aaa.Left factoring does not apply and there is no left recursion to remove.s() {if(lookahead == ‘+’)match(‘+’); s(); s();else if(lookahead == ‘*’)match(‘*’); s(); s();else if(lookahead == ‘a’)match(‘a’);elsereport(“syntax error”);}First(S) = {+, *, a}Follow(S) = {$, +, *, a}The predictive parsing algorithm on page 227 (fig4.19 and 4.20) can use this table for non-recursive predictive parsing.5.1.1 a, b, c: Investigating GraphViz as a solution to presenting trees5.1.2: Extend the SDD of Fig. 5.4 to handle expressions as in Fig. 5.1:1.L -> E N1.L.val = E.syn2. E -> F E'1. E.syn = E'.syn2.E'.inh = F.val3.E' -> + T Esubone'1.Esubone'.inh = E'.inh + T.syn2.E'.syn = Esubone'.syn4.T -> F T'1.T'.inh = F.val2.T.syn = T'.syn5.T' -> * F Tsubone'1.Tsubone'.inh = T'.inh * F.val2.T'.syn = Tsubone'.syn6.T' -> epsilon1.T'.syn = T'.inh7.E' -> epsilon1.E'.syn = E'.inh8. F -> digit1. F.val = digit.lexval9. F -> ( E )1. F.val = E.syn10.E -> T1. E.syn = T.syn5.1.3 a, b, c: Investigating GraphViz as a solution to presenting trees5.2.1: What are all the topological sorts for the dependency graph of Fig. 5.7?1.1, 2, 3, 4, 5, 6, 7, 8, 92.1, 2, 3, 5, 4, 6, 7, 8, 93.1, 2, 4, 3, 5, 6, 7, 8, 94.1, 3, 2, 4, 5, 6, 7, 8, 95.1, 3, 2, 5, 4, 6, 7, 8, 96.1, 3, 5, 2, 4, 6, 7, 8, 97.2, 1, 3, 4, 5, 6, 7, 8, 98.2, 1, 3, 5, 4, 6, 7, 8, 99.2, 1, 4, 3, 5, 6, 7, 8, 910.2, 4, 1, 3, 5, 6, 7, 8, 95.2.2 a, b: Investigating GraphViz as a solution to presenting trees5.2.3: Suppose that we have a production A -> BCD. Each of the four nonterminals A, B, C, and D have two attributes: s is a synthesized attribute, and i is an inherited attribute. For each of the sets of rules below, tell whether (1) the rules are consistent with an S-attributed definition (2) the rules are consistent with an L-attributed definition, and (3) whether the rules are consistent with any evaluation order at all?a) A.s = B.i + C.s1.No--contains inherited attribute2.Yes--"From above or from the left"3.Yes--L-attributed so no cyclesb) A.s = B.i + C.s and D.i = A.i + B.s1.No--contains inherited attributes2.Yes--"From above or from the left"3.Yes--L-attributed so no cyclesc) A.s = B.s + D.s1.Yes--all attributes synthesized2.Yes--all attributes synthesized3.Yes--S- and L-attributed, so no cyclesd)• A.s = D.i• B.i = A.s + C.s• C.i = B.s• D.i = B.i + C.i1.No--contains inherited attributes2.No--B.i uses A.s, which depends on D.i, which depends on B.i (cycle)3.No--Cycle implies no topological sorts (evaluation orders) using the rules5.3.1: Below is a grammar for expressions involving operator + and integer or floating-point operands. Floating-point numbers are distinguished by having a decimal point.1. E -> E + T | T2.T -> num . num | numa) Give an SDD to determine the type of each term T and expression E.1. E -> Esubone + T1. E.type = if (E.type == float || T.type == float) { E.type = float } else{ E.type = integer }2. E -> T1. E.type = T.type3.T -> numsubone . numsubtwo1.T.type = float4.T -> num1.T.type = integerb) Extend your SDD of (a) to translate expressions into postfix notation. Use the binary operator intToFloat to turn an integer into an equivalent float.Note: I use character ',' to separate floating point numbers in the resulting postfix notation. Also, the symbol "||" implies concatenation.1. E -> Esubone + T1. E.val = Esubone.val || ',' || T.val || '+'2. E -> T1. E.val = T.val3.T -> numsubone . numsubtwo1.T.val = numsubone.val || '.' || numsubtwo.val4.T -> num1.T.val = intToFloat(num.val)5.3.2 Give an SDD to translate infix expressions with + and * into equivalent expressions without redundant parenthesis. For example, since both operators associate from the left, and * takes precedence over +, ((a*(b+c))*(d)) translates into a*(b+c)*d. Note: symbol "||" implies concatenation.1.S -> E1. E.iop = nil2.S.equation = E.equation2. E -> Esubone + T1.Esubone.iop = E.iop2.T.iop = E.iop3. E.equation = Esubone.equation || '+' || T.equation4. E.sop = '+'3. E -> T1.T.iop = E.iop2. E.equation = T.equation3. E.sop = T.sop4.T -> Tsubone * F1.Tsubone.iop = '*'2. F.iop = '*'3.T.equation = Tsubone.equation || '*' || F.equation4.T.sop = '*'5.T -> F1. F.iop = T.iop2.T.equation = F.equation3.T.sop = F.sop6. F -> char1. F.equation = char.lexval2. F.sop = nil7. F -> ( E )1.if (F.iop == '*' && E.sop == '+') { F.equation = '(' || E.equation || ')' }else { F.equation = E.equation }2. F.sop = nil5.3.3: Give an SDD to differentiate expressions such as x * (3*x + x * x) involving the operators + and *, the variable x, and constants. Assume that no simplification occurs, so that, for example, 3*x will be translated into 3*1 + 0*x. Note: symbol "||" implies concatenation. Also, differentiation(x*y) = (x * differentiation(y) + differentiation(x) * y) and differentiation(x+y) = differentiation(x) + differentiation(y).1.S -> E1.S.d = E.d2. E -> T1. E.d = T.d2. E.val = T.val3.T -> F1.T.d = F.d2.T.val = F.val4.T -> Tsubone * F1.T.d = '(' || Tsubone.val || ") * (" || F.d || ") + (" || Tsubone.d || ") * (" ||F.val || ')'2.T.val = Tsubone.val || '*' || F.val5. E -> Esubone + T1. E.d = '(' || Esubone.d || ") + (" || T.d || ')'2. E.val = Esubone.val || '+' || T.val6. F -> ( E )1. F.d = E.d2. F.val = '(' || E.val || ')'7. F -> char1. F.d = 12. F.val = char.lexval8. F -> constant1. F.d = 02. F.val = constant.lexval。

龙书第二章部分答案

龙书第二章部分答案

第二章2.2 Exercises for Section 2.22.2.1Consider the context-free grammar:S -> S S + | S S * | a1Show how the string aa+a* can be generated by this grammar.2Construct a parse tree for this string.3What language does this grammar generate? Justify your answer.answer4S -> S S * -> S S + S * -> a S + S * -> a a + S * -> a a + a *56L = {Postfix expression consisting of digits, plus and multiple signs}2.2.2What language is generated by the following grammars? In each case justify your answer.7S -> 0 S 1 | 0 18S -> + S S | - S S | a9S -> S ( S ) S | ε10S -> a S b S | b S a S | ε11⧗ S -> a | S + S | S S | S * | ( S )answer12L = {0n1n | n>=1}13L = {Prefix expression consisting of plus and minus signs}14L = {Matched brackets of arbitrary arrangement and nesting, includes ε} 15L = {String has the same amount of a and b, includes ε}16?2.2.3Which of the grammars in Exercise 2.2.2 are ambiguousanswer17No18NoYesYesYes2.2.4Construct unambiguous context-free grammars for each of the following languages. In each case show that your grammar is correct.19Arithmetic expressions in postfix notation.20Left-associative lists of identifiers separated by commas.21Right-associative lists of identifiers separated by commas.22Arithmetic expressions of integers and identifiers with the four binary operators +, - , *, /.answer1. E -> E E op | num2. list -> list , id | id3. list -> id , list | id4. expr -> expr + term | expr - term | termterm -> term * factor | term / factor | factorfactor -> id | num | (expr)5. expr -> expr + term | expr - term | termterm -> term * unary | term / unary | unaryunary -> + factor | - factorfactor - > id | num | (expr)2.2.5Show that all binary strings generated by the following grammar have values divisible by 3. Hint. Use induction on the number of nodes in a parse tree.num -> 11 | 1001 | num 0 | num numDoes the grammar generate all binary strings with values divisible by 3?answerproveany string derived from the grammar can be considered to be a sequence consisting of 11, 1001 and 0, and not prefixed with 0.the sum of this string is:sum= Σn (21 + 20) * 2 n + Σm (23 + 20) * 2m= Σn 3 * 2 n + Σm 9 * 2mIt is obviously can divisible by 3.No. Consider string "10101", it is divisible by 3, but cannot derived from the grammar. Question: any general prove?2.2.6Construct a context-free grammar for roman numerals.Note: we just consider a subset of roman numerals which is less than 4k.answerwikipedia: Roman_numeralsvia wikipedia, we can categorize the single noman numerals into 4 groups:I, II, III | I V | V, V I, V II, V III | I Xthen get the production:digit -> smallDigit | I V | V smallDigit | I XsmallDigit -> I | II | III | εand we can find a simple way to map roman to arabic numerals. For example: o XII => X, II => 10 + 2 => 12o CXCIX => C, XC, IX => 100 + 90 + 9 => 199o MDCCCLXXX => M, DCCC, LXXX => 1000 + 800 + 80 => 1880 via the upper two rules, we can derive the production:romanNum -> thousand hundred ten digitthousand -> M | MM | MMM | εhundred -> smallHundred | C D | D smallHundred | C MsmallHundred -> C | CC | CCC | εten -> smallTen | X L | L smallTen | X CsmallTen -> X | XX | XXX | εdigit -> smallDigit | I V | V smallDigit | I XsmallDigit -> I | II | III | ε2.3 Exercises for Section 2.32.3.1Construct a syntax-directed translation scheme that trans­lates arithmetic expressions from infix notation into prefix notation in which an operator appears before its operands; e.g. , -xy is the prefix notation for x - y . Give annotated parse trees for the inputs 9-5+2 and 9-5*2.。

编译原理部分课后答案,仅供参考

编译原理部分课后答案,仅供参考

第一章编译程序概述1.1什么是编译程序编译程序是现代计算机系统的基本组成部分之一,而且多 数计算机系统都含有不止一个高级语言的编译程序。

对有些高 级语言甚至配置了几个不同性能的编译程序。

1.2编译过程概述和编译程序的结构编译程序完成从源程序到目标程序的翻译工作,是一个复 杂的整体的过程。

从概念上来讲,一个编译程序的整个工作过 程是划分成阶段进行的,每个阶段将源程序的一种表示形式转 换成另一种表示形式,各个阶段进行的操作在逻辑上是紧密连 接在一起的。

一般一个编译过程划分成词法分析、语法分析、 语义分析、中间代码生成,代码优化和目标代码生成六个阶段,这是一种典型的划分方法。

事实上,某些阶段可能组合在一起, 这些阶段间的源程序的中间表示形式就没必要构造岀来了。

我 们将分别介绍各阶段的任务。

另外两个重要的工作:表格管理 和岀错处理与上述六个阶段都有联系。

编译过程中源程序的各 种信息被保留在种种不同的表格里,编译各阶段的工作都涉及 到构造、查找或更新有关的表格,因此需要有表格管理的工作; 如果编译过程中发现源程序有错误,编译程序应报告错误的性 质和错误发生的地点,并且将错误所造成的影响限制在尽可能 小的范围内,使得源程序的其余部分能继续被编译下去,有些 编译程序还能自动校正错误, 这些工作称之为岀错处理。

图1.3表示了编译的各个阶段。

图1.3编译的各个阶段它不生成目标代码,它每遇到一个语句,就要对这个语句进行 分析以决定语句的含义,执行相应的动作。

右面的图示意了它 的工作机理第二章:PL/O 编译程序问答第1题 PL/0语言允许过程嵌套定义和递归调用,试问 它的编译程序如何解决运行时的存储管理。

答:PL/0语言允许过程嵌套定义和递归调用,它的编译程序在运行时采用了栈式动态存储管理。

(数组CODE 存放的只读目 标程序,它在运行时不改变。

)运行时的数据区S 是由解释程序 定义的一维整型数组,解释执行时对数据空间S 的管理遵循后进先岀规则,当每个过程(包括主程序)被调用时,才分配数据 空间,退出过程时,则所分配的数据空间被释放。

编译原理课后习题答案

编译原理课后习题答案

编译原理课后习题答案第三章N=>D=> {0,1,2,3,4,5,6,7,8,9}N=>ND=>NDDL={a |a(0|1|3..|9)n且 n>=1}(0|1|3..|9)n且 n>=1{ab,}a nb n n>=1第6题.(1) <表达式> => <项> => <因子> => i(2) <表达式> => <项> => <因子> => (<表达式>) => (<项>)=> (<因子>)=>(i)(3) <表达式> => <项> => <项>*<因子> => <因子>*<因子> =i*i(4) <表达式> => <表达式> + <项> => <项>+<项> => <项>*<因子>+<项>=> <因子>*<因子>+<项> => <因子>*<因子>+<因子> = i*i+i (5) <表达式> => <表达式>+<项>=><项>+<项> => <因子>+<项>=i+<项> => i+<因子> => i+(<表达式>) => i+(<表达式>+<项>)=> i+(<因子>+<因子>)=> i+(i+i)(6) <表达式> => <表达式>+<项> => <项>+<项> => <因子>+<项> => i+<项> => i+<项>*<因子> => i+<因子>*<因子> = i+i*i第7题第9题语法树ss s* s s+aa a推导: S=>SS*=>SS+S*=>aa+a* 11. 推导:E=>E+T=>E+T*F语法树:E*短语: T*F E+T*F直接短语: T*F句柄: T*F12.短语:直接短语:句柄:13.(1)最左推导:S => ABS => aBS =>aSBBS => aBBS => abBS => abbS => abbAa => abbaa最右推导:S => ABS => ABAa => ABaa => ASBBaa => ASBbaa => ASbbaa => Abbaa => a1b1b2a2a3 (2) 文法:S ABSS Aa S ε A aB b(3) 短语:a1 , b1 , b2, a2 , , bb , aa , abbaa,直接短语: a1 , b1 , b2, a2 , , 句柄:a114 (1)S ABA aAb | εB aBb | ε (2)S 1S0 S AA 0A1 |ε第四章1. 1. 构造下列正规式相应的DFA (1) 1(0|1)*101NFA123114(2) 1(1010*|1(010)*1)*0 NFA(3)NFA(4)NFA2.解:构造DFA 矩阵表示b其中0 表示初态,*表示终态用0,1,2,3,4,5分别代替{X} {Z} {X,Z} {Y} {X,Y} {X,Y,Z}得DFA状态图为:3.解:构造DFA矩阵表示构造DFA的矩阵表示其中表示初态,*表示终态替换后的矩阵4.(1)解构造状态转换矩阵:{2,3} {0,1}{2,3}a={0,3} {2},{3},{0,1}{0,1}a={1,1} {0,1}b={2,2}(2)解:首先把M 的状态分为两组:终态组{0},和非终态组{1,2,3,4,5} 此时G=( {0},{1,2,3,4,5} ) {1,2,3,4,5}a ={1,3,0,5} {1,2,3,4,5}b ={4,3,2,5}由于{4}a ={0} {1,2,3,5}a ={1,3,5}因此应将{1,2,3,4,5}划分为{4},{1,2,3,5} G=({0}{4}{1,2,3,5}) {1,2,3,5}a ={1,3,5} {1,2,3,5}b ={4,3,2}因为{1,5}b ={4} {23}b ={2,3}所以应将{1,2,3,5}划分为{1,5}{2,3} G=({0}{1,5}{2,3}{4}){1,5}a ={1,5} {1,5}b ={4} 所以{1,5} 不用再划分{2,3}a ={1,3} {2,3}b ={3,2}因为 {2}a ={1} {3}a ={3} 所以{2,3}应划分为{2}{3} 所以化简后为G =( {0},{2},{3},{4},{1,5})7.去除多余产生式后,构造NFA 如下确定化,构造DFA 矩阵G={(0,1,3,4,6),(2,5)} {0,1,3,4,6}a={1,3}{0,1,3,4,6}b={2,3,4,5,6}所以将{0,1,3,4,6}划分为 {0,4,6}{1,3} G={(0,4,6),(1,3),(2,5)}{0,4,6}b={3,6,4} 所以划分为{0},{4,6} G={(0),(4,6),(1,3),(2,5)}不能再划分,分别用0,4,1,2代表各状态,构造DFA 状态转换图如下;b8.代入得S = 0(1S|1)| 1(0S|0) = 01(S|ε) | 10(S|ε) = (01|10)(S|ε) = (01|10)S | (01|10)= (01|10)*(01|10)构造NFA由NFA可得正规式为(01|10)*(01|10)=(01|10)+9.状态转换函数不是全函数,增加死状态8,G={(1,2,3,4,5,8),(6,7)}(1,2,3,4,5,8)a=(3,4,8) (3,4)应分出(1,2,3,4,5,8)b=(2,6,7,8)(1,2,3,4,5,8)c=(3,8)(1,2,3,4,5,8)d=(3,8)所以应将(1,2,3,4,5,8)分为(1,2,5,8), (3,4)G={(1,2,5,8),(3,4),(6,7)}(1,2,5,8)a=(3,4,8) 8应分出(1,2,5,8)b=(2,8)(1,2,5,8)c=(8)(1,2,5,8)d=(8)G={(1,2,5),(8),(3,4),(6,7)}(1,2,5)a=(3,4,8) 5应分出G={(1,2), (3,4),5, (6,7) ,(8) }去掉死状态8,最终结果为 (1,2) (3,4) 5,(6,7) 以1,3,5,6代替,最简DFA为b正规式:b*a(da|c)*bb*第五章1.S->a | ^ |( T )T -> T , S | S(a,(a,a))S => ( T ) => ( T , S ) => ( S , S ) => ( a , S) => ( a, ( T )) =>(a , ( T , S ) ) => (a , ( S , S )) => (a , ( a , a ) )S=>(T) => (T,S) => (S,S) => ( ( T ) , S ) => ( ( T , S ) , S ) => ( ( T , S , S ) , S ) => ( ( S , S , S ) , S )=> ( ( ( T ) , S , S ) , S ) => ( ( ( T , S ) , S , S ) , S ) =>( ( ( S , S ) ,S , S ) , S ) => ( ( ( a , S ) , S , S ) , S )=> ( ( ( a , a ) , S , S ) , S ) => ( ( ( a , a ) , ^ , S ) , S ) => ( ( ( a , a ) , ^ , ( T ) ) , S )=> ( ( ( a , a ) , ^ , ( S ) ) , S ) => ( ( ( a , a ) , ^ , ( a ) ) , S ) => ( ( ( a , a ) , ^ , ( a ) ) , a )S->a | ^ |( T )T -> T , ST -> S消除直接左递归:S->a | ^ |( T )T -> S T’T’ -> , S T’ | ξSELECT ( S->a) = {a}SELECT ( S->^) = {^}SELECT ( S->( T ) ) = { ( }SELECT ( T -> S T’) = { a , ^ , ( }SELECT ( T’ -> , S T’ ) = { , }SELECT ( T’ ->ξ) = FOLLOW ( T’ ) = FOLLOW ( T ) = { ) } 构造预测分析表分析符号串( a , a )#分析栈剩余输入串所用产生式#S ( a , a) # S -> ( T )# ) T ( ( a , a) # ( 匹配# ) T a , a ) # T -> S T’# ) T’ S a , a ) # S -> a# ) T’ a a , a ) # a 匹配# ) T’,a) # T’ -> , S T’# ) T’ S , , a ) # , 匹配# ) T’ S a ) # S->a# ) T’ a a ) # a匹配# ) T’) # T’ ->ξ# ) ) # )匹配# # 接受2.E->TE’ E’->+E E’->ξ T->FT’ T’->T T’->ξ F->PF’ F’->*F’ F’->ξP->(E) P->a P->b P->∧SELECT(E’->+E)={+}SELECT(E’->ε)=FOLLOW(E’)= {#,)}SELECT(T->FT’)=FIRST(F)= {(,a,b,^}SELECT(T’ —>T)=FIRST(T)= {(,a,b,^)SELECT(T’->ε)=FOLLOW(T’)= {+,#,)}SELECT(F ->P F’)=FIRST(F)= {(,a,b,^}SELECT(F’->*F’)={*}SELECT(F’->ε)=FOLLOW(F’)= {(,a,b,^,+,#,)}3. S->MH S->a H->Lso H->ξ K->dML K->ξ L->eHf M->K M->bLMFIRST ( S ) =FIRST(MH)= FIRST ( M ) ∪ FIRST ( H ) ∪ {ξ} ∪ {a}= {a, d , b , e ,ξ}FIRST( H ) = FIRST ( L ) ∪ {ξ}= { e , ξ}FIRST( K ) = { d , ξ}FIRST( M ) = FIRST ( K ) ∪ { b } = { d , b ,ξ}FOLLOW ( S ) = { # , o }FOLLOW ( H ) = FOLLOW ( S ) ∪ { f } = { f , # , o }FOLLOW ( K ) = FOLLOW ( M ) = { e , # , o }FOLLOW ( L ) ={ FIRST ( S ) –{ξ} } ∪{o} ∪ FOLLOW ( K )∪ { FIRST ( M ) –{ξ} } ∪ FOLLOW ( M )= {a, d , b , e , # , o }FOLLOW ( M ) ={ FIRST ( H ) –{ξ} } ∪ FOLLOW ( S )∪{ FIRST ( L ) –{ξ} } = { e , # , o }SELECT ( S-> M H) = ( FIRST ( M H) –{ξ} ) ∪ FOLLOW ( S )= ( FIRST( M ) ∪ FIRST ( H ) –{ξ} ) ∪ FOLLOW ( S )= { d , b , e , # , o }SELECT ( S-> a ) = { a }SELECT ( H->L S o ) = FIRST(L S o) = { e }SELECT ( H ->ξ ) = FOLLOW ( H ) = { f , # , o }SELECT ( K-> d M L ) = { d }SELECT ( K->ξ ) = FOLLOW ( K ) = { e , # , o }SELECT ( L-> e H f ) = { e }SELECT ( M->K ) = ( FIRST( K ) –{ξ} ) ∪ FOLLOW ( M ) = {d,e , # , o }SELECT ( M -> b L M )= { b }构造LL( 1 ) 分析表4 . 文法含有左公因式,变为S->C $ { b, a }C-> b A { b }C-> a B { a }A -> b A A { b }A-> a A’ { a }A’-> ξ { $ , a, b }A’-> C { a , b }B->a B B { a }B -> b B’ { b }B’->ξ { $ , a , b }B’-> C { a, b }5. <程序> --- S <语句表>――A <语句>――B <无条件语句>――C <条件语句>――D <如果语句>――E<如果子句> --FS->begin A end S->begin A end { begin }A-> B A-> B A’ { a , if }A-> A ; B A’-> ; B A’ { ; }A’->ξ { end }B-> C B-> C { a }B-> D B-> D { if }C-> a C-> a { a }D-> E D-> E D’ { if }D-> E else B D’-> else B { else }D’->ξ {; , end }E-> FC E-> FC { if }F-> if b then F-> if b then { if }非终结符是否为空S-否 A-否A’-是 B-否 C-否 D-否D’-是 E-否 F-否FIRST(S) = { begin }FIRST(A) = FIRST(B) ∪ FIRST(A’) ∪ {ξ} = {a , if , ; , ξ}FIRST(A’) ={ ; , ξ}FIRST(B) = FIRST(C) ∪ FIRST(D) ={ a , if }FIRST(C) = {a}FIRST(D) = FIRST(E)= { if }FIRSR(D’) = {else , ξ}FIRST(E) = FIRST(F) = { if }FIRST(F) = { if }FOLLOW(S) = {# }FOLLOW(A) = {end}FOLLOW(A’) = { end }FOLLOW(B) = {; , end }FOLLOW (C) = {; , end , else }FOLLOW(D) = {; , end }FOLLOW( D’ ) = { ; , end }FOLLOW(E) = { else , ; end }FOLLOW(F) = { a }S A A’ B C D D’ E F if then else begin end a b ;6. 1.(1) S -> A | B(2) A -> aA|a(3)B -> bB |b提取(2),(3)左公因子(1) S -> A | B(2) A -> aA’(3) A’-> A|ξ(4) B -> bB’2.(1) S->AB(2) A->Ba|ξ(3) B->Db|D(4) D-> d|ξ提取(3)左公因子(1) S->AB(2) A->Ba|ξ(3) B->DB’(4) B’->b|ξ(5) D-> d|ξ3.(1) S->aAaB | bAbB(2) A-> S| db(3) B->bB|a4(1)S->i|(E)(2)E->E+S|E-S|S提取(2)左公因子(1)S->i|(E)(2)E->SE’(3)E’->+SE’|-SE’ |ξ5(1)S->SaA | bB(2)A->aB|c(3)B->Bb|d消除(1)(3)直接左递归(1)S->bBS’(2)S’->aAS’|ξ(4) B -> dB’(5)B’->bB’|ξ6.(1) M->MaH | H(2) H->b(M) | (M) |b消除(1)直接左递归,提取(2)左公因子(1)M-> HM’(2)M’-> aHM’ |ξ(3)H->bH’ | ( M )(4)H’->(M) |ξ7. (1)1)A->baB2)A->ξ3)B->Abb4)B->a将1)、2)式代入3)式1)A->baB2)A->ξ3)B->baBbb4)B->bb5)B->a提取3)、4)式左公因子1)A->baB2)A->ξ3)B->bB’4)B’->aBbb | b5)B->a(3)1)S->Aa3)A->SB4)B->ab将3)式代入1)式1)S->SBa2)S->b3)A->SB4)B->ab消除1)式直接左递归1)S->bS’2)S’->BaS’ |ξ3)S->b4)A->SB5)B->ab删除多余产生式4)1)S->bS’2)S’->BaS’ |ξ3)S->b4)B->ab(5)1)S->Ab2)S->Ba3)A->aA4)A->a5)B->a提取3) 4)左公因子1)S->Ab2)S->Ba3)A->aA’4)A’-> A |ξ将3)代入1) 5)代入21)S->aA’b2)S->aa3)A->aA’4)A’-> A |ξ5)B->a提取1) 2)左公因子1)S-> aS’2)S’->A’b | a3)A->aA’4)A’-> A |ξ5)B->a删除多余产生式5)1)S-> aS’2)S’->A’b | a3)A->aA’4)A’-> A |ξA A’ S’ S将3)代入4)1)S-> aS’2)S’->A’b | a3)A->aA ’4)A’-> aA’ |ξ将4)代入2)1)S-> aS’2)S’->aA’b3)S’->a4)S’->b5)A->aA ’6)A’-> aA’ |ξ对2)3)提取左公因子1)S->aS’2)S’->aS’’3)S’’->A’b|ξ4)S’->b5)A->aA ’6)A’-> aA’ |ξ删除多余产生式5)1)S->aS’2)S’->aS’’3)S’’->A’b|ξ4)S’->b5)A’-> aA’ |ξ第六章1S a | ∧ | ( T )T T , S | S解:(1) 增加辅助产生式S’#S#求 FIRSTVT集FIRSTVT(S’)= {#}FIRSTVT(S)={a ∧ ( }={ a ∧ ( } FIRSTVT (T) ={,} ∪ FIRSTVT( S ) = { , a ∧ ( }求 LASTVT集LASTVT(S’)= { # }LASTVT(S)={ a ∧ )}LASTVT (T) ={ , a ∧ )}(2)算符优先关系表a∧(),# a·>·>·>∧·>·>·> (<·<·<·=·<·)·>·>·>,<·<·<··>·>#<·<·<·=·因为任意两终结符之间至多只有一种优先关系成立,所以是算符优先文法(3)a ∧( ) , #F 1 1 1 1 1 1g 1 1 1 1 1 1f 2 2 1 3 2 1g 2 2 2 1 2 1f 3 3 1 3 3 1g 4 4 4 1 2 1f 3 3 1 3 3 1g 4 4 4 1 2 1(4)栈优先关系当前符号剩余输入串移进或规约#<· ( a,a)# 移进#( <· a ,a)# 移进# (a ·> , a)# 规约#(T <· , a)# 移进#(T,<· a )# 移进#(T,a ·> ) # 规约#(T,T ·> ) # 规约#(T =· ) # 移进#(T) ·> #规约#T =·#接受4.扩展后的文法S’#S# S S;G S G G G(T) G H H a H(S)T T+S T S(1)FIRSTVT(S)={;}∪FIRST VT(G) = {; , a , ( }FIRSTVT(G)={ ( }∪FIRSTVT(H) = {a , ( }FIRSTCT(H)={a , ( }FIRSTVT(T) = {+} ∪FIRSTVT(S) = {+ , ; , a , ( }LASTVT(S) = {;} ∪LASTVT(G) = { ; , a , )}LASTVT(G) = { )} ∪ LASTVT(H) = { a , )}LASTVT(H) = {a, )}LASTVT(T) = {+ } ∪LASTVT(S) = {+ , ; , a , ) };()a+#;·><··><··>·> (<·<·=·<·<·)·>·>·>·>·> a·>·>·>·>·> +<·<··><··>#<·<·<·=·因为任意两终结符之间至多只有一种优先关系成立,所以是算符优先文法(2)句型a(T+S);H;(S)的短语有:a(T+S);H;(S) a(T+S);H a(T+S) a T+S (S) H直接短语有: a T+S H (S)句柄: a素短语:a T+S (S)最左素短语:a(3)分析a;(a+a)(4)不能用最右推导推导出上面的两个句子。

编译原理课后答案

编译原理课后答案

第二章 高级语言及其语法描述4.令+、*和↑代表加,乘和乘幂,按如下的非标准优先级和结合性质的约定,计算1+1*2↑2*1↑2的值:(1) 优先顺序(从高至低)为+,*和↑,同级优先采用左结合。

(2) 优先顺序为↑,+,*,同级优先采用右结合。

解:(1)1+1*2↑2*1↑2=2*2↑1*1↑2=4↑1↑2=4↑2=16 (2)1+1*2↑2*1↑2=1+1*2*1=2*2*1=2*2=46.令文法G6为 N →D|NDD →0|1|2|3|4|5|6|7|8|9 (1) G6 的语言L (G6)是什么?(2) 给出句子0127、34和568的最左推导和最右推导。

解:(1)L (G6)={a|a ∈∑+,∑=﹛0,1,2,3,4,5,6,7,8,9}}(2)N =>ND => NDD => NDDD => DDDD => 0DDD => 01DD => 012D => 0127 N => ND => N7=> ND7=> N27=> ND27=> N127=> D127=> 0127 N => ND => DD => 3D => 34 N => ND => N4=> D4 =>34N => ND => NDD => DDD => 5DD => 56D => 568 N => ND => N8=> ND8=> N68=> D68=> 5687.写一个文法,使其语言是奇数集,且每个奇数不以0开头。

解:A →SN, S →+|-|∑, N →D|MDD →1|3|5|7|9, M →MB|1|2|3|4|5|6|7|8|9 B →0|1|2|3|4|5|6|7|8|9 8. 文法:E T E T E T TF T F T F F E i→+-→→|||*|/()| 最左推导:E E T T TF T i T i T F i F F i i F i i i E T T F F F 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 TF E T i E F i E i i T i i F i i i i i E T F T F F F E F E T F E F F E i F T i F F i F i i i i i ⇒+⇒+⇒+⇒+⇒+⇒+⇒+⇒+⇒⇒⇒⇒⇒+⇒+⇒+⇒+⇒+⇒+⇒+**********()*()*()*()*()*()*()*()语法树:/********************************EE FTE +T F F T +iiiEEFTE-T F F T -iiiE EFT+T F FTiii*i+i+ii-i-ii+i*i*****************/9.证明下面的文法是二义的:S → iSeS|iS|I解:因为iiiiei 有两种最左推导,所以此文法是二义的。

编译原理英文版第四章答案

编译原理英文版第四章答案

Chapter 4 Answers - Compiler Principles Exercise 1Question:Consider the following context-free grammar (CFG):S → a | BB → a1.Is the language generated by this CFG ambiguous? Justify your answer.2.If the CFG is ambiguous, provide a string that can be generated in twodifferent ways.Answer:1.The language generated by this CFG is not ambiguous. In order toprove this, we need to show that for every valid string in the language, there isa unique deriv ation tree. Let’s analyze the grammar to determine thisuniqueness:•The production rule S → a is a direct derivation from the start symbol S, resulting in the terminal symbol a. Since this is the only possible derivation from S, it is unambiguous.•The production rule S → B has B as its right-hand side. The production rule B → a is the only production rule for B, so B will eventually derive theterminal a. Therefore, the derivation from S to the single terminal a is unique.Hence, we can conclude that the language generated by this CFG is not ambiguous because there is a unique derivation for every valid string.2.Since the language is not ambiguous, we cannot provide a string thatcan be generated in two different ways, as every valid string has only onederivation.Exercise 2Question:Consider the following context-free grammar (CFG):E → E + E | E * E | id1.Starting with the non-terminal symbol E, apply the productions of thegrammar to generate the string id + id * id.2.Draw a parse tree for the string id + id * id using the abovegrammar.Answer:1.To generate the string id + id * id, we can apply the productions ofthe grammar as follows:E (Starting with E)E + E (Using E → E + E)E + E * E (Using E → E * E)id + id * id (Using E → id)2.The parse tree for the string id + id * id is as follows:E_____|_____| | |E + E| |E E| |id __|__| |E *| |E id|idExercise 3Question:Provide a context-free grammar (CFG) for the language of palindromes over the alphabet {a, b}.Answer:We can define a context-free grammar (CFG) to generate palindromes over the alphabet {a, b} as follows:S → ε | a | b | aSa | bSbExplanation: - The production rule S → ε generates an empty string (epsilon). - The production rules S → a and S → b generate single characters a and b respectively. - The production rules S → aSa and S → bSb generate palindromes. Here, the recursive non-terminal S is enclosed between the same terminal symbols a or b.This CFG covers all possible palindromes over the alphabet {a, b}, including empty strings, single characters, and palindromic strings of various lengths.Exercise 4Question:Consider the following context-free grammar (CFG):S → AaBA → ε | aAB → ε | bB1.What is the language generated by this CFG?2.Determine if the CFG is ambiguous. If so, provide a string that can begenerated in two different ways.Answer:1.The language generated by this CFG consists of strings with a prefix ofa and a suffix of b. The generated language can be represented as:L = {anbm | n ≥ 0, m ≥ 0}Where an represents zero or more a and bn represents zero or more b.2.The CFG is ambiguous. Here is an example of a string that can begenerated in two different ways: aab.Possible derivations:•Derivation 1:S (Start with S)AaB (Using S → AaB)aaABb (Using A → aA)aaBb (Using A → ε)aab (Using B → bB)•Derivation 2:S (Start with S)AaB (Using S → AaB)AaaBBb (Using A → aA)aaBBb (Using A → ε)aaBb (Using B → ε)aab (Using B → bB)Thus, the string aab can be derived in two different ways, making the grammar ambiguous.Exercise 5Question:Consider the following context-free grammar (CFG):E → E + D | DD → D * F | FF → idStarting with the non-terminal symbol E, apply the productions of the grammar to generate the string id + id * id.Answer:To generate the string id + id * id, we can apply the productions of the grammar as follows:E (Starting with E)E + D (Using E → E + D)D + D (UsingE → D)D + D * F (Using D → D * F)F + D * F (Using D → F)id + D * F (Using F → id)id + D * id (Using F → id)id + D * id * F (Using D → F)id + id * F (Using F → id)id + id * id (Using F → id)Hence, the string id + id * id can be generated by applying the productions of the given grammar starting with the non-terminal symbol E.。

编译原理教程课后习题答案

编译原理教程课后习题答案

编译原理教程课后习题答案【篇一:编译原理教程课后习题答案——第一章】完成下列选择题:(1) 构造编译程序应掌握a. 源程序b. 目标语言c. 编译方法d. 以上三项都是(2) 编译程序绝大多数时间花在上。

a. 出错处理b. 词法分析c. 目标代码生成d. 表格管理(3) 编译程序是对。

a. 汇编程序的翻译b. 高级语言程序的解释执行c. 机器语言的执行d. 高级语言的翻译【解答】(1) d (2) d(3) d1.2 计算机执行用高级语言编写的程序有哪些途径?它们之间的主要区别是什么?【解答】计算机执行用高级语言编写的程序主要有两种途径:解释和编译。

在解释方式下,翻译程序事先并不采用将高级语言程序全部翻译成机器代码程序,然后执行这个机器代码程序的方法,而是每读入一条源程序的语句,就将其解释(翻译)成对应其功能的机器代码语句串并执行,而所翻译的机器代码语句串在该语句执行后并不保留,最后再读入下一条源程序语句,并解释执行。

这种方法是按源程序中语句的动态执行顺序逐句解释(翻译)执行的,如果一语句处于一循环体中,则每次循环执行到该语句时,都要将其翻译成机器代码后再执行。

在编译方式下,高级语言程序的执行是分两步进行的:第一步首先将高级语言程序全部翻译成机器代码程序,第二步才是执行这个机器代码程序。

因此,编译对源程序的处理是先翻译,后执行。

从执行速度上看,编译型的高级语言比解释型的高级语言要快,但解释方式下的人机界面比编译型好,便于程序调试。

这两种途径的主要区别在于:解释方式下不生成目标代码程序,而编译方式下生成目标代码程序。

1.3 请画出编译程序的总框图。

如果你是一个编译程序的总设计师,设计编译程序时应当考虑哪些问题?【解答】编译程序总框图如图1-1所示。

作为一个编译程序的总设计师,首先要深刻理解被编译的源语言其语法及语义;其次,要充分掌握目标指令的功能及特点,如果目标语言是机器指令,还要搞清楚机器的硬件结构以及操作系统的功能;第三,对编译的方法及使用的软件工具也必须准确化。

编译原理龙书答案

编译原理龙书答案

第八、九章习题解答参考Aho:《编译原理技术与工具》书中习题(Aho)8.3、9.6 将下面C程序,翻译为三地址码,画出流图,翻译为汇编代码main(){int i;int a[10];i = 1;while (i <= 10) {a[i] = 0; i = i + 1;}}解:三地址码为i := 1L1: if i <= 10 goto L2goto LnextL2: t1 := ct2 := 4 * it1[t2] := 0t1 := i + 1i := t1goto L1Lnext:汇编码为采取了9.7节寄存器优化算法并已进行窥孔优化:MOV 1, R0L1:CMP R0, 10J<= L2GOTO LnextL2: MOV 4, R1MUL R0, R1MOV 0, a(R1)MOV i, R1ADD 1, R1MOV R1, R0GOTO L1Lnext: 文案编辑词条B 添加义项?文案,原指放书的桌子,后来指在桌子上写字的人。

现在指的是公司或企业中从事文字工作的职位,就是以文字来表现已经制定的创意策略。

文案它不同于设计师用画面或其他手段的表现手法,它是一个与广告创意先后相继的表现的过程、发展的过程、深化的过程,多存在于广告公司,企业宣传,新闻策划等。

基本信息中文名称文案外文名称Copy目录1发展历程2主要工作3分类构成4基本要求5工作范围6文案写法7实际应用折叠编辑本段发展历程汉字"文案"(wén àn)是指古代官衙中掌管档案、负责起草文书的幕友,亦指官署中的公文、书信等;在现代,文案的称呼主要用在商业领域,其意义与中国古代所说的文案是有区别的。

在中国古代,文案亦作" 文按"。

公文案卷。

《北堂书钞》卷六八引《汉杂事》:"先是公府掾多不视事,但以文案为务。

"《晋书·桓温传》:"机务不可停废,常行文按宜为限日。

编译原理(龙书)习题答案(chap2,3)

编译原理(龙书)习题答案(chap2,3)

DFA的状态图:
4) (a | b) * abb(a | b) * 根据算法3.25得到NFA:
DFA的转换表:
状态 -A{0} B{0,1} C{0,2} +D{0,3} +E{0,1,3} +F{0,2,3} a B B B E E E b A C D D F D
DFA的状态图:
括号的匹配,包括空串
4)
S a S b S |b S a S |
由相同数目的a和b组成பைடு நூலகம்字符串的集合,或者空串 5)
S a | S S | S S | S | ( S )
以a为变量,包括+,连接,*和括号四种运算的表 达式的集合
2.2.3 练习2.2.2中哪些文法具有二义性? 3) 4) 5)具有二义性。 以5)为例进行说明: 给定字符串 a+a+a ,对应着两棵分析树:
包含3个b的a,b字符串的集合
5) (aa | bb) * ((ab | ba)(aa | bb) * (ab | ba)(aa | bb)*)* 包含偶数个a和偶数个b的a,b字符串的集合
3.2.5 试写出下列语言的正则定义: 1)包含5个元音的所有小写字母串,这些串中的元音按 顺序出现。 X:除五个元音外的所有小写字母的集合。
8)所有由a和b组成且不含子串abb的串。
b * (a | ab) *
9)所有由a和b组成且不含子序列abb的串。
b * a * (b | )a * b * a * (a | b)a* |
X:[^*"] Y:[^/] Z:[^"]
3.6.1 将下图中的NFA转换为DFA。
DFA的转换表:
第二章 一个简单的语法制导翻译器

编译原理课后习题解答(2)

编译原理课后习题解答(2)

2/7
西北大学 Gong Xq
龙书本科教学版习题解答
仅供教学参考
解答:文法 3) 、4) 、5)有二义性。 证明:3)对文法的句子()(),存在两棵不同的语法分析树如下:
S S
S
(
S ε
)
S ε
S ε
( ε
S
)
S
S ε
(
S ε
)
S ε
S ε
(
S ε
)
S ε
所以文法是二义的。 4)对文法的句子 abab,存在两棵不同的语法分析树如下:
5/7
西北大学 Gong Xq
龙书本科教学版习题解答
仅供教学参考
2.3 节 语法制导翻译
产生式
翻译方案 { E.pre = '+' || E1.pre || T.pre } { E.pre = '-' || E1.pre || T.pre } { E.pre = T.pre } { T.pre = '*' || T1.pre || F.pre } { T.pre = '/' || T1.pre || F.pre } { T.pre = F.pre } {F.pre = id.lexeme} {F.pre = num.value} {F.pre = E.pre} E pre =-9* 52
1)证明:对语法分析树的结点数目使用数学归纳法。 ①归纳基础:当语法分析树有两个结点时,形如
num 11
num 1001
生成的串分别为 11 和 1001,表示的值为 3 和 9,能被 3 整除。 ②归纳步骤: 假设语法分析树的结点数目少于 n 时生成的二进制串的值能被 3 整除, 那么当 结点数目等于 n 时,语法分析树的根有下面两种可能的形式:

编译原理教程-课后习题答案第三章语法分析

编译原理教程-课后习题答案第三章语法分析

第三章 语法分析 【解答】 (1) 消除左递归后,文法G[D′]如下:
D→TL T→int|long|short L→idL
第三章 语法分析
D
D
TL
TL
int a L′
int L , c
, b L′
L,b
, c L′
a (a)
(b)
图3-6 两种文法为int a,b,c构造的分析树 (a) 文法G(D); (b) 文法G′(D)
第三章 语法分析 表3-1 预测分析表
A A′ B B′
{ S ( ); T′( ); } void T′ ( ) { if ( lookahead==′, ′) { match (′, ′); S ( ); T′ ( ); } }
第三章 语法分析
3.10 已知文法G[A]: A→aABl|a B→Bb|d
(1) 试给出与G[A]等价的LL(1)文法G[A′]; (2) 构造G[A′]的LL(1)分析表; (3) 给出输入串aadl#的分析过程。 【解答】 (1) 文法G[A]存在左递归和回溯,故其不是 LL(1)文法。要将G[A]改造为LL(1)文法,首先要消除文法 的左递归,即将形如P→Pα | β 的产生式改造为 P→β P′ P→α P′| ε
#⋖ (⋖,⋖ (⋖a⋗)⋗)⋗# 因此,素短语为a。
第三章 语法分析
3.8 下述文法描述了C语言整数变量的声明语句: G[D]: D→TL T→int|long|short L→id|L,id
(1) 改造上述文法,使其接受相同的输入序列, 但文法是右递归的;
(2) 分别用上述文法G[D]和改造后的文法G[D′] 为输入序列int a,b,c构造分析树。
c. 最左推导和最右推导必定相同
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1) What is the difference between a compiler and an interpreterA compiler is a program that can read a program in one language - the source language - and translate it into an equivalent program in another language – the target language and report any errors in the source program that it detects during the translation process.Interpreter directly executes the operations specified in the source program on inputs supplied by the user.2) What are the advantages of:(a) a compiler over an interpretera. The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs.(b) an interpreter over a compilerb. An interpreter can usually give better error diagnostics than a compiler, because it executes the source program statement by statement.3) What advantages are there to a language-processing system in which the compilerproduces assembly language rather than machine languageThe compiler may produce an assembly-language program as its output, because assembly language is easier to produce as output and is easier to debug.4.2.3 Design grammars for the following languages:a) The set of all strings of 0s and 1s such that every 0 is immediately followed by at least 1.S -> SS | 1 | 01 |4.3.1 The following is a grammar for the regular expressions over symbols a and b only, using + in place of | for unions, to avoid conflict with the use of vertical bar as meta-symbol in grammars:r expr -> rexpr + rterm | rtermr term -> rterm rfactor | rfactorr factor -> rfactor * | rprimaryr primary -> a | ba) Left factor this grammar.r expr -> rexpr + rterm | rtermr term -> rterm rfactor | rfactorr factor -> rfactor * | rprimaryr primary -> a | bb) Does left factoring make the grammar suitable for top-down parsingNo, left recursion is still in the grammar.c) In addition to left factoring, eliminate left recursion from the original grammar.r expr -> rterm rexpr’rexpr’ -> + rterm rexpr |r term -> rfactor rterm’r term’ -> rfactor rterm |rfactor -> rprimary rfactor’r factor’ -> * rfactor’ |r primary -> a | bd) Is the resulting grammar suitable for top-down parsingYes.Exercise 4.4.1 For each of the following grammars, derive predictive parsers and show the parsing tables. You may left-factor and/or eliminate left-recursion from your grammars first. A predictive parser may be derived by recursive decent or by the table driven approach. Either way you must also show the predictive parse table.a) The grammar of exercise 4.2.2(a).4.2.2 a) S -> 0S1 | 01This grammar has no left recursion. It could possibly benefit from left factoring. Here is the recursive decent PP code.s() {m atch(‘0’);i f (lookahead == ‘0’)s();m atch(‘1’);}OrLeft factoring the grammar first:S -> 0S’S’ -> S1 | 1s() {m atch(‘0’); s’();}s’() {i f (lookahead == ‘0’)s(); match(‘1’);e lsematch(‘1’);}Now we will build the PP tableS -> 0S’S’ -> S1 | 1First(S) = {0}First(S’) = {0, 1}Follow(S) = {1, $}Follow(S’) = {1, $}The predictive parsing algorithm on page 227 and can use this table for non-recursive predictive parsing.b) The grammar of exercise 4.2.2(b).4.2.2 b) S -> +SS | *SS | a with string +*aaa.Left factoring does not apply and there is no left recursion to remove.s() {i f(lookahead == ‘+’)match(‘+’); s(); s();e lse i f(lookahead == ‘*’)match(‘*’); s(); s();e lse if(lookahead == ‘a’)match(‘a’);e lsereport(“syntax error”);}First(S) = {+, *, a}Follow(S) = {$, +, *, a}The predictive parsing algorithm on page 227 and can use this table for non-recursive predictive parsing.5.1.1 a, b, c: Investigating GraphViz as a solution to presenting trees Extend the SDD of Fig. to handle expressions as in Fig. :1.L -> E N1. =2. E -> F E'1. = E'.syn2.E'.inh =3.E' -> + T Esubone'1.Esubone'.inh = E'.inh +2.E'.syn = Esubone'.syn4.T -> F T'1.T'.inh =2. = T'.syn5.T' -> * F Tsubone'1.Tsubone'.inh = T'.inh *2.T'.syn = Tsubone'.syn6.T' -> epsilon1.T'.syn = T'.inh7.E' -> epsilon1.E'.syn = E'.inh8. F -> digit1. =9. F -> ( E )1. =10. E -> T1. =5.1.3 a, b, c: Investigating GraphViz as a solution to presenting treesWhat are all the topological sorts for the dependency graph of Fig.1.1, 2, 3, 4, 5, 6, 7, 8, 92.1, 2, 3, 5, 4, 6, 7, 8, 93.1, 2, 4, 3, 5, 6, 7, 8, 94.1, 3, 2, 4, 5, 6, 7, 8, 95.1, 3, 2, 5, 4, 6, 7, 8, 96.1, 3, 5, 2, 4, 6, 7, 8, 97.2, 1, 3, 4, 5, 6, 7, 8, 98.2, 1, 3, 5, 4, 6, 7, 8, 99.2, 1, 4, 3, 5, 6, 7, 8, 910.2, 4, 1, 3, 5, 6, 7, 8, 95.2.2 a, b: Investigating GraphViz as a solution to presenting treesSuppose that we have a production A -> BCD. Each of the four nonterminals A, B, C, and D have two attributes: s is a synthesized attribute, and i is an inherited attribute. For each of the sets of rules below, tell whether (1) the rules are consistent with an S-attributed definition (2) the rules are consistent with an L-attributed definition, and (3) whether the rules are consistent with any evaluation order at all?a) = +1.No--contains inherited attribute2.Yes--"From above or from the left"3.Yes--L-attributed so no cyclesb) = + and = +1.No--contains inherited attributes2.Yes--"From above or from the left"3.Yes--L-attributed so no cyclesc) = +1.Yes--all attributes synthesized2.Yes--all attributes synthesized3.Yes--S- and L-attributed, so no cyclesd)== +== +1.No--contains inherited attributes2. uses , which depends on , which depends on (cycle)3.No--Cycle implies no topological sorts (evaluation orders)using the rulesBelow is a grammar for expressions involving operator +and integer or floating-point operands. Floating-point numbers are distinguished by having a decimal point.1. E -> E + T | T2.T -> num . num | numa) Give an SDD to determine the type of each term T and expression E.1. E -> Esubone + T1. = if == float|| == float) { = float} else{ = integer}2. E -> T1. =3.T -> numsubone . numsubtwo1. = float4.T -> num1. = integerb) Extend your SDD of (a) to translate expressions into postfix notation. Use the binary operator intToFloat to turn an integer into an equivalent : I use character ',' to separate floating point numbers in the resulting postfix notation. Also, the symbol "||" implies concatenation.1. E -> Esubone + T1. = || ',' || || '+'2. E -> T1. =3.T -> numsubone . numsubtwo1. = || '.' ||4.T -> num1. = intToFloat5.3.2 Give an SDD to translate infix expressions with + and * into equivalent expressions without redundant parenthesis. For example, since both operators associate from the left, and * takes precedence over +, ((a*(b+c))*(d)) translates into a*(b+c)*d.Note: symbol "||" implies concatenation.1.S -> E1. = nil2. =2. E -> Esubone + T1. =2. =3. = || '+' ||4. = '+'3. E -> T1. =2. =3. =4.T -> Tsubone * F1. = '*'2. = '*'3. = || '*' ||4. = '*'5.T -> F1. =2. =3. =6. F -> char1. =2. = nil7. F -> ( E )1.if == '*' && == '+') { = '(' || || ')' } else { = }2. = nil5.3.3: Give an SDD to differentiate expressions such as x * (3*x + x * x) involving the operators + and *, the variable x, and constants. Assume that no simplification occurs, so that, for example, 3*x will be translated into 3*1 + 0*x.Note: symbol "||" implies concatenation. Also, differentiation(x*y) = (x * differentiation(y) + differentiation(x) * y) and differentiation(x+y) = differentiation(x) + differentiation(y).1.S -> E1. =2. E -> T1. =2. =3.T -> F1. =2. =4.T -> Tsubone * F1. = '(' || || ") * (" || || ") + (" || || ") * (" |||| ')'2. = || '*' ||5. E -> Esubone + T1. = '(' || || ") + (" || || ')'2. = || '+' ||6. F -> ( E )1. =2. = '(' || || ')'7. F -> char1. = 12. =8. F -> constant1. = 02. =。

相关文档
最新文档