
龙书的解答方案。
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
编译原理龙书答案 完整性高第二章2.2 Exercises for Section 2.2
2.2.1Consider the context-free grammar:
S -> S S + | S S * | a
展示如何使用该文法生成字符串“aa+a*”。构造该字符串的解析树。该文法生成什么语言?请给出您的理由。
答案:S -> S S * -> S S + S * -> a S + S * -> a a + S * -> a a + a *L = {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.
S -> 0 S 1 | 0 1
S -> + S S | - S S | a S
S -> ( S ) S | ε
答案:
L = {0n1n | n>=1}
L = {Prefix expression consisting of plus and minus signs}
L = {Matched brackets of arbitrary arrangement and nesting, includes ε}
L = {String has the same amount of a and b, includes ε}
2.2.3Which of the grammars in Exercise 2.2.2 are ambiguous?
答案:No, No, Yes, Yes, Yes
2.2.4Construct unambiguous context-free grammars for each of the following languages. In each case show that your grammar is correct.
Arithmetic expressions in postfix notation.
Left-associative lists of identifiers separated by commas.
Right-associative lists of identifiers separated by commas.
Arithmetic expressions of integers and identifiers with the four binary operators +, - , *, /.
答案:
1. E -> E E op | num (其中op代表运算符)
2. list -> list , id | id
3. list -> id , list | id
4. expr -> expr + term | expr - term | term term -> term * factor | term / factor | factor factor -> id | num | (expr)
5 . expr -> expr + term| expr - term|term term->term*unary|term/unary|unary unary->+factor|-factor factor->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?答案proveany string derived from the grammar can be considered to be a sequence consisting of 11, 1001 and 0,并且不以0开头。这个字符串的和是:sum= Σn (2^1+2^0)*n+ Σm (3*4^m) = Σn(3*4^n)+ Σm(9*4^m). It is obviously can divisible by three; No,Consider string 1010, it is divisible by three but cannot derived from this 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。答案via wikipedia: Roman_numerals via wikipedia,we can categorize the single noman numerals into four groups: I, II, III| I V| V, V I, V II, V III| I X then get production: digit->smallDigit|I V|V smallDigit|I XsmallDigit->I||II||III||εand we can find simple way to map roman to arabic numerals For example: XII => XII=>X+II=>X+II=>XII via upper two rules we can derive production:romanNum->thousand hundred ten digit thousand->M||MM||MMM||εhundred->smallHundred || C D || D smallHundred || C M smallHundred->C||CC||CCC||εten->smallTen || X L || L smallTen || X C smallTen->X||XX||XXX||εdigit->smallDigit || I V ||V smallDigit || I X smallDigit->I||II||III||ε
Exercises for Section 2.3
(略,此处省略了具体内容)
Exercises for Section 2.6
(略,此处省略了具体内容)
Exercises for Section 2.8
(略,此处省略了具体内容)
全部评论 (0)


