Skip to content

Commit e3bb8eb

Browse files
committed
Translation.
1 parent c4f2881 commit e3bb8eb

File tree

1 file changed

+144
-100
lines changed

1 file changed

+144
-100
lines changed

ch04/4.2/4.2.md

Lines changed: 144 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,115 @@
1-
# 4.2 节的练习
1+
# Exercises for Section 4.2
22

33
### 4.2.1
44

5-
考虑上下文无关文法:
5+
Consider the context-free grammar:
66

7+
```
78
S -> S S + | S S * | a
9+
```
810

9-
以及串 aa+a*
11+
and the string aa + a*.
1012

11-
1. 给出这个串的一个最左推导
12-
2. 给出这个串的一个最右推导
13-
3. 给出这个串的一个语法分析树
14-
4. ! 这个文法是否为二义性的?证明你的回答。
15-
5. ! 描述这个语法生成的语言。
13+
1. Give a leftmost derivation for the string.
14+
2. Give a rightmost derivation for the string.
15+
3. Give a parse tree for the string.
16+
4. ! Is the grammar ambiguous or unambiguous? Justify your answer.
17+
5. ! Describe the language generated by this grammar.
1618

17-
#### 解答
19+
#### Answer
1820

1921
1. S =lm=> SS\* => SS+S\* => aS+S\* => aa+S\* => aa+a\*
2022
2. S =rm=> SS\* => Sa\* => SS+a\* => Sa+a\* => aa+a\*
2123
3.
2224

