You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapter10.textile
+16-25Lines changed: 16 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ the C pre-processor.
27
27
28
28
!images/ch_parser_build.png(Parser construction process)!
29
29
30
-
h3. Disecting `parse.y`
30
+
h3. Dissecting `parse.y`
31
31
32
32
Let's now look at `parse.y` in a bit more detail. The following figure presents
33
33
a rough outline of the contents of `parse.y`.
@@ -147,12 +147,12 @@ to represent them.
147
147
|Function definition|`defun definition function fndef`|
148
148
|Declarations|`declaration decl`|
149
149
150
-
In general, programming lanaguages tend to have the following symbol heiarchy.
150
+
In general, programming languages tend to have the following symbol hierarchy.
151
151
152
152
|Program element|Properties|
153
153
|Statement|Can not be combined with other symbols. A syntax tree trunk.|
154
154
|Expression|Can be combined with itself or be part of other
155
-
expressions. A syntax tree interal node.|
155
+
expressions. A syntax tree internal node.|
156
156
|Primary|An element which can not be further decomposed. A syntax tree leaf node.|
157
157
158
158
C function definitions and Java class definitions are examples of statements in
@@ -179,8 +179,8 @@ h3. Program structure
179
179
Now let's turn our attention to the grammer rules of `ruby`. Firstly,
180
180
`yacc` will begin by examining the first rule defined in `parse.y`, and as
181
181
we can see from the following table, this is `program`. From here we can see
182
-
the ruby grammar unfold and the existance of the `program stmt expr primary`
183
-
heierarchy mentioned earlier. However there is an extra rule here for `arg`.
182
+
the ruby grammar unfold and the existence of the `program stmt expr primary`
183
+
hierarchy mentioned earlier. However there is an extra rule here for `arg`.
184
184
Let's now take a look at this.
185
185
186
186
▼ `ruby` grammar (outline)
@@ -330,9 +330,9 @@ term : ';'
330
330
The initial `;` or `\n` of a `terms` can be followed by any number of `;` only; based on that, you might start thinking that if there are 2 or more consecutive newlines, it could cause a problem. Let's try and see what actually happens.
331
331
332
332
<pre class="emlist">
333
-
1 + 1 # 改行一つめ
334
-
# 改行二つめ
335
-
# 改行三つめ
333
+
1 + 1 # first newline
334
+
# second newline
335
+
# third newline
336
336
1 + 1
337
337
</pre>
338
338
@@ -343,17 +343,15 @@ Run that with `ruby -c`.
343
343
Syntax OK
344
344
</pre>
345
345
346
-
Strange, it worked! What actually happens is: consecutive newlines are discarded by the scanner, which passes on only the first newline in a series.
346
+
Strange, it worked! What actually happens is this: consecutive newlines are simply discarded by the scanner, which returns only the first newline in a series.
347
347
348
348
By the way, although we said that `program` is the same as `compstmt`, if that was really true, you would question why `compstmt` exists at all. Actually, the distinction is there only for execution of semantic actions. `program` exists to execute any semantic actions which should be done once in the processing of an entire program. If it was only a question of parsing, `program` could be omitted with no problems at all.
349
349
350
-
これを一般化すると、規則には見ための解析のために必要なものと、アクショ
351
-
ンを実行するために必要なものの二種類があるということになる。`stmts`のと
352
-
ころにある`none`もアクションのために必要な規則の一つで、空リストに対して
353
-
(`NODE*`型の)`NULL`ポインタを返すために用意されている。
350
+
To generalize this point, the grammar rules can be divided into 2 groups: those which are needed for parsing the program structure, and those which are needed for execution of semantic actions. The `none` rule which was mentioned earlier when talking about `stmts` is another one which exists for executing actions -- it's used to return a `NULL` pointer for an empty list of type `NODE*`.
354
351
355
352
h3. `stmt`
356
353
354
+
Next is `stmt`. *** We'll look into it a bit at a time.
357
355
次に文、`stmt`だ。`stmt`の規則はわりと量があるので、少しずつ見ていく
358
356
ことにする。
359
357
@@ -373,18 +371,11 @@ stmt : kALIAS fitem fitem
373
371
| klEND '{' compstmt '}'
374
372
</pre>
375
373
376
-
なんとなくわかる。最初にいくつかあるのは`alias`だし、
377
-
次が`undef`、その後にいくつか並ぶ「なんたら`_MOD`」は
378
-
modifier(修飾子)のことだろうから、後置系構文だと想像が付く。
374
+
Looking at that, somehow things start to make sense. The first few have `alias`, then `undef`, then the next few are all something followed by `_MOD` -- those should be statements with modifiers -- 後置系構文だと想像が付く。
`expr_value` and `primary_value` are grammar rules which exist to execute semantic actions. For example, `expr_value` represents an `expr` which has a value. Expressions which don't have values are `return` and `break`, as well as `if`-modifier statements, etc., which contain either `return` or `break`. For a detailed definition of what it means to "have a value", see chapter 12 『構文木の構築』. In the same way, `primary_value` is a `primary` which has a value.
386
377
387
-
`klBEGIN`と`klEND`は説明した通り`BEGIN`と`END`のこと。
378
+
Just as they say, `klBEGIN` and `klEND` represent `BEGIN` and `END`.
388
379
389
380
▼ `stmt`(2)
390
381
<pre class="longlist">
@@ -464,7 +455,7 @@ expr kAND expr
464
455
expr kOR expr
465
456
</pre>
466
457
467
-
`kAND`は「`and`」で`kOR`は「`or`」。この二つは制御構造としての役割があ
458
+
`kAND` is `and`, and `kOR` is `or`. この二つは制御構造としての役割があ
468
459
るので、`command_call`以上に「大きい」構文単位に入れなければならない。
469
460
そして`command_call`は`expr`にある。だから最低でも`expr`にしてやらない
470
461
とうまくいかない。例えば次のような使いかたが存在するのだが……
@@ -528,7 +519,7 @@ arg : lhs '=' arg
528
519
| primary
529
520
</pre>
530
521
531
-
規則の数は多いが、文法の複雑さは規則の数には比例しない。単に場合分けが
522
+
Although there are many rules here, the complexity of the grammar is not in proportion to the number of rules. 単に場合分けが
0 commit comments