编译原理课后答案

第二章2.3叙述由下列正规式描述的语言(a) 0(0|1)*0在字母表{0, 1}上,以0开头和结尾的长度至少是2的01串(b) ((ε|0)1*)*在字母表{0, 1}上,所有的01串,包括空串(c) (0|1)*0(0|1)(0|1)在字母表{0, 1}上,倒数第三位是0的01串(d) 0*10*10*10*在字母表{0, 1}上,含有3个1的01串(e) (00|11)*((01|10)(00|11)*(01|10)(00|11)*)*在字母表{0, 1}上,含有偶数个0和偶数个1的01串2.4为下列语言写正规定义C语言的注释,即以 /* 开始和以 */ 结束的任意字符串,但它的任何前缀(本身除外)不以 */ 结尾。

[解答] other → a | b | … other指除了*以外C语言中的其它字符other1 → a | b | …other1指除了*和/以外C语言中的其它字符 comment → /* other* (* ** other1 other*)* ** */(f) 由偶数个0和偶数个1构成的所有0和1的串。

[解答]由题目分析可知,一个符号串由0和1组成,则0和1的个数只能有四种情况:x 偶数个0和偶数个1(用状态0表示); x 偶数个0和奇数个1(用状态1表示); x 奇数个0和偶数个1(用状态2表示); x 奇数个0和奇数个1(用状态3表示);所以,x 状态0(偶数个0和偶数个1)读入1,则0和1的数目变为:偶数个0和奇数个1(状态1)x 状态0(偶数个0和偶数个1)读入0,则0和1的数目变为:奇数个0和偶数个1(状态2)x 状态1(偶数个0和奇数个1)读入1,则0和1的数目变为:偶数个0和偶数个1(状态0)x 状态1(偶数个0和奇数个1)读入0,则0和1的数目变为:奇数个0和奇数个1(状态3)x 状态2(奇数个0和偶数个1)读入1,则0和1的数目变为:奇数个0和奇数个1(状态3)x 状态2(奇数个0和偶数个1)读入0,则0和1的数目变为:偶数个0和偶数个1(状态0)x 状态3(奇数个0和奇数个1)读入1,则0和1的数目变为:奇数个0和偶数个1(状态2)x 状态3(奇数个0和奇数个1)读入0,则0和1的数目变为:偶数个0和奇数个1(状态1)因为,所求为由偶数个0和偶数个1构成的所有0和1的串,故状态0既为初始状态又为终结状态,其状态转换图:由此可以写出其正规文法为:S0 → 1S1 | 0S2 | ε S1 → 1S0 | 0S3 | 1 S2 → 1S3 | 0S0 | 0 S3 → 1S2 | 0S1在不考虑S0 →ε产生式的情况下,可以将文法变形为: S0 = 1S1 + 0S2 S1 = 1S0 + 0S3 + 1 S2 = 1S3 + 0S0 + 0S3 = 1S2 + 0S1 所以: S0 = (00|11) S0 + (01|10) S3 + 11 + 00(1) S3 = (00|11) S3 + (01|10) S0 + 01 + 10(2) 解(2)式得: S3 = (00|11)* ((01|10) S0 + (01|10)) 代入(1)式得:S0 = (00|11) S0 + (01|10) (00|11)*((01|10) S0 + (01|10)) + (00|11) => S0 = ((00|11) + (01|10) (00|11)*(01|10))S0 + (01|10) (00|11)*(01|10) + (00|11) => S0 = ((00|11)|(01|10) (00|11)*(01|10))*((00|11) + (01|10) (00|11)* (01|10)) => S0 = ((00|1 1)|(01|10) (00|11)* (01|10))+因为S0→ε所以由偶数个0和偶数个1构成的所有0和1的串的正规定义为: S0 → ((00|11)|(01|10) (00|11)* (01|10))* (g) 由偶数个0和奇数个1构成的所有0和1的串。

[解答]此题目我们可以借鉴上题的结论来进行处理。

