Skip to content

Commit

Permalink
Translation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fool2fish committed Dec 20, 2014
1 parent c4f2881 commit e3bb8eb
Showing 1 changed file with 144 additions and 100 deletions.
244 changes: 144 additions & 100 deletions ch04/4.2/4.2.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,115 @@
# 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\*
3.

![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、
Expand All @@ -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 sym­bols
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
<table>
<thead>
<tr>
<th>扩展文法表示</th>
<th>不带扩展的文法表示</th>
<th>extended grammar</th>
<th>not extended grammar</th>
</tr>
</thead>
<tbody>
Expand All @@ -154,73 +168,103 @@ S -> S S + | S S * | a
### 4.2.5
使用练习 4.2.4 中描述的括号表示法来简化如下的语句块和条件语句的文法。
Use the braces described in Exercise 4.2.4 to simplify the
following grammar for statement blocks and conditional statements:
stmt -> if expr then stmt else stmt
| if stmt them stmt
| begin stmtList end
stmtList -> stmt; stmtList | stmt
```
stmt -> if expr then stmt else stmt
| if stmt them stmt
| begin stmtList end
stmtList -> stmt; stmtList | stmt
```
#### 解答
#### Answer
stmt -> if expr then stmt [else stmt]
| begin stmtList end
stmtList -> stmt[; stmtList]
```
stmt -> if expr then stmt [else stmt]
| begin stmtList end
stmtList -> stmt [; stmtList]
```
### 4.2.6
扩展练习 4.2.4 的思想,使得产生式体中可以出现文法符号的任意正则表达式。证明这个扩展没有使得文法可以定义任何新的语言。
Extend the idea of Exercise 4.2.4 to allow any regular expres­sion
of grammar symbols in the body of a production. Show that this extension
does not allow grammars to define any new languages.
#### 证明
#### Proof
只要证明所有的正则文法均有等价的无扩展文法即可
Every regular grammar has a corresponding not extended grammar
### 4.2.7
### 4.2.7 !
如果不存在形如 S =\*=> wXy =\*=> wxy 的推导,那么文法符号 X 就被称为无用的。也就是说 X 不可能出现在任何句子的推导过程当中。
A grammar symbol X (terminal or nonterminal) is useless if
there is no derivation of the form S =\*=> wXy =\*=> wxy. That is, X can never
appear in the derivation of any sentence.
1. 给出一个算法,从一个文法中消除所有包含无用符号的产生式。
2. 将你的算法应用于以下文法:
1. Give an algorithm to eliminate from a grammar all productions containing useless symbols.
2. Apply your algorithm to the grammar:
S -> 0 | A
A -> AB
B -> 1
```
S -> 0 | A
A -> AB
B -> 1
````
### 4.2.8
图 4-7 中的文法可以生成单个数值标识符的声明,这些声明包含四种不同的、相互独立的数字性质。
The grammar in Fig. 4.7 generates declarations for a sin­gle
numerical identifier; these declarations involve four different, independent
properties of numbers.
stmt -> declare id optionList
optionList -> optionList option | ε
option -> mode | scale | precision | base
mode -> real | complex
scale -> fixed | floating
precision -> single | double
base -> binary | decimal
```
stmt -> declare id optionList
optionList -> optionList option | ε
option -> mode | scale | precision | base
mode -> real | complex
scale -> fixed | floating
precision -> single | double
base -> binary | decimal
```
1. 扩展图 4-7 中的文法,使得它可以允许 n 种选项 A_i,其中 n 是一个固定的数, i = 1, 2 , ..., n。选项 A_i 的取值可以是 a_i 或 b_i。你的文法发只能使用 O(n) 个文法符号,并且产生式的总长度页必须是 O(n) 的。
2. ! 图 4-7 中的文法和它在 1 中的扩展支持相互矛盾或冗余的声明,比如:
1. Generalize the grammar of Fig. 4.7 by allowing n options Ai, for some
fixed n and for i = 1,2... ,n, where Ai can be either ai or bi· Your
grammar should use only 0(n) grammar symbols and have a total length
of productions that is O(n).
2. ! The grammar of Fig. 4.7 and its generalization in part (a) allow declarations
that are contradictory and/or redundant, such as
declare foo real fixed real floating
我们可以要求这个语言的语法禁止这种声明。也就是说,由这个文法生成的每个声明中,n 种选项中的每一项都只有一个取值。如果我们这样做,那么对于任意给定的 n 值,合法声明的个数是有穷的。因此和任何有穷语法一样,合法声明组成的语言有一个文法(同时也有一个正则表达式)。最显而易见的文法是这样的:文法的开始符号对每个合法声明都是一个产生式,这样共有 n! 个产生式。该文法的产生式的总长度是 O(n*n!)。你必须做得更好:给出一个产生式总长度为 O(n2^n) 的文法。
We could insist that the syntax of the language forbid such declarations;
that is, every declaration generated by the grammar has exactly one value
for each of the n options. If we do, then for any fixed n there is only a finite
number of legal declarations. The language of legal declarations thus has
a grammar (and also a regular expression), as any finite language does.
The obvious grammar, in which the start symbol has a production for
every legal declaration has n! productions and a total production length
of O(n x n!). You must do better: a total production length that is O(n2^n)
3. !! 说明对于任何满足 2 中的要求的文法,其产生式的总长度至少是 2^n 。
4. 我们可以通过程序设计语言的语法来保证声明中的选项无冗余性、无矛盾。对于这个文法的可行性,本题 3 的结论说明了什么问题?
3. !! Show that any grammar for part (b) must have a total production length of at least 2n.
4. What does part (c) say about the feasibility of enforcing nonredundancy
and noncontradiction among options in declarations via the syntax of the programming language?
#### 解答
#### Answer
1、
stmt -> declare id optionList
optionList -> optionList option | ε
option -> A_1 | A_2 | … | A_n
A_1 -> a_1 | b_1
A_2 -> a_2 | b_2
A_n -> a_n | b_n
```
stmt -> declare id optionList
optionList -> optionList option | ε
option -> A_1 | A_2 | … | A_n
A_1 -> a_1 | b_1
A_2 -> a_2 | b_2
A_n -> a_n | b_n
```
显然这个文法符号的个数是 O(n) 的
Expand Down

0 comments on commit e3bb8eb

Please sign in to comment.