diff --git a/ch04/4.2/4.2.md b/ch04/4.2/4.2.md index 87b28efd..22472c49 100644 --- a/ch04/4.2/4.2.md +++ b/ch04/4.2/4.2.md @@ -1,20 +1,22 @@ -# 4.2 节的练习 +# Exercises for Section 4.2 ### 4.2.1 -考虑上下文无关文法: +Consider the context-free grammar: +``` S -> S S + | S S * | a +``` -以及串 aa+a* 。 +and the string aa + a*. -1. 给出这个串的一个最左推导 -2. 给出这个串的一个最右推导 -3. 给出这个串的一个语法分析树 -4. ! 这个文法是否为二义性的?证明你的回答。 -5. ! 描述这个语法生成的语言。 +1. Give a leftmost derivation for the string. +2. Give a rightmost derivation for the string. +3. Give a parse tree for the string. +4. ! Is the grammar ambiguous or unambiguous? Justify your answer. +5. ! Describe the language generated by this grammar. -#### 解答 +#### Answer 1. S =lm=> SS\* => SS+S\* => aS+S\* => aa+S\* => aa+a\* 2. S =rm=> SS\* => Sa\* => SS+a\* => Sa+a\* => aa+a\* @@ -22,89 +24,92 @@ S -> S S + | S S * | a ![4 2 1](https://f.cloud.github.com/assets/340282/469058/c08b4f9c-b6af-11e2-8236-f79c6a56215a.gif) -4. 非二义 -5. 包含加法和乘法的后缀表达式 +4. Unambiguous +5. The set of all postfix expressions consist of addition and multiplication ### 4.2.2 -对下列的每一对文法和串重复练习 4.2.1 。 +Repeat Exercise 4 . 2 . 1 for each of the following grammars and strings: -1. S -> 0 S 1 | 0 1 和串 000111 -2. S -> + S S | \* S S | a 和串 +\*aaa -3. ! S -> S (S) S | ε 和串 (()()) -4. ! S -> S + S | S S | (S) | S \* | a 和串 (a+a)\*a -5. ! S -> (L) | a 以及 L -> L, S | S 和串 ((a,a),a,(a)) -6. !! S -> a S b S | b S a S | ε 和串 aabbab -7. 下面的布尔表达式对应的文法: +1. S -> 0 S 1 | 0 1 with string 00011l. +2. S -> + S S | \* S S | a with string + \* aaa. +3. ! S -> S (S) S | ε with string (()()) +4. ! S -> S + S | S S | (S) | S \* | a with string (a+a)\*a +5. ! S -> (L) | a 以及 L -> L, S | S with string ((a,a),a,(a)) +6. !! S -> a S b S | b S a S | ε with string aabbab +7. The following grammar for boolean expressions: - bexpr -> bexpr or bterm | bterm - bterm -> bterm and bfactor | bfactor - bfactor -> not bfactor | (bexpr) | true | false + ``` + bexpr -> bexpr or bterm | bterm + bterm -> bterm and bfactor | bfactor + bfactor -> not bfactor | (bexpr) | true | false + ``` -#### 解答 - -1、 +#### Answer 1. S =lm=> 0S1 => 00S11 => 000111 2. S =rm=> 0S1 => 00S11 => 000111 -3. 略 -4. 非二义 -5. 前导n个连续0,后跟n个连续1的串 +3. Omit +4. Unambiguous +5. The set of all strings of 0s and followed by an equal number of 1s 2、 1. S =lm=> +SS => +\*SSS => +\*aSS => +\*aaS => +\*aaa 2. S =rm=> +SS => +Sa => +\*SSa => +\*Saa => +\*aaa -3. 略 -4. 非二义 -5. 包含加法和乘法的前缀表达式 +3. Omit +4. Unambiguous +5. The set of all prefix expressions consist of addition and multiplication. 3、 1. S =lm=> S(S)S => (S)S => (S(S)S)S => ((S)S)S => (()S)S => (()S(S)S)S => (()(S)S)S => (()()S)S => (()())S => (()()) 2. S =rm=> S(S)S => S(S) => S(S(S)S) => S(S(S)) => S(S()) => S(S(S)S()) => S(S(S)()) => S(S()()) => S(()()) => (()()) -3. 略 -4. 二义 -5. 所有对称的括号串 +3. Omit +4. Ambiguous +5. The set of all strings of symmetrical parentheses 4、 1. S =lm=> SS => S\*S => (S)\*S => (S+S)\*S => (a+S)\*S => (a+a)\*S => (a+a)\*a 2. S =rm=> SS => Sa => S\*a => (S)\*a => (S+S)\*a => (S+a)\*a => (a+a)\*a -3. 略 -4. 二义 -5. 由加号、乘号和字符a和对称的括号组成的串,且加号不在开头和结尾位置,乘号不在开头位置 +3. Omit +4. Ambiguous +5. The set of all string of plus, mupplication, 'a' and symmetrical parentheses, and plus is not the beginning and end of the position, multiplication is not the beginning of the position 5、 1. S =lm=> (L) => (L, S) => (L, S, S) => ((S), S, S) => ((L), S, S) => ((L, S), S, S) => ((S, S), S, S) => ((a, S), S, S) => ((a, a), S, S) => ((a, a), a, S) => ((a, a), a, (L)) => ((a, a), a, (S)) => ((a, a), a, (a)) 2. S =rm=> (L) => (L, S) => (L, (L)) => (L, (a)) => (L, S, (a)) => (L, a, (a)) => (S, a, (a)) => ((L), a, (a)) => ((L, S), a, (a)) => ((S, S), a, (a)) => ((S, a), a, (a)) => ((a, a), a, (a)) -3. 略 -4. 非二义 -5. 类似于python中的元组 +3. Omit +4. Unambiguous +5. Something like tuple in Python 6、 1. S =lm=> aSbS => aaSbSbS => aabSbS => aabbS => aabbaSbS => aabbabS => aabbab 2. S =rm=> aSbS => aSbaSbS => aSbaSb => aSbab => aaSbSbab => aaSbbab => aabbab -3. 略 -4. 二义 -5. 数量相同的a和b组成的串 +3. Omit +4. Ambiguous +5. The set of all strings of 'a's and 'b's of the equal number of 'a's and 'b's -7、 非二义,该文法生成布尔表达式 +7、 Unambiguous, boolean expression ### 4.2.3 -为下面的语言设计文法 +Design grammars for the following languages: -1. 所有由 0 和 1 组成的并且每个 0 之后至少跟着一个 1 的串的集合。 -2. ! 所有由 0 和 1 组成的回文集合。 -3. ! 所有由 0 和 1 组成的具有相同多个 0 和 1 的串的集合。 -4. !! 所有由 0 和 1 组成的并且 0 的个数和 1 的个数不同的串的集合。 -5. ! 所有由 0 和 1 组成的且不包含子串 011 的串的集合。 -6. !! 所有由 0 和 1 组成的形如 xy 的串的集合,其中 x<>y 且 x 和 y 等长。 +1. The set of all strings of 0s and 1s such that every 0 is immediately followed +by at least one 1. +2. ! The set of all strings of 0s and 1s that are palindromes; that is, the string +reads the same backward as forward. +3. ! The set of all strings of 0s and 1s with an equal number of 0s and 1s. +4. !! The set of all strings of 0s and 1s with an unequal number of 0s and 1s. +5. ! The set of all strings of 0s and as in which 011 does not appear as a +substring. +6. !! The set of all strings of 0s and 1s of the form xy, where x<>y and x and y are of the same length. -#### 解答 +#### Answer 1、 @@ -124,20 +129,29 @@ S -> S S + | S S * | a ### 4.2.4 -有一个常用的扩展的文法表示方法。在这个表示方法中,产生式体中的方括号和花括号是元符号,且具有如下含义: +There is an extended grammar notation in common use. +In this notation, square and curly braces in production bodies are metasymbols +(like -> or |) with the following meanings: -1. 一个或多个文法符号两边的方括号表示这些构造是可选的。因此,产生式 A -> X\[Y\]Z 和两个产生式 A -> XYZ 及 A -> XZ 具有相同的效果。 -2. 一个或多个文法符号两边的花括号表示这些符号可以重复任意多次(包括零次)。因此, A -> X{YZ} 和如下的无穷产生式序列具有相同的效果: A -> X, A -> XYZ, A -> XYZYZ, ... 等等。 +1. Square braces around a grammar symbol or symbols denotes that these +constructs are optional. Thus, production A -> X\[Y\]Z has the same +effect as the two productions A -> XYZ and A -> XZ. +2. Curly braces around a grammar symbol or symbols says that these symbols +may be repeated any number of times, including zero times. Thus, +A -> X{YZ} has the same effect as the infinite sequence of productions +A -> X, A -> XYZ, A -> XYZYZ, and so on. -正明这两个扩展并没有增加文法功能。也就是说,由带有这些扩展表示的文法生成的任何语言都可以由一个不带扩展表示的文法生成。 +Show that these two extensions do not add power to grammars; that is, any +language that can be generated by a grammar with these extensions can be +generated by a grammar without the extensions. -#### 证明 +#### Proof
扩展文法表示 | -不带扩展的文法表示 | +extended grammar | +not extended grammar |
---|