对于由偶数个0和奇数个1构成的所有0和1的串,我们分情况讨论:(1) 若符号串首字符为0,则剩余字符串必然是奇数个0和奇数个1,因此我们必须在上题偶数个0和偶数个1的符号串基础上再读入10(红色轨迹)或01(蓝色轨迹),又因为在0→1和1→3的过程中可以进行多次循环(红色虚线轨迹),同理0→2和2→3(蓝色虚线轨迹),所以还必须增加符号串(00|11)*,我们用S0表示偶数个0和偶数个1, 用S 表示偶数个0和奇数个1则其正规定义为: S → 0(00|11)*(01|10) S0 S0 → ((00|11)|(01|10) (00|11)* (01|10))*(2) 若符号串首字符为1,则剩余字符串必然是偶数个0和偶数个1,其正规定义为: S → 1S0S0 → ((00|11)|(01|10) (00|11)* (01|10))* 综合(1)和(2)可得,偶数个0和奇数个1构成的所有0和1串其正规定义为: S → 0(00|11)*(01|10) S0|1S0 S0 → ((00|11)|(01|10) (00|11)* (01|10))*2.7(c) ((ε|a)b*)*ababbab:s->4->0->1->5->6->7->8->4->0->1->5->6->7->6->7->8->4->0->1->5->6->7->8->f2.12 为下列正规式构造最简的DFA(b) (a|b)* a (a|b) (a|b)(1) 根据算法2.4构造该正规式所对应的NFA ,如图所示。

(2) 根据算法2.2(子集法)将NFA 转换成与之等价的DFA (确定化过程) 初始状态 S0 = ε-closure(0) = {0, 1, 2, 4, 7} 标记状态S0S1 = ε-closure(move(S0, a)) = ε-closure({5, 8}) = {1, 2, 4, 5, 6, 7, 8, 9, 11} S2 = ε-closure(move(S0, b)) = ε-closure({3}) = {1, 2, 3, 4, 6, 7} 标记状态S1S3 = ε-closure(move(S1, a)) = ε-closure({5, 8, 12}) = {1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16} S4 = ε-closure(move(S1, b)) = ε-closure({3, 10}) = {1, 2, 4, 5, 6, 7, 10, 13, 14, 16} 标记状态S2S1 = ε-closure(move(S2, a)) = ε-closure({5, 8}) = {1, 2, 4, 5, 6, 7, 8, 9, 11} S2 = ε-closure(move(S2, b)) = ε-closure({3}) = {1, 2, 3, 4ε εa ε ε ε ε εb ε ε ε ε start, 6, 7} 标记状态S3S5 = ε-closure(move(S3, a)) = ε-closure({5, 8, 12, 17}) = {1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18}S6 = ε-closure(move(S3, b)) = ε-closure({3, 10, 15}) = {1, 2, 4, 5, 6, 7, 10, 13, 14, 15, 16, 18} 标记状态S4S7 = ε-closure(move(S4, a)) = ε-closure({5, 8, 17}) = {1, 2, 4, 5, 6, 7, 8, 9, 11, 17, 18}S8 = ε-closure(move(S4, b)) = ε-closure({3, 15}) = {1, 2, 3, 4, 6, 7, 15, 18} 标记状态S5S5 = ε-closure(move(S5, a)) = ε-closure({5, 8, 12, 17}) = {1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18}S6 = ε-closure(move(S5, b)) = ε-closure({3, 10, 15}) = {1, 2, 4, 5, 6, 7, 10, 13, 14, 15, 16, 18} 标记状态S6S7 = ε-closure(move(S6, a)) = ε-closure({5, 8, 17}) = {1, 2, 4, 5, 6, 7, 8, 9, 11, 17, 18}S8 = ε-closure(move(S6, b)) = ε-closure({3, 15}) = {1, 2, 3, 4, 6, 7, 15, 18} 标记状态S7S3 = ε-closure(move(S7, a)) = ε-closure({5, 8, 12}) = {1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16}S4 = ε-closure(move(S7, b)) = ε-closure({3, 10}) = {1, 2, 4, 5, 6, 7, 10, 13, 14, 16} 标记状态S8S1 = ε-closure(move(S8, a)) = ε-closure({5, 8}) = {1, 2, 4, 5, 6, 7, 8, 9, 11} S2 = ε-closure(move(S8, b)) = ε-closure({3}) = {1, 2, 3, 4 , 6, 7}由以上可知,确定化后的DFA的状态集合S = {S0, S1, S2, S3, S4, S5, S6, S7, S8},输入符号集合Σ = {a, b},状态转换函数move如上,S0为开始状态,接收状态集合F = {S5, S6, S7, S8},其状态转换图如下所示:(3) 根据算法2.3过将DFA最小化第一次划分:{S0, S1, S2, S3, S4} {S5, S6, S7, S8} {S0, S1, S2, S3, S4}a = {S1, S3, S1, S5, S7}第二次划分:{S0, S1, S2} {S3, S4} {S5, S6, S7, S8} {S0, S1, S2}a = {S1, S3, S1}第三次划分:{S0, S2} {S1} {S3, S4} {S5, S6, S7, S8}{S0, S2}a = {S1} {S0, S2}b = {S2} S0, S2不可区分,即等价。

