1- $comment(-* - coding: utf-8 -* - vim: set encoding=utf-8:)$
1+ ---
2+ layout : default
3+ title : Classes and modules
4+ ---
5+
26Translated by Robert GRAVINA
37
48h1. Chapter 10: Parser
@@ -28,7 +32,7 @@ h3. Disecting `parse.y`
2832Let's now look at ` parse.y ` in a bit more detail. The following figure presents
2933a rough outline of the contents of ` parse.y ` .
3034
31- ▼ parse.y
35+ ▼ parse.y
3236<pre class =" longlist " >
3337%{
3438header
@@ -124,10 +128,10 @@ compstmt : stmts opt_terms
124128The output is quite long - over 450 lines of grammar rules - and as such I have
125129only included the most important parts in this chapter.
126130
127- Which symbols, then, are the most important? Symbols such as ` program ` , ` expr ` ,
128- ` stmt ` ,` primary ` , ` arg ` etc. represent the more general grammatical elements of
129- a programming language and it is to these which we shall focus our attention.
130- The following table outlines these general elements and the symbol names used
131+ Which symbols, then, are the most important? Symbols such as ` program ` , ` expr ` ,
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.
134+ The following table outlines these general elements and the symbol names used
131135to represent them.
132136
133137| Syntax element| Relevant symbol names|
@@ -147,36 +151,36 @@ In general, programming lanaguages tend to have the following symbol heiarchy.
147151
148152| Program element| Properties|
149153| Statement| Can not be combined with other symbols. A syntax tree trunk.|
150- |Expression|Can be combined with itself or be part of other
154+ |Expression|Can be combined with itself or be part of other
151155expressions. A syntax tree interal node.|
152- | Primary| An element which can not be further decomposed. A syntax tree leaf node.|
156+ | Primary| An element which can not be further decomposed. A syntax tree leaf node.|
153157
154- C function definitions and Java class definitions are examples of statements in
155- other languages. An expression can be a procedure call, an arithmetic expression
156- etc. while a primary usually refers to a string literal or number. Some languages
157- do not contain all of these symbol types, however they generally contain some
158+ C function definitions and Java class definitions are examples of statements in
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
161+ do not contain all of these symbol types, however they generally contain some
158162kind of hierarchy of symbols such as ` program ` →` stmt ` →` expr ` →` primary ` .
159163
160- It is often the case that symbol types lower in this heirarchy can be promoted
161- to higher levels and vice versa. For example, in C function calls are expressions
164+ It is often the case that symbol types lower in this heirarchy can be promoted
165+ to higher levels and vice versa. For example, in C function calls are expressions
162166yet can be may also be statements.
163167
164168Conversely, when surrounded in parenthesis expressions become primaries.
165169
166- Scoping rules differ considerably between programming languages. Consider
167- substitution. In C, the value of expressions can be used in substitutions
170+ Scoping rules differ considerably between programming languages. Consider
171+ substitution. In C, the value of expressions can be used in substitutions
168172whereas in Pascal substitution occurs only at the statement level. Also,
169- function and class definitions are typically statements however in languages
170- such as Lisp and Scheme, since everything is an expression, they too are
173+ function and class definitions are typically statements however in languages
174+ such as Lisp and Scheme, since everything is an expression, they too are
171175expressions. Ruby follows Lisp's design in this regard.
172176
173177h3. Program structure
174178
175- Now let's turn our attention to the grammer rules of ` ruby ` . Firstly,
176- ` yacc ` will begin by examining the first rule defined in ` parse.y ` , and as
177- we can see from the following table, this is ` program ` . From here we can see
178- the ruby grammar unfold and the existance of the ` program stmt expr primary `
179- heierarchy mentioned earlier. However there is an extra rule here for ` arg ` .
179+ Now let's turn our attention to the grammer rules of ` ruby ` . Firstly,
180+ ` yacc ` will begin by examining the first rule defined in ` parse.y ` , and as
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 ` .
180184Let's now take a look at this.
181185
182186▼ ` ruby ` grammar (outline)
@@ -222,9 +226,9 @@ primary : literal
222226 | kRETRY
223227</pre >
224228
225- If you look at each of the final alternatives for each of the rules you should
229+ If you look at each of the final alternatives for each of the rules you should
226230be able to clearly make out a hierarchy of ` program ` →` stmt ` →` expr ` →` arg ` →
227- ` primary ` .
231+ ` primary ` .
228232
229233I'd like to focus on the rule for ` primary ` .
230234
@@ -235,11 +239,11 @@ primary : literal
235239 | tLPAREN_ARG expr ')' /* here */
236240</pre >
237241
238- The name ` tLPAREN_ARG ` comes from ` t ` for terminal symbol, ` L ` for left and
239- ` PAREN ` for parentheses - it is the open parenthesis. Why this isn't ` '(' `
240- is covered in the next section "Context-dependent scanner". The purpose of this
241- rule is demote an ` expr ` to a ` primary ` , and is shown in Figure 2. This creates
242- a cycle which can the seen in Figure 2, and the arrow shows how this rule is
242+ The name ` tLPAREN_ARG ` comes from ` t ` for terminal symbol, ` L ` for left and
243+ ` PAREN ` for parentheses - it is the open parenthesis. Why this isn't ` '(' `
244+ is covered in the next section "Context-dependent scanner". The purpose of this
245+ rule is demote an ` expr ` to a ` primary ` , and is shown in Figure 2. This creates
246+ a cycle which can the seen in Figure 2, and the arrow shows how this rule is
243247reduced during parsing.
244248
245249!images/ch_parser_exprloop.png(` expr ` demotion)!
@@ -253,14 +257,14 @@ primary : literal
253257 | tLPAREN compstmt ')' /* here */
254258</pre >
255259
256- A ` compstmt ` , which represents code for an entire program, can be demoted to
260+ A ` compstmt ` , which represents code for an entire program, can be demoted to
257261a ` primary ` with this rule. The next figure illustrates this rule in action.
258262
259263!images/ch_parser_progloop.png(` program ` の縮退)!
260264
261- This means that for any syntax element in Ruby, if we surround it with
262- parenthesis it will become a ` primary ` and can be passed as an argument to a
263- function, be used as the right hand side of an expression etc. It helps to
265+ This means that for any syntax element in Ruby, if we surround it with
266+ parenthesis it will become a ` primary ` and can be passed as an argument to a
267+ function, be used as the right hand side of an expression etc. It helps to
264268see an example of this to grasp what this truly means.
265269
266270<pre class =" emlist " >
@@ -271,7 +275,7 @@ p((if true then nil else nil end))
271275p((1 + 1 * 1 ** 1 - 1 / 1 ^ 1))
272276</pre >
273277
274- If we invoke ` ruby ` with the ` -c ` option (syntax check), we get the following
278+ If we invoke ` ruby ` with the ` -c ` option (syntax check), we get the following
275279output.
276280
277281<pre class =" screen " >
@@ -281,11 +285,11 @@ Syntax OK
281285
282286Although it might look surprising at first, yes you really can do this in Ruby!
283287
284- The details of this are covered when we look at semantic analysis (in Chaper 12
285- "Syntax tree construction") however it is important to note there are exceptions
286- to this rule. For example passing a ` return ` statement as an argument to a
287- function will result in an error. For the most part, however, the "surrounding
288- anything in parenthesis means it can be passed as an argument to a function"
288+ The details of this are covered when we look at semantic analysis (in Chaper 12
289+ "Syntax tree construction") however it is important to note there are exceptions
290+ to this rule. For example passing a ` return ` statement as an argument to a
291+ function will result in an error. For the most part, however, the "surrounding
292+ anything in parenthesis means it can be passed as an argument to a function"
289293rule does hold.
290294
291295In the next section I will cover the most important grammar rules in some detail.
@@ -303,8 +307,8 @@ stmts : none
303307 | stmts terms stmt
304308</pre >
305309
306- As mentioned earlier, ` program ` represents an entire program in the grammar.
307- For all intents and purposes ` compstmts ` is equivilent to ` program ` .
310+ As mentioned earlier, ` program ` represents an entire program in the grammar.
311+ For all intents and purposes ` compstmts ` is equivilent to ` program ` .
308312
309313前述の通り` program ` は文法全体、即ちプログラム全体を表している。その
310314` program ` は` compstmts ` と同じであり、` compstmts ` は` stmts ` とほぼ同じである。
0 commit comments