2325
![4 2 1](https://f.cloud.github.com/assets/340282/469058/c08b4f9c-b6af-11e2-8236-f79c6a56215a.gif)
2426

25-
4. 非二义
26-
5. 包含加法和乘法的后缀表达式
27+
4. Unambiguous
28+
5. The set of all postfix expressions consist of addition and multiplication
2729

2830
### 4.2.2
2931

30-
对下列的每一对文法和串重复练习 4.2.1 。
32+
Repeat Exercise 4 . 2 . 1 for each of the following grammars and strings:
3133

32-
1. S -> 0 S 1 | 0 1 和串 000111
33-
2. S -> + S S | \* S S | a 和串 +\*aaa
34-
3. ! S -> S (S) S | ε 和串 (()())
35-
4. ! S -> S + S | S S | (S) | S \* | a 和串 (a+a)\*a
36-
5. ! S -> (L) | a 以及 L -> L, S | S 和串 ((a,a),a,(a))
37-
6. !! S -> a S b S | b S a S | ε 和串 aabbab
38-
7. 下面的布尔表达式对应的文法:
34+
1. S -> 0 S 1 | 0 1 with string 00011l.
35+
2. S -> + S S | \* S S | a with string + \* aaa.
36+
3. ! S -> S (S) S | ε with string (()())
37+
4. ! S -> S + S | S S | (S) | S \* | a with string (a+a)\*a
38+
5. ! S -> (L) | a 以及 L -> L, S | S with string ((a,a),a,(a))
39+
6. !! S -> a S b S | b S a S | ε with string aabbab
40+
7. The following grammar for boolean expressions:
3941

40-
bexpr -> bexpr or bterm | bterm
41-
bterm -> bterm and bfactor | bfactor
42-
bfactor -> not bfactor | (bexpr) | true | false
42+
```
43+
bexpr -> bexpr or bterm | bterm
44+
bterm -> bterm and bfactor | bfactor
45+
bfactor -> not bfactor | (bexpr) | true | false
46+
```
4347
44-
#### 解答
45-
46-
1、
48+
#### Answer
4749
4850
1. S =lm=> 0S1 => 00S11 => 000111
4951
2. S =rm=> 0S1 => 00S11 => 000111
50-
3.
51-
4. 非二义
52-
5. 前导n个连续0,后跟n个连续1的串
52+
3. Omit
53+
4. Unambiguous
54+
5. The set of all strings of 0s and followed by an equal number of 1s
5355
5456
2、
5557
5658
1. S =lm=> +SS => +\*SSS => +\*aSS => +\*aaS => +\*aaa
5759
2. S =rm=> +SS => +Sa => +\*SSa => +\*Saa => +\*aaa
58-
3.
59-
4. 非二义
60-
5. 包含加法和乘法的前缀表达式
60+
3. Omit
61+
4. Unambiguous
62+
5. The set of all prefix expressions consist of addition and multiplication.
6163
6264
3、
6365
6466
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 => (()())
6567
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(()()) => (()())
66-
3.
67-
4. 二义
68-
5. 所有对称的括号串
68+
3. Omit
69+
4. Ambiguous
70+
5. The set of all strings of symmetrical parentheses
6971
7072
4、
7173
7274
1. S =lm=> SS => S\*S => (S)\*S => (S+S)\*S => (a+S)\*S => (a+a)\*S => (a+a)\*a
7375
2. S =rm=> SS => Sa => S\*a => (S)\*a => (S+S)\*a => (S+a)\*a => (a+a)\*a
74-
3.
75-
4. 二义
76-
5. 由加号、乘号和字符a和对称的括号组成的串,且加号不在开头和结尾位置,乘号不在开头位置
76+
3. Omit
77+
4. Ambiguous
78+
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
7779
7880
5、
7981
8082
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))
8183
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))
82-
3.
83-
4. 非二义
84-
5. 类似于python中的元组
84+
3. Omit
85+
4. Unambiguous
86+
5. Something like tuple in Python
8587
8688
6、
8789
8890
1. S =lm=> aSbS => aaSbSbS => aabSbS => aabbS => aabbaSbS => aabbabS => aabbab
8991
2. S =rm=> aSbS => aSbaSbS => aSbaSb => aSbab => aaSbSbab => aaSbbab => aabbab
90-
3.
91-
4. 二义
92-
5. 数量相同的a和b组成的串
92+
3. Omit
93+
4. Ambiguous
94+
5. The set of all strings of 'a's and 'b's of the equal number of 'a's and 'b's
9395
94-
7、 非二义,该文法生成布尔表达式
96+
7、 Unambiguous, boolean expression
9597
9698
### 4.2.3
9799
98-
为下面的语言设计文法
100+
Design grammars for the following languages:
99101
100-
1. 所有由 0 和 1 组成的并且每个 0 之后至少跟着一个 1 的串的集合。
101-
2. ! 所有由 0 和 1 组成的回文集合。
102-
3. ! 所有由 0 和 1 组成的具有相同多个 0 和 1 的串的集合。
103-
4. !! 所有由 0 和 1 组成的并且 0 的个数和 1 的个数不同的串的集合。
104-
5. ! 所有由 0 和 1 组成的且不包含子串 011 的串的集合。
105-
6. !! 所有由 0 和 1 组成的形如 xy 的串的集合,其中 x<>y 且 x 和 y 等长。
102+
1. The set of all strings of 0s and 1s such that every 0 is immediately followed
103+
by at least one 1.
104+
2. ! The set of all strings of 0s and 1s that are palindromes; that is, the string
105+
reads the same backward as forward.
106+
3. ! The set of all strings of 0s and 1s with an equal number of 0s and 1s.
107+
4. !! The set of all strings of 0s and 1s with an unequal number of 0s and 1s.
108+
5. ! The set of all strings of 0s and as in which 011 does not appear as a
109+
substring.
110+
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.
106111
107-
#### 解答
112+
#### Answer
108113
109114
1、
110115
@@ -124,20 +129,29 @@ S -> S S + | S S * | a
124129
125130
### 4.2.4
126131
127-
有一个常用的扩展的文法表示方法。在这个表示方法中,产生式体中的方括号和花括号是元符号,且具有如下含义:
132+
There is an extended grammar notation in common use.
133+
In this notation, square and curly braces in production bodies are metasymbols
134+
(like -> or |) with the following meanings:
128135
129-
1. 一个或多个文法符号两边的方括号表示这些构造是可选的。因此,产生式 A -> X\[Y\]Z 和两个产生式 A -> XYZ 及 A -> XZ 具有相同的效果。
130-
2. 一个或多个文法符号两边的花括号表示这些符号可以重复任意多次(包括零次)。因此, A -> X{YZ} 和如下的无穷产生式序列具有相同的效果: A -> X, A -> XYZ, A -> XYZYZ, ... 等等。
136+
1. Square braces around a grammar symbol or symbols denotes that these
137+
constructs are optional. Thus, production A -> X\[Y\]Z has the same
138+
effect as the two productions A -> XYZ and A -> XZ.
139+
2. Curly braces around a grammar symbol or symbols says that these sym­bols
140+
may be repeated any number of times, including zero times. Thus,
141+
A -> X{YZ} has the same effect as the infinite sequence of productions
142+
A -> X, A -> XYZ, A -> XYZYZ, and so on.
131143
132-
正明这两个扩展并没有增加文法功能。也就是说,由带有这些扩展表示的文法生成的任何语言都可以由一个不带扩展表示的文法生成。
144+
Show that these two extensions do not add power to grammars; that is, any
145+
language that can be generated by a grammar with these extensions can be
146+
generated by a grammar without the extensions.
133147
134-
#### 证明
148+
#### Proof
135149
136150
<table>
137151
<thead>
138152
<tr>
139-
<th>扩展文法表示</th>
140-
<th>不带扩展的文法表示</th>
153+
<th>extended grammar</th>
154+
<th>not extended grammar</th>
141155
</tr>
142156
</thead>
143157
<tbody>
@@ -154,73 +168,103 @@ S -> S S + | S S * | a
154168
155169
### 4.2.5
156170
157-
使用练习 4.2.4 中描述的括号表示法来简化如下的语句块和条件语句的文法。
171+
Use the braces described in Exercise 4.2.4 to simplify the
172+
following grammar for statement blocks and conditional statements:
158173
159-
stmt -> if expr then stmt else stmt
160-
| if stmt them stmt
161-
| begin stmtList end
162-
stmtList -> stmt; stmtList | stmt
174+
```
175+
stmt -> if expr then stmt else stmt
176+
| if stmt them stmt
177+
| begin stmtList end
178+
stmtList -> stmt; stmtList | stmt
179+
```
163180
164-
#### 解答
181+
#### Answer
165182
166-
stmt -> if expr then stmt [else stmt]
167-
| begin stmtList end
168-
stmtList -> stmt[; stmtList]
183+
```
184+
stmt -> if expr then stmt [else stmt]
185+
| begin stmtList end
186+
stmtList -> stmt [; stmtList]
187+
```
169188
170189
### 4.2.6
171190
172-
扩展练习 4.2.4 的思想,使得产生式体中可以出现文法符号的任意正则表达式。证明这个扩展没有使得文法可以定义任何新的语言。
191+
Extend the idea of Exercise 4.2.4 to allow any regular expres­sion
192+
of grammar symbols in the body of a production. Show that this extension
193+
does not allow grammars to define any new languages.
173194
174-
#### 证明
195+
#### Proof
175196
176-
只要证明所有的正则文法均有等价的无扩展文法即可
197+
Every regular grammar has a corresponding not extended grammar
177198
178-
### 4.2.7
199+
### 4.2.7 !
179200
180-
如果不存在形如 S =\*=> wXy =\*=> wxy 的推导,那么文法符号 X 就被称为无用的。也就是说 X 不可能出现在任何句子的推导过程当中。
201+
A grammar symbol X (terminal or nonterminal) is useless if
202+
there is no derivation of the form S =\*=> wXy =\*=> wxy. That is, X can never
203+
appear in the derivation of any sentence.
181204
182-
1. 给出一个算法,从一个文法中消除所有包含无用符号的产生式。
183-
2. 将你的算法应用于以下文法:
205+
1. Give an algorithm to eliminate from a grammar all productions containing useless symbols.
206+
2. Apply your algorithm to the grammar:
184207
185-
S -> 0 | A
186-
A -> AB
187-
B -> 1
208+
```
209+
S -> 0 | A
210+
A -> AB
211+
B -> 1
212+
````
188213
189214
### 4.2.8
190215
191-
图 4-7 中的文法可以生成单个数值标识符的声明,这些声明包含四种不同的、相互独立的数字性质。
216+
The grammar in Fig. 4.7 generates declarations for a sin­gle
217+
numerical identifier; these declarations involve four different, independent
218+
properties of numbers.
192219
193-
stmt -> declare id optionList
194-
optionList -> optionList option | ε
195-
option -> mode | scale | precision | base
196-
mode -> real | complex
197-
scale -> fixed | floating
198-
precision -> single | double
199-
base -> binary | decimal
220+
```
221+
stmt -> declare id optionList
222+
optionList -> optionList option | ε
223+
option -> mode | scale | precision | base
224+
mode -> real | complex
225+
scale -> fixed | floating
226+
precision -> single | double
227+
base -> binary | decimal
228+
```
200229
201-
1. 扩展图 4-7 中的文法,使得它可以允许 n 种选项 A_i,其中 n 是一个固定的数, i = 1, 2 , ..., n。选项 A_i 的取值可以是 a_i 或 b_i。你的文法发只能使用 O(n) 个文法符号,并且产生式的总长度页必须是 O(n) 的。
202-
2. ! 图 4-7 中的文法和它在 1 中的扩展支持相互矛盾或冗余的声明,比如:
230+
1. Generalize the grammar of Fig. 4.7 by allowing n options Ai, for some
231+
fixed n and for i = 1,2... ,n, where Ai can be either ai or bi· Your
232+
grammar should use only 0(n) grammar symbols and have a total length
233+
of productions that is O(n).
234+
235+
2. ! The grammar of Fig. 4.7 and its generalization in part (a) allow declarations
236+
that are contradictory and/or redundant, such as
203237
204238
declare foo real fixed real floating
205239
206-
我们可以要求这个语言的语法禁止这种声明。也就是说,由这个文法生成的每个声明中,n 种选项中的每一项都只有一个取值。如果我们这样做,那么对于任意给定的 n 值,合法声明的个数是有穷的。因此和任何有穷语法一样,合法声明组成的语言有一个文法(同时也有一个正则表达式)。最显而易见的文法是这样的:文法的开始符号对每个合法声明都是一个产生式,这样共有 n! 个产生式。该文法的产生式的总长度是 O(n*n!)。你必须做得更好:给出一个产生式总长度为 O(n2^n) 的文法。
240+
We could insist that the syntax of the language forbid such declarations;
241+
that is, every declaration generated by the grammar has exactly one value
242+
for each of the n options. If we do, then for any fixed n there is only a finite
243+
number of legal declarations. The language of legal declarations thus has
244+
a grammar (and also a regular expression), as any finite language does.
245+
The obvious grammar, in which the start symbol has a production for
246+
every legal declaration has n! productions and a total production length
247+
of O(n x n!). You must do better: a total production length that is O(n2^n)
207248
208-
3. !! 说明对于任何满足 2 中的要求的文法,其产生式的总长度至少是 2^n 。
209-
4. 我们可以通过程序设计语言的语法来保证声明中的选项无冗余性、无矛盾。对于这个文法的可行性,本题 3 的结论说明了什么问题?
249+
3. !! Show that any grammar for part (b) must have a total production length of at least 2n.
250+
4. What does part (c) say about the feasibility of enforcing nonredundancy
251+
and noncontradiction among options in declarations via the syntax of the programming language?
210252
211-
#### 解答
253+
#### Answer
212254
213255
1、
214256
215-
stmt -> declare id optionList
216-
optionList -> optionList option | ε
217-
option -> A_1 | A_2 | … | A_n
218-
A_1 -> a_1 | b_1
219-
A_2 -> a_2 | b_2
220-
221-
A_n -> a_n | b_n
257+
```
258+
stmt -> declare id optionList
259+
optionList -> optionList option | ε
260+
option -> A_1 | A_2 | … | A_n
261+
A_1 -> a_1 | b_1
262+
A_2 -> a_2 | b_2
263+
264+
A_n -> a_n | b_n
265+
```
266+
222267
223-
显然这个文法符号的个数是 O(n) 的
224268
225269
226270

0 commit comments

Comments
 (0)