合集下载

编译原理 全部参考答案

编译原理 全部参考答案

第二章P-36-6(1)L(G)是0~9组成的数字串;(2)最左推导:N⇒ND⇒NDD⇒NDDD⇒DDDD⇒0DDD⇒01DD⇒012D⇒0127N⇒ND⇒DD⇒3D⇒34N⇒ND⇒NDD⇒DDD⇒5DD⇒56D⇒568最右推导:N⇒ND⇒N7⇒ND7⇒N27⇒ND27⇒N127⇒D127⇒0127N⇒ND⇒N4⇒D4⇒34N⇒ND⇒N8⇒ND8⇒N68⇒D68⇒568P-36-7G(S):(没有考虑正负符号问题)S→P|APP→1|3|5|7|9A→AD|NN→2|4|6|8|PD→0|N或者:(1)S→ABC|CA→1|2|3|4|5|6|7|8|9B→BA|B0|εC→1|3|5|7|9P-36-8G(E):E→T|E+T|E-TT→F|T*F|T/FF→(E)|i最左推导:E⇒E+T⇒T+T⇒F+T⇒i+T⇒i+T*F⇒i+F*F⇒i+i*F⇒i+i*iE⇒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+T*F⇒E+T*i⇒E+F*i⇒E+i*i⇒T+i*i⇒F+i*i⇒i+i*iE⇒T⇒T*F⇒T*(E)⇒T*(E+T)⇒T*(E+F)⇒T*(E+i)⇒T*(T+i)⇒T*(F+i)⇒T*(i+i)⇒F*(i+i)⇒i*(i+i)语法树:SiSeSiSiiSiS i S eS iiP-36-9句子:iiiei 有两个语法树: S ⇒iSeS ⇒iSei ⇒iiSei ⇒iiiei S ⇒iS ⇒iiSeS ⇒iiSei ⇒iiiei因此iiiei 是二义性句子,因此 该文法是二义性的。

P-36-10 S →TS|T T →(S)|()P-36-11L1: G(S): S →AC A →aAb|ab C →cC|ε L2: G(S): S →AB A →aA|ε B →bBc|bc L3: G(S): S →AB A →aAb|ε B →aAb|ε L4: G(S): S →1S0|A A →0A1|ε或者:S →A|B A →0A1|ε B →1B0|A 第三章(1)Ei+i+iE +TE +TT F iF iF iEE +TT F iT *FF iii+i*i Ei-i-iE-TE -TT F iF iF i确定化:最小化:{0,1,2,3,4,5},{6}{0,1,2,3,4,5}0={1,3,5} {0,1,2,3,4,5}1={1,2,4,6} {0,1,2,3,4},{5},{6} {0,1,2,3,4}0={1,3,5} {0,1,2,3},{4},{5},{6}{0,1,2,3}0={1,3} {0,1,2,3}1={1,2,4} {0,1},{2,3},{4},{5},{6}{0,1}0={1} {0,1}1={1,2} {2,3}0={3} {2,3}1={4} {0},{1},{2,3},{4},{5},{6}P64-8(1)(0|1)*01(2)(1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)*(0|5)|(0|5)(3)0*1(0|10*1)* | 1*0(1|01*0)*P84-12(a)a最小化:{0,1} {2,3}{0,1}a={1},{0,1}b={2}{2,3}a={0,3},{2,3}={3}{0,1},{2},{3}a(b)已经确定化,只需最小化:{0,1},{2,3,4,5}{0,1}a = {1} {0,1}b = {2,4}{2,3,4,5}a = {1,3,0,5} {2,3,4,5}b = {2,3,4,5}又:{2,4}a = {1,0} {2,4}b = {3,5} {3,5}a={3,5} {3,5}b = {2,4} 分划为:{0,1},{2,4},{3,5}{0,1}a = {1} {0,1}b = {2,4}{2,4}a = {1,0} {2,4}b = {3,5}{3,5}a = {3,5} {3,5}b = {2,4}所以不能再分aP64-14正规式:(0|10)*还可以:然后再确定化,最小化,结果应该一样。

编译原理第四版课后答案

编译原理第四版课后答案

编译原理第四版课后答案1. What are the three basic phases of a compiler and what are their main functions?- The three basic phases of a compiler are lexical analysis, syntax analysis, and code generation.- The main function of lexical analysis is to read the source code and break it into individual tokens, such as keywords, identifiers, numbers, and symbols.- The main function of the syntax analysis is to parse the tokens and verify that they form valid syntax according to the grammar rules of the programming language.- The main function of code generation is to convert the parsed tokens into executable code in a target programming language or machine code.2. What is lexical analysis and what are its main tasks?- Lexical analysis is the first phase of a compiler, which reads the source code and breaks it into individual tokens.- The main tasks of lexical analysis include tokenization, where the source code is divided into meaningful units called tokens, such as keywords, identifiers, numbers, and symbols; removal of comments, where any comments in the source code are ignored; and removal of white spaces, where unnecessary spaces, tabs, and line breaks are eliminated.3. What is a parser and what is its main function?- A parser is a component of the compiler that performs syntax analysis, also known as parsing.- Its main function is to analyze the structure of the tokens generated by the lexical analysis phase and verify that they form avalid syntax according to the grammar rules of the programming language.- The parser constructs a derivation tree or a parse tree to represent the structure of the code and checks for syntax errors, such as missing or misplaced tokens.4. What is the difference between a compiler and an interpreter?- A compiler is a program that translates the entire source code of a programming language into an equivalent target code or machine code before execution.- An interpreter, on the other hand, does not translate the entire source code into machine code before execution. Instead, it reads and executes the source code line by line, translating and executing each line as it encounters it.- In terms of efficiency, a compiled program tends to run faster than an interpreted program because the compiled code is already in machine language, whereas the interpreted code needs to be translated and executed at runtime.5. What are the advantages and disadvantages of using an interpreted language?- Advantages of using an interpreted language include faster development time, as there is no need to compile the entire code before execution; easier debugging, as errors can be detected and fixed immediately; and platform independence, as the interpreter can run on different operating systems without the need to compile separate binaries.- Disadvantages of using an interpreted language include slower execution speed compared to compiled languages; lower performance, as the interpreter needs to translate and execute eachline at runtime; and potential security risks, as the interpreted code can be easily accessed and modified.6. What is meant by bytecode and what is its role in interpreter-based execution?- Bytecode is a low-level representation of the source code that is generated by a compiler or an interpreter. It is a set of instructions that can be executed by a virtual machine.- In interpreter-based execution, the source code is first compiled into bytecode, which is a platform-independent representation of the code. The interpreter then reads and executes the bytecode on the virtual machine, providing a compromise between compilation and interpretation.- Bytecode allows for faster execution compared to interpreting the source code directly, as the bytecode is already in a form that can be executed by the virtual machine.7. What is code optimization and why is it important?- Code optimization is the process of improving the efficiency and performance of the generated code by the compiler.- It is important because optimized code can run faster and consume less memory, resulting in improved overall performance of the program.- Code optimization techniques include constant folding, loop unrolling, dead code elimination, and register allocation, among others.8. What is a symbol table and what is its purpose?- A symbol table is a data structure that is used by a compiler to store information about the variable and function namesencountered in the source code.- Its purpose is to keep track of the properties and attributes of each symbol, such as its data type, memory location, scope, and visibility.- The symbol table is used by various phases of the compiler, such as the lexical analyzer, parser, and code generator, to perform tasks such as name resolution, type checking, and memory management.9. What is the role of an assembler in the compilation process?- An assembler is a program that converts assembly language code into machine code.- In the compilation process, the assembler is responsible for translating the assembly language code written by the programmer into machine code that can be executed directly by the computer hardware.- The assembler performs a one-to-one mapping of assembly instructions to their corresponding machine code instructions, and also resolves symbolic addresses and labels used by the programmer.10. What is the difference between a single-pass compiler and a multi-pass compiler?- A single-pass compiler is a compiler that reads the source code of a program once and generates the corresponding executable code in a single pass or iteration.- A multi-pass compiler, on the other hand, requires multiple passes or iterations over the source code in order to generate the executable code.- Single-pass compilers are generally simpler and require less memory, but they are unable to perform certain optimizations orglobal analysis that requires information from the entire source code. Multi-pass compilers are more powerful and can perform more complex optimizations and analysis, but they are typically slower and require more memory.。

