编译原理与实践 第三章 答案
- 格式:doc
- 大小:91.50 KB
- 文档页数:5
第三章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 => abc9、(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 是句型。
第三章作业第三章作业答案P47 练习1、文法G=({A,B,S},{a,b,c},P,S),其中P为:S->Ac|aB A->ab B->bc写出L(G [S])的全部元素。
S=>Ac=>abc或S=>aB=>abc所以L(G[S])={abc}2、文法G[N]为:N->D|NDD->0|1|2|3|4|5|6|7|8|9G[N]的语言是什么?【解】N=>ND=>NDD.... =>NDDDD...D=>D......DG[N]的语言是V+。
V={0,1,2,3,4,5,6,7,8,9}或:解: N ND n-1D n{0,1,3,4,5,6,7,8,9}+∴L(G[N])= {0,1,3,4,5,6,7,8,9}+5.写一文法,使其语言是偶正数的集合。
要求:(1)允许0打头(2)不允许0打头【解】(1)允许0开头的偶正整数集合的文法E->NT|G|SFMT->NT|GN->D|1|3|5|7|9D->0|GG->2|4|6|8S->NS|εF->1|3|5|7|9|GM->M0|0(2)不允许0开头的偶正整数集合的文法E->NT|DT->FT|GN->D|1|3|5|7|9D->2|4|6|8F->N|0G->D|09.考虑下面上下文无关文法:S→SS*|SS+|a(1) 表明通过此文法如何生成串aa+a*,并为该串构造推导树。
(2) 该文法生成的语言是什么?【解】(1) S=>SS*=>SS+S*aa+a*该串的推导树如下:(2) 该文法生成的语言是只含+、*的算术表达式的逆波兰表示。
11.令文法G[E]为:E→T|E+T|E-TT→F|T*F|T/FF→(E)|i证明E+T*F是它的一个句型,指出这个句型的所有短语、直接短语和句柄。
第三章习题参考解答3.1 构造自动机A,使得①②③当从左至右读入二进制数时,它能识别出读入的奇数;④它识别字母表{a, b}上的符号串,但符号串不能含两个相邻的a,也不含两个相邻的b;⑤它能接受字母表{0, 1}上的符号串,这些符号串由任意的1、0和随后的任意的11、00对组成。
⑥它能识别形式如±dd*⋅ d*E ±dd的实数,其中,d∈{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}。
3.2 构造下列正规表达式的DFSA:① xy*∣yx*y∣xyx;② 00∣(01)*∣11;③ 01((10∣01)*(11∣00))*01;④ a(ab*∣ba*)*b。
3.3 消除图3.24所示自动机的空移。
图3.24 含空移的自动机3.4 将图3.25所示NDFSA确定化和最小化。
图3.25 待确定化的NDFSA3.5 设e、e1、e2是字母表∑上的正规表达式,试证明① e∣e=e;② {{e}}={e};③ {e}=ε∣e{e};④ {e1 e2} e1= e1{e2 e1};⑤ {e1∣e2}={{e1}{e2}}={{e1}∣{e2}}。
3.6 构造下面文法G[Z]的自动机,指明该自动机是不是确定的,并写出它相应的语言: G[Z]:Z→A0A→A0∣Z1∣03.7 设NDFSA M=({x, y},{a, b},f, x, {y}), 其中,f(x, a)={x, y}, f(x, b)={y}, f(y, a)=∅, f(y, b)={x, y}。
试对此NDFSA 确定化。
3.8 设文法G[〈单词〉]:〈单词〉→〈标识符〉∣〈无符号整数〉 〈标识符〉→〈字母〉∣〈标识符〉〈字母〉∣〈标识符〉〈数字〉 〈无符号整数〉→〈数字〉∣〈无符号整数〉〈数字〉 〈字母〉→a ∣b 〈数字〉→1∣2试写出相应的有限自动机和状态图。
3.9 图3.29所示的是一个NDFSA A ,试构造一个正规文法G ,使得L(G)= L(A)。
第3 章文法和语言第1 题文法G=({A,B,S},{a,b,c},P,S)其中P 为:S→Ac|aBA→abB→bc写出L(G[S])的全部元素。
答案:L(G[S])={abc}第2 题文法G[N]为:N→D|NDD→0|1|2|3|4|5|6|7|8|9G[N]的语言是什么?答案: G[N]的语言是V+。
V={0,1,2,3,4,5,6,7,8,9}N=>ND=>NDD.... =>NDDDD...D=>D......D或者:允许0 开头的非负整数?第3题为只包含数字、加号和减号的表达式,例如9-2+5,3-1,7等构造一个文法。
答案:G[S]:S->S+D|S-D|DD->0|1|2|3|4|5|6|7|8|9第4 题已知文法G[Z]:Z→aZb|ab写出L(G[Z])的全部元素。
答案:Z=>aZb=>aaZbb=>aaa..Z...bbb=> aaa..ab...bbbL(G[Z])={anbn|n>=1}第5 题写一文法,使其语言是偶正整数的集合。
要求:(1) 允许0 打头;(2)不允许0 打头。
答案:(1)允许0 开头的偶正整数集合的文法E→NT|DT→NT|DN→D|1|3|5|7|9D→0|2|4|6|8(2)不允许0 开头的偶正整数集合的文法E→NT|DT→FT|GN→D|1|3|5|7|9D→2|4|6|8F→N|0G→D|0第6 题已知文法G:<表达式>::=<项>|<表达式>+<项> <项>::=<因子>|<项>*<因子><因子>::=(<表达式>)|i试给出下述表达式的推导及语法树。
(5)i+(i+i)(6)i+i*i答案:(5) <表达式>=><表达式>+<项>=><表达式>+<因子>=><表达式>+(<表达式>)=><表达式>+(<表达式>+<项>)=><表达式>+(<表达式>+<因子>)=><表达式>+(<表达式>+i)=><表达式>+(<项>+i)=><表达式>+(<因子>+i)=><表达式>+(i+i)=><项>+(i+i)=><因子>+(i+i)=>i+(i+i)(6) <表达式>=><表达式>+<项>=><表达式>+<项>*<因子> =><表达式>+<项>*i=><表达式>+<因子>*i =><表达式>+i*i=><项>+i*i=><因子>+i*i=>i+i*i<表达式><表达式> + <项><因子><表达式><表达式> + <项><因子>i<项><因子>i<项><因子>i()<表达式><表达式> + <项><项> * <因子><因子> i<项><因子>ii第7 题证明下述文法G[〈表达式〉]是二义的。
附录部分习题参考答案第1章习题1. 解释下列术语。
翻译程序,编译程序,解释程序,源程序,目标程序,遍,前端,后端解答:略!2. 高级语言程序有哪两种执行方式?阐述其主要异同点。
描述编译方式执行程序的过程。
解答:略!3. 在你所使用的C语言编译器中,观察程序1.1经过预处理、编译、汇编、链接四个过程生成的中间结果。
解答:略!4. 编译程序有哪些主要构成成分?各自的主要功能是什么?解答:略!5. 编译程序的构造需要掌握哪些原理和技术?编译程序构造工具的作用是什么?解答:略!6. 复习C语言,其字母表中有哪些符号?有哪些关键字、运算符和界符?标识符、整数和实数的构成规则是怎样的?各种语句和表达式的结构是什么样的?解答:略!7.编译技术可应用在哪些领域?解答:略!8. 你能解释在Java编译器中,输入某个符号后会提示一些单词、某些单词会变为不同的颜色是如何实现的吗?你能解释在Code Blocks中在输入{后,会自动添加},输入do 会自动添加while()是为什么吗?解答:略!第2章习题1. 判断题,对下面的陈述,正确的在陈述后的括号内画√,否则画×。
(1) 有穷自动机识别的语言是正规语言。
()(2) 若r1和r2是Σ上的正则表达式,则r1|r2也是。
()(3) 设M是一个NFA,并且L(M)={x,y,z},则M的状态数至少为4个。
()(4) 令Σ={a,b},则所有以b开头的字构成的正规集的正则表达式为b*(a|b)*。
()(5) 对任何一个NFA M,都存在一个DFA M',使得L(M')=L(M)。
()1解答:略!2.从供选择的答案中,选出应填入下面叙述中?内的最确切的解答。
有穷自动机可用五元组(Q,V T,δ,q0,Q f)来描述,设有一有穷自动机M定义如下:V T={0,1},Q={q0,q1,q2},Q f={q2},δ的定义为:δ (q0,0)=q1δ (q1,0)=q2δ (q2,1)=q2δ (q2,0)=q2M是一个 A 有穷状态自动机,它所对应的状态转换图为 B ,它所能接受的语言可以用正则表达式表示为 C 。
The exercises of Chapter Three3.2 Given the grammar A →AA|(A)|εa. Describe the language it generates;b. Show that it is ambiguous.[Solution]:a. Generates a string of balanced parenthesis, including the empty string.b. parse trees of ():3.3 Given the grammarexp → exp addop term | termaddop → + | -term → term mulop factor| factormulop → *factor → ( exp ) | numberWrite down leftmost derivations, parse trees, and abstract syntax trees for the following expression:a. 3+4*5-6b. 3*(4-5+6)c. 3-(4+5*6)[Solution]: a. The leftmost derivations for the expression3+4*5-6:Exp => exp addop term =>exp addop term addop term =>term addop term addop term=> factor addop term addop term=>3 addop term addop term => 3 + term addop term=>3+term mulop factor addop term =>3+factor mulop factor addop term=>3+4 mulop factor addop term => 3+4* factor addop termA ( ) ε A AA A A ( ) ε ε=>3+4*5 addop term => 3+4*5-term=> 3+4*5-factor=>3+4*5-63.5 Write a grammar for Boolean expressions that includes the constants true and false, the operators and, or and not, and parentheses. Be sure to give or a lower precedence than and and and a lower precedence that not and to allow repeated not’s, as in the Boolean expression not not true. Also be sre your grammar is not ambiguous.[solution]bex p→bexp or A | AA→ A and B | BB→ not B | CC→ (bexp) | true | falseEx: not not trueboolExp → A→ B→ not B→ not not B→ not not C→ not not true3.8 Given the following grammarstatement→if-stmt | other | εif-stmt→ if ( exp ) statement else-partelse-part→ else statement | εexp→ 0 | 1a. Draw a parse tree for the stringif(0) if (1) other else else otherb. what is the purpose of the two else’s?The two else’s allow the programmer to associate an else clause with the outmost else, when two if statements are nested and the first does not have an else clause.c. Is similar code permissible in C? Explain.The grammar in C looks like:if-stmt→if ( exp ) statement | if (exp) statement else statement the way to override “dangling else”problem is to enclose the inner if statement in {}s. e.g. if (0) { if(1) other } else other.3.10 a. Translate the grammar of exercise 3.6 into EBNF.b. Draw syntax diagramms for the EBNF of part (a).[Solution]a. The original grammarlexp→atom|listatom→number|identifierlist→(lexp-seq)lexp-seq→lexp-seq lexp| lexpThe EBNF of the above grammar:lexp→atom|listatom→number|identifierlist→(lexp-seq)lexp-seq→lexp {lexp}b. The syntax diagramms for the above EBNF:3.12. Unary minuses can be added in several ways to the simple arithmetic expression grammar of Exercise 3.3. Revise the BNF for each of the cases that follow so that it satisfies the stated rule.a. At most one unary minus is allowed in each expression, and it must come at the beginning of an expression, so -2-3 is legal ( and evaluates to -5 ) and -2-(-3) is legal, but -2--3 is not.exp →exp addop term | termaddop →+ | -term → term mulop factor| factormulop →*factor →( exp) | (-exp) | number |b. At most one unary minus are allowed before a number or left parenthesis, so -2--3 is legal but --2 and -2---3 are not.exp →exp addop term | termaddop →+ | -term → term mulop factor| factormulop →*factor ( exp) | -(exp) | number | -numberc. Arbitrarily many unary minuses are allowed before numbers and left parentheses, so everything above is legal.3.19 In some languages ( Modula-2 and Ada are examples), a procedure declaration is expected to be terminated by syntax that includes the name of the procedure. For example, in Modular-2 a procedure is declared as follows:PROCEDURE P;BEGIN……END P;Note the use of the procedure name P alter the closing END. Can such a requirement be checked by a parser? Explain.[Answer]This requirement can not be handled as part of the grammar without making a new rule for each legal variable name, which makes it intractable for all practical purposes, even if variable names are restricted to a very short length. The parser will just check the structure, that an identifier follows the keyword PROCEDURE and an identifier also follows the keyword END, however checking that it is the same identifier is left for semantic analysis. See the discussion on pages 131-132 of your text.3.20 a. Write a regular expression that generate the same language as the following grammar:A→aA|B|εB→bB|Ab. Write a grammar that generates the same language as the following regularexpression:(a|c|ba|bc)*(b|ε)[Solution]a. The regular expression:(a|b)*b. The grammar:Step 1:A→BCB→aB|cB|baB|bcB|εC→b|εStep 2:A→Bb|BB→aB|cB|baB|bcB|ε。