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
+26-30Lines changed: 26 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,13 +14,13 @@ h3. Parser construction
14
14
The parser is defined in the `yacc` source file `parse.y`, which `yacc` uses to
15
15
produce a working parser, `parse.c`.
16
16
17
-
Although one would expect `lex.c` to contain the scanner this is not the case.
17
+
Although one would expect `lex.c` to contain the scanner, this is not the case.
18
18
This file is created by `gperf`, taking the file `keywords` as input, and
19
19
defines the reserved word hashtable. This tool-generated `lex.c` is `#include`d
20
20
in (the also tool-generated) `parse.c`. The details of this process is somewhat
21
21
difficult to explain at this time, so we shall return to this later.
22
22
23
-
Figure 1 shows the parser construction process. For the benifit of those readers
23
+
Figure 1 shows the parser construction process. For the benefit of those readers
24
24
using Windows who may not be aware, the `mv` (move) command creates a new copy
25
25
of a file and removes the original. `cc` is, of course, the C compiler and `cpp`
26
26
the C pre-processor.
@@ -59,8 +59,8 @@ Previously we briefly mentioned the rules and user code sections. With this
59
59
chapter we begin to study the parser in some detail and so turn our attention to
60
60
these sections.
61
61
62
-
There is a considerable number of support functions defined in the user code
63
-
section, however roughly speaking they can be divided into the six parts
62
+
There are a considerable number of support functions defined in the user code
63
+
section, but roughly speaking, they can be divided into the six parts
64
64
previously mentioned. The following table shows where each of parts are
65
65
explained in this book.
66
66
@@ -103,7 +103,7 @@ symbol names were required.
103
103
104
104
h3. Important symbols
105
105
106
-
`parse.y` contains both grammar rules and actions, however for now I would like
106
+
`parse.y` contains both grammar rules and actions, however, for now I would like
107
107
to concentrate on the grammar rules alone. The script sample/exyacc.rb can be
108
108
used to extract the grammar rules from this file. Aside from this, running `yacc
109
109
-v` will create a logfile `y.output` which also contains the grammar rules,
@@ -130,7 +130,7 @@ only included the most important parts in this chapter.
130
130
131
131
Which symbols, then, are the most important? Symbols such as `program`, `expr`,
132
132
`stmt`,`primary`, `arg` etc. represent the more general grammatical elements of
133
-
a programming language and it is to these which we shall focus our attention.
133
+
a programming language and it is on these which we shall focus our attention.
134
134
The following table outlines these general elements and the symbol names used
135
135
to represent them.
136
136
@@ -157,15 +157,15 @@ expressions. A syntax tree internal node.|
157
157
158
158
C function definitions and Java class definitions are examples of statements in
159
159
other languages. An expression can be a procedure call, an arithmetic expression
160
-
etc. while a primary usually refers to a string literal or number. Some languages
160
+
etc., while a primary usually refers to a string literal or number. Some languages
161
161
do not contain all of these symbol types, however they generally contain some
162
162
kind of hierarchy of symbols such as `program`→`stmt`→`expr`→`primary`.
163
163
164
-
It is often the case that symbol types lower in this heirarchy can be promoted
164
+
It is often the case that symbol types lower in this hierarchy can be promoted
165
165
to higher levels and vice versa. For example, in C function calls are expressions
166
166
yet can be may also be statements.
167
167
168
-
Conversely, when surrounded in parenthesis expressions become primaries.
168
+
Conversely, when surrounded in parentheses, expressions become primaries.
169
169
170
170
Scoping rules differ considerably between programming languages. Consider
171
171
substitution. In C, the value of expressions can be used in substitutions
@@ -176,7 +176,7 @@ expressions. Ruby follows Lisp's design in this regard.
176
176
177
177
h3. Program structure
178
178
179
-
Now let's turn our attention to the grammer rules of `ruby`. Firstly,
179
+
Now let's turn our attention to the grammar 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
182
the ruby grammar unfold and the existence of the `program stmt expr primary`
@@ -283,7 +283,7 @@ output.
283
283
Syntax OK
284
284
</pre>
285
285
286
-
Although it might look surprising at first, yes you really can do this in Ruby!
286
+
Although it might look surprising at first, yes, you really can do this in Ruby!
287
287
288
288
The details of this are covered when we look at semantic analysis (in Chaper 12
289
289
"Syntax tree construction") however it is important to note there are exceptions
@@ -351,9 +351,7 @@ To generalize this point, the grammar rules can be divided into 2 groups: those
351
351
352
352
h3. `stmt`
353
353
354
-
Next is `stmt`. *** We'll look into it a bit at a time.
355
-
次に文、`stmt`だ。`stmt`の規則はわりと量があるので、少しずつ見ていく
356
-
ことにする。
354
+
Next is `stmt`. This one is rather involved, so we'll look into it a bit at a time.
357
355
358
356
▼ `stmt`(1)
359
357
<pre class="longlist">
@@ -371,11 +369,11 @@ stmt : kALIAS fitem fitem
371
369
| klEND '{' compstmt '}'
372
370
</pre>
373
371
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 -- 後置系構文だと想像が付く。
372
+
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 postposition modifiers, as you can imagine.
375
373
376
-
`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.
374
+
`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`, or `return`/`break` followed by a postposition modifier, such as an `if` clause. For a detailed definition of what it means to "have a value", see chapter 12, "Syntax Tree Construction". In the same way, `primary_value` is a `primary` which has a value.
377
375
378
-
Just as they say, `klBEGIN` and `klEND` represent `BEGIN` and `END`.
376
+
Just as the names say, `klBEGIN` and `klEND` represent `BEGIN` and `END`.
379
377
380
378
▼ `stmt`(2)
381
379
<pre class="longlist">
@@ -390,17 +388,15 @@ Just as they say, `klBEGIN` and `klEND` represent `BEGIN` and `END`.
390
388
</pre>
391
389
392
390
この規則は全部くくって見るのが正しい。
393
-
共通点は右辺に`command_call`が来る代入であることだ。
394
-
`command_call`は引数括弧を省略したメソッド呼び出しである。
395
-
新しく出てきた記号は以下に表を置いておくので対照しながら確かめてほしい。
396
-
397
-
|`lhs`|代入の左辺(Left Hand Side)|
398
-
|`mlhs`|多重代入の左辺(Multiple Left Hand Side)|
399
-
|`var_lhs`|代入の左辺、ただし変数系のみ(VARiable Left Hand Side)|
The common point is that they all have `command_call` on the right-hand side. `command_call` represents a method call with the parentheses omitted. The new symbols which are introduced here are explained in the following table. I hope you'll refer to the table as you check over each grammar rule.
392
+
393
+
|`lhs`|assignment left hand side|
394
+
|`mlhs`|assignment multiple left hand side|
395
+
|`var_lhs`|assignment left hand side, which must be a variable|
396
+
|`tOP_ASGN`|compound assignment operator like `+=` or `*=` (OPerator ASsiGN)|
397
+
|`aref_args`|argument to a `[]` method call (Array REFerence)|
398
+
|`tIDENTIFIER`|local variable identifier|
399
+
|`tCONSTANT`|constant identifier (with leading uppercase letter)|
404
400
|`tCOLON2`|`::`|
405
401
|`backref`|`$1 $2 $3...`|
406
402
@@ -519,7 +515,7 @@ arg : lhs '=' arg
519
515
| primary
520
516
</pre>
521
517
522
-
Although there are many rules here, the complexity of the grammar is not in proportion to the number of rules. 単に場合分けが
518
+
Although there are many rules here, the complexity of the grammar is not proportionate to the number of rules. 単に場合分けが
523
519
多いだけの文法は`yacc`にとっては非常に扱いやすく、むしろ規則の深さである
524
520
とか再帰のほうが複雑さに影響する。
525
521
@@ -565,7 +561,7 @@ arg: lhs '=' arg /* 1 */
565
561
566
562
h3. `primary`
567
563
568
-
`primary`は規則が多いので最初から分割して示す。
564
+
Because `primary` has a lot of grammar rules, we'll split them up and show them in parts.
0 commit comments