编译原理课后习题答案

编译原理课后习题答案

(a) 0 ( 0 | 1)* 0
由0和1组成且以0开始和结束的符号串全体. (b) ( ( | 0 ) 1* ) * 由0和1组成的符号串全体.
(c) ( 0 | 1 )* 0 ( 0 | 1) ( 0 | 1) 由0和1组成且以000,001,010或011结束的符号串全体. 长度大于等于3且倒数第3个字符为0的01符号串全体.
R R ‘|’ S | S S ST | T T U* | U U (R) | a | b
a
a
a
28
4.5 dangling-else文法: stmt if expr then stmt | matched-stmt matched-stmt if expr then matched-stmt else stmt | other 试说明此文法是二义性的。 句子 if e1 then if e2 then s1 else if e3 then s2 else s3 if e1 then if e2 then s1 else if e3 then s2 else s3
0|1 B 1 D E 0 ABDE ABDE ABCDE ABDE 1 ABCDE ABCDE
start
A
1
C
NFA 0
start A' 1
0
B'
0
1
start
A'
1
最小化DFA
24
DFA
3.8 给定右线性文法G: S 0S | 1S | 1A | 0B A 1C |1 B 0C | 1 C 0C | 1C | 0 | 1 试求一个等价的左线性文法G’.
20
3.6 给出接受下列在字母表{0,1}上的DFA。 (a)所有以00结束的符号串的集合; (1|0)*00

《编译原理》课后习题答案

《编译原理》课后习题答案

第7 题证明下述文法G[〈表达式〉]是二义的。

〈表达式〉∷=a|(〈表达式〉)|〈表达式〉〈运算符〉〈表达式〉〈运算符〉∷=+|-|*|/答案:可为句子a+a*a 构造两个不同的最右推导:最右推导1 〈表达式〉=>〈表达式〉〈运算符〉〈表达式〉=>〈表达式〉〈运算符〉a=>〈表达式〉* a=>〈表达式〉〈运算符〉〈表达式〉* a=>〈表达式〉〈运算符〉a * a=>〈表达式〉+ a * a=>a + a * a最右推导2 〈表达式〉=>〈表达式〉〈运算符〉〈表达式〉=>〈表达式〉〈运算符〉〈表达式〉〈运算符〉〈表达式〉=>〈表达式〉〈运算符〉〈表达式〉〈运算符〉a=>〈表达式〉〈运算符〉〈表达式〉* a=>〈表达式〉〈运算符〉a * a=>〈表达式〉+ a * a=>a + a * a第8 题文法G[S]为:S→Ac|aB A→ab B→bc该文法是否为二义的?为什么?答案:对于串abc(1)S=>Ac=>abc (2)S=>aB=>abc即存在两不同的最右推导。

所以,该文法是二义的。

或者:对输入字符串abc,能构造两棵不同的语法树,所以它是二义的。

第9 题考虑下面上下文无关文法:S→SS*|SS+|a(1)表明通过此文法如何生成串aa+a*,并为该串构造语法树。

(2)G[S]的语言是什么?答案:(1)此文法生成串aa+a*的最右推导如下S=>SS*=>SS*=>Sa*=>SS+a*=>Sa+a*=>aa+a*(2)该文法生成的语言是:*和+的后缀表达式,即逆波兰式。

第10 题文法S→S(S)S|ε(1) 生成的语言是什么?(2) 该文法是二义的吗?说明理由。

答案:(1)嵌套的括号(2)是二义的,因为对于()()可以构造两棵不同的语法树。

第11 题令文法G[E]为:E→T|E+T|E-T T→F|T*F|T/F F→(E)|i证明E+T*F 是它的一个句型,指出这个句型的所有短语、直接短语和句柄。

(完整word版)编译原理课后答案

(完整word版)编译原理课后答案

第二章 高级语言及其语法描述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 -iiiEEFT+T F FTiii*i+i+ii-i-ii+i*i*****************/9.证明下面的文法是二义的:S → iSeS|iS|I解:因为iiiiei 有两种最左推导,所以此文法是二义的。

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

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

编译原理(龙书)课后习题解答(详细)编译原理(龙书)课后题解答第一章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 :什么是自下而上分析?答:自下而上分析是指从输入字符串出发,自底向上构造推导过程,直到推导出起始符号。

(完整版)编译原理课后答案(第三版蒋立源康慕宁编)

编译原理课后答案(第三版蒋立源康慕宁编)第一章习题解答1解:源程序是指以某种程序设计语言所编写的程序。

目标程序是指编译程序(或解释程序)将源程序处理加工而得的另一种语言(目标语言)的程序。

翻译程序是将某种语言翻译成另一种语言的程序的统称。

编译程序与解释程序均为翻译程序,但二者工作方法不同。

解释程序的特点是并不先将高级语言程序全部翻译成机器代码,而是每读入一条高级语言程序语句,就用解释程序将其翻译成一段机器指令并执行之,然后再读入下一条语句继续进行解释、执行,如此反复。

即边解释边执行,翻译所得的指令序列并不保存。

编译程序的特点是先将高级语言程序翻译成机器语言程序,将其保存到指定的空间中,在用户需要时再执行之。

即先翻译、后执行。

2解:一般说来,编译程序主要由词法分析程序、语法分析程序、语义分析程序、中间代码生成程序、代码优化程序、目标代码生成程序、信息表管理程序、错误检查处理程序组成。

3解:C语言的关键字有:auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while。

上述关键字在C语言中均为保留字。

4解:C语言中括号有三种:{},[],()。

其中,{}用于语句括号;[]用于数组;()用于函数(定义与调用)及表达式运算(改变运算顺序)。

C语言中无END关键字。

逗号在C语言中被视为分隔符和运算符,作为优先级最低的运算符,运算结果为逗号表达式最右侧子表达式的值(如:(a,b,c,d)的值为d)。

5略第二章习题解答1.(1)答:26*26=676(2)答:26*10=260(3)答:{a,b,c,...,z,a0,a1,...,a9,aa,...,az,...,zz,a00,a01,...,zzz},共26+26*36+26*36*36=34658个2.构造产生下列语言的文法(1){anbn|n≥0}解:对应文法为G(S) = ({S},{a,b},{ S→ε| aSb },S)(2){anbmcp|n,m,p≥0}解:对应文法为G(S) = ({S,X,Y},{a,b,c},{S→aS|X,X→bX|Y,Y→cY|ε},S)(3){an # bn|n≥0}∪{cn # dn|n≥0}解:对应文法为G(S) = ({S,X,Y},{a,b,c,d,#}, {S→X, S→Y,X→aXb|#,Y→cYd|# },S)(4){w#wr# | w?{0,1}*,wr是w的逆序排列}解:G(S) = ({S,W,R},{0,1,#}, {S→W#, W→0W0|1W1|# },S)(5)任何不是以0打头的所有奇整数所组成的集合解:G(S) = ({S,A,B,I,J},{-,0,1,2,3,4,5,6,7,8,9},{S→J|IBJ,B→0B|IB|e, I→J|2|4|6|8, Jà1|3|5|7|9}, S)(6)所有偶数个0和偶数个1所组成的符号串集合解:对应文法为S→0A|1B|e,A→0S|1C B→0C|1S C→1A|0B3.描述语言特点(1)S→10S0S→aAA→bAA→a解:本文法构成的语言集为:L(G)={(10)nabma0n|n, m≥0}。

编译原理课后答案-第二版

第三章1、L(G[S])={ abc }2、L(G[N])={ n位整数或空字符串| n>0 }3、G[E]:E—>E+D | E-D | DD—>0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 94、L(G[Z])={ a n b n | n>0 }5、(1) 考虑不包括“0”的情况G[S]:S—>0S | ABC | 2 | 4| 6 | 8A—>1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9B—>AB | 0B | εC—>0 | 2 | 4 | 6 | 8考虑包括“0”的情况:G[S]:S—>AB | CB—>AB | CA—>0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9C—>0 | 2 | 4 | 6 | 8(2)方法1:G[S]:S—> ABC | 2 | 4 | 6 | 8A—>1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9B—>AB | 0B | εC—>0 | 2 | 4 | 6 | 8方法2:G[S]:S—>AB | CB—> AB | 0B | C | 0A—> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9C—>2 | 4 | 6 | 86、设<表达式>为E,<项>为T,<因子>为F,注:推导过程不能省略,以下均为最左推导(1) E => T => F => i(4) E => E+T => T+T => T*F+T => F*F+T => i*F+T => i*i+T => i*i+F => i*i+i(6) E => E+T => T+T => F+T => i+T => i+T*F => i+F*F => i+i*F => i+i*I7、<表达式><表达式>*<表达式><表达式>+<表达式>i i i<表达式><表达式>+<表达式>i <表达式>*<表达式>i i8、是有二义性的,因为句子abc 有两棵语法树(或称有两个最左推导或有两个最右推导)最左推导1:S => Ac => abc 最左推导2:S => aB => abc 9、(1)(2) 该文法描述了变量a 和运算符+、*组成的逆波兰表达式10、(1) 该文法描述了各种成对圆括号的语法结构(2) 是有二义性的,因为该文法的句子()()存在两种不同的最左推导: 最左推导1:S => S(S)S => (S)S => ()S => ()S(S)S => ()(S)S => ()()S => ()()最左推导2:S => S(S)S => S(S)S(S)S => (S)S(S)S=> ()S(S)S => ()(S)S => ()()S => ()()11、(1) 因为从文法的开始符E 出发可推导出E+T*F ,推导过程如下:E => E+T =>E+T*F ,所以E+T*F 是句型。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

编译原理课后答案

<表达式>AVV ------ *第二早1、 L(G[S])={ abc }2、 L(G[N])={ n 位整数或空字符串| n>0}3、 G[E] : E —>E+D | E-D | DD —>0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 94、 L(G[Z])={ a n b n | n>0 }5、(1)考虑不包括“ 0”的情况G[S]: S — >0S | ABC | 2 | 4| 6 | 8A —>1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9B —>AB | 0B | &C —>0 | 2 | 4 | 6 | 8考虑包括“ 0”的情况: G[S]: S — >AB | CB —>AB | CA —>0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 C —>0 | 2 | 4 | 6 | 8(2)方法1:G[S]: S — > ABC | 2 | 4 | 6 | 8A —>1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9B —>AB | 0B | &C —>0 | 2 | 4 | 6 | 8方法2:G[S]: S — >AB | CB —> AB | 0B |C | 0A —> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 C —>2 | 4 | 6 | 8&设<表达式 >为E , <项>为T , <因子〉为F ,注:推导过程不能省略,以下均为最 左推导(1) E => T => F => i(4) E => E+T => T+T => T*F+T => F*F+T => i*F+T => i*i+T => i*i+F => i*i+i (6) E => E+T => T+T => F+T => i+T => i+T*F => i+F*F => i+i*F => i+i*I8、 是有二义性的,因为句子abc 有两棵语法树(或称有两个最左推导或有两个最右 推导)ii<表达式>最左推导1: S => Ac => abc最左推导2:S => aB => abc9、⑴a a(2) 该文法描述了变量a和运算符+、*组成的逆波兰表达式10、(1)该文法描述了各种成对圆括号的语法结构(2)是有二义性的,因为该文法的句子()()存在两种不同的最左推导:最左推导1:S => S(S)S => (S)S => ()S => ()S(S)S => ()(S)S => ()()S => ()() 最左推导2:S => S(S)S => S(S)S(S)S => (S)S(S)S=> ()S(S)S => ()(S)S => ()()S => ()()11、⑴因为从文法的开始符E出发可推导出E+T*F,推导过程如下:E => E+T =>E+T*F,所以E+T*F 是句型。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档