~ubuntu-branches/ubuntu/intrepid/racc/intrepid

« back to all changes in this revision

Viewing changes to packages/racc/doc/usage.html.m

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2005-04-09 17:54:44 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050409175444-fvtir838ypkn7a58
Tags: 1.4.4-1
* new upstream version.  (closes: #301768)
* added racc2y.1 and y2racc.1.
* modified racc2y and y2racc to use optparse instead of deprecated getopts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
j
 
2
<h1>Racc �λȤ���</h1>
 
3
<p>
 
4
Racc ��ʸˡ��§���� Ruby �ǽ񤫤줿�ѡ�������������ѡ��������ͥ졼���Ǥ���
 
5
�ѡ����������르�ꥺ��ˤ� yacc �ʤɤ�Ʊ�� LALR(1) ����Ѥ��Ƥ��ޤ���
 
6
</p>
 
7
<p>
 
8
yacc ���ΤäƤ���ͤϵ���ˡ�ΰ㤤�����狼��лȤ���Ȼפ��ޤ���
 
9
yacc ���Τ�ʤ��ͤ�
 
10
������Ruby �� 256 �ܻȤ�������� ̵ƻ�ԡ�(������Ϻ����ASCII)
 
11
�ʤɤ���ɤ��Ƥ��������Τ��褤���Ȼפ��ޤ���
 
12
¾�� UNIX ���ޥ�ɤʤɤȤϰۤʤꡢ
 
13
�����ʤ�Ȥ������� Racc �����򤹤�ΤϤ��ʤ꺤��Ǥ���
 
14
</p>
 
15
 
 
16
<h2>Racc �ȤϤʤˤ�</h2>
 
17
<p>
 
18
Racc ��ʸˡ���������ġ���Ǥ���
 
19
ʸ����Ϥ�����ʸ������ǡ�����ԥ塼���ˤȤäƤϰ�̣������ޤ���
 
20
�������ʹ֤Ϥ���ʸ���������ˤʤˤ���̣�򸫽Ф����Ȥ��Ǥ��ޤ���
 
21
����ԥ塼���ˤ⤽�Τ褦�ʤ��Ȥ���ʬŪ�ˤǤ⡢������줿�������Ǥ��礦��
 
22
Racc �Ϥ��μ������򤷤Ƥ���ޤ��������ʼ�ư���ǤϤ���ޤ��󤬡�
 
23
�ʹ֤������������ڤ��˴�ñ�ˤʤ�ޤ���
 
24
</p>
 
25
<p>
 
26
Racc ����ư�����Ƥ������ʬ�Ȥϡ�ʸ����δޤ�ֹ�¤�פν����Ǥ���
 
27
���Ȥ��� Ruby �� if ʸ��ͤ��Ƥߤ�ȡ����Τ褦���꼰���Ǥ��ޤ���
 
28
</p>
 
29
<pre>
 
30
if ��P [then]
 
31
  ʸ
 
32
  ��
 
33
[elsif ��P [then]
 
34
  ʸ
 
35
  ��]
 
36
[else
 
37
  ʸ
 
38
  ��]
 
39
end
 
40
</pre>
 
41
<p>
 
42
if ʸ�Ǥ� if �Ȥ���ñ�줬�ǽ�ˤʤ��ƤϤʤ餺��
 
43
elsif ��� else �������ˤʤ��ƤϤ����ޤ���
 
44
���Τ褦�����֤δط� (��¤) ����Racc �����������оݤǤ���
 
45
</p>
 
46
<p>
 
47
������Racc �ǽ����Ǥ��ʤ��ΤϤɤ��������ȤǤ��礦��������ϡ����Ȥ���
 
48
if �ξ�P�ˤ�������ʬ���֤ʤ�Ǥ��뤫�פȤ������ȤǤ����Ĥޤꡢ���
 
49
���� if �ξ����Ȥ������ȤǤ�������ϡ����ä��Ǿ��Ȥ��ư��������ɤ�
 
50
�񤤤Ƥ��ʤ��Ȥ����ޤ���
 
51
</p>
 
52
<p>
 
53
�ȸ��äƤ⡢�狼��ˤ����Ǥ��礦�������������Ū�ʤ�Τϼºݤˤ����ä�
 
54
�ߤ�Τ����֤Ǥ���
 
55
</p>
 
56
 
 
57
<h2>�ºݤ���</h2>
 
58
<p>
 
59
�ºݤ� Racc ��ɤΤ褦�˻Ȥ����Ȥ����ä򤷤ޤ���Racc �ˤ��ȼ��Υ�����
 
60
�����ɤߤ����ʤ�Τ����äơ�������˽����������ֹ�¤�פ򵭽Ҥ��Ƥ�����
 
61
�������Υ������ե�������ʸˡ�ե�����פȸƤ֤��Ȥˤ��ޤ��礦������ʸ
 
62
ˡ�ե������̾���� parse.y �Ȳ��ꤹ��ȡ����ޥ�ɥ饤�󤫤�ʲ��Τ褦
 
63
���Ǥ�����С����ι�¤��������뤿��Υ��饹��ޤ���ե����뤬������
 
64
����
 
65
</p>
 
66
<pre>
 
67
$ racc parse.y
 
68
</pre>
 
69
<p>
 
70
���������ե�����ϥǥե���ȤǤ� "�ե�����̾.tab.rb" �Ǥ���¾��̾��
 
71
�ˤ������ʤ顢-o ���ץ������ѹ��Ǥ��ޤ���
 
72
</p>
 
73
<pre>
 
74
$ racc parse.y -o myparser.rb
 
75
</pre>
 
76
<p>
 
77
���Τ褦�ˤ��ƺ�ä����饹���ޤ��Ϥ��Τ褦�ʽ�����ô������ѡ��ȡ�
 
78
�Τ��Ȥϥѡ��� (parser) �ȸƤ֤��ȤˤʤäƤ��ޤ������Ϥ����ġ�
 
79
�Ȥ������餤��Ŭ���ˤȤ館�Ƥ���������
 
80
</p>
 
81
 
 
82
<h2>ʸˡ�ե�������</h2>
 
83
<p>
 
84
Racc ��ʸˡ�ե����뤫�� Ruby �Υ��饹����������ġ�����ȸ����ޤ�����
 
85
���Υ��饹������ Racc::Parser �β��̥��饹�ǡ�̾����ʸˡ�ե��������
 
86
���ꤷ�ޤ����ʲ��������˽񤯤٤����Ȥ��֤ʤ�ʤΤ��פ��������ޤ���
 
87
�����Ǥ����Ƥ˽������֤��Τǡ�ʸˡ�ե����뼫�Τ�ʸˡ�ξܺ٤�
 
88
<a href="grammar.html">ʸˡ��ե����</a>�򸫤Ƥ���������
 
89
</p>
 
90
 
 
91
<h3>ʸˡ</h3>
 
92
<p>
 
93
�ޤ��ϡ����Τγ����Ǥ���
 
94
</p>
 
95
<pre>
 
96
class MyParser
 
97
rule
 
98
 
 
99
  if_stmt: IF expr then stmt_list elsif else END
 
100
 
 
101
  then   : THEN
 
102
         |
 
103
 
 
104
  elsif  :
 
105
         | ELSIF stmt_list
 
106
 
 
107
  else   :
 
108
         | ELSE stmt_list
 
109
 
 
110
  expr   : NUMBER
 
111
         | IDENT
 
112
         | STRING
 
113
 
 
114
  stmt_list : �դˤ�դˤ�
 
115
 
 
116
end
 
117
</pre>
 
118
<p>
 
119
Ruby ������ץȤΤ褦�� class �ǥѡ������饹̾����ꤷ��rule ... end 
 
120
�δ֤˥ѡ����˲��Ϥ�������ʸˡ�򵭽Ҥ��ޤ���
 
121
</p>
 
122
<p>
 
123
ʸˡ�ϡ�������¤ӤǤ�ä�ɽ���ޤ���rule ... end �δ֤ˤ��륳����ȥС�
 
124
�ʳ��Τ�Ρ�if_stmt IF expr then �ʤɤ����ơֵ���פǤ��������ƥ�����
 
125
�����ܸ�Ǹ����֡��ϡߡߤ��פΡ֤ϡפߤ����ʤ��ǡ����κ��ε��椬����
 
126
��������Ʊ����Τ�ؤ����Ȥ����դ���������ޤ����ޤ����С��ϡ֤ޤ��ϡ�
 
127
���̣���ޤ�������ȡ�ñ��˥�����κ��ε���Τ��Ȥ��ա������դȤ�
 
128
�����ޤ����ʲ��Ϥ�����Τۤ���Ȥä��������ޤ��礦��
 
129
</p>
 
130
<p>
 
131
�������դ�ɬ�פ�����Ҥ٤ޤ����ޤ���then �Ρ��С��Τ��Ȥ���� (��§) ��
 
132
���Ƥ��������������ˤϲ���񤤤Ƥ��ʤ��Τǡ�����Ϥ����̤��̵�פǤ���
 
133
�Ƥ⤤�����Ȥ������Ȥ�ɽ���Ƥ��ޤ����Ĥޤꡢthen �ϵ��� THEN ��Ĥ���
 
134
�ޤ��Ϥʤˤ�ʤ�(��ά����)�Ǥ褤���Ȥ������ȤǤ������� then �ϼºݤ� 
 
135
Ruby �Υ����������ɤˤ��� then �Ȥ��ڤ�Υ���ƹͤ��ޤ��礦
 
136
(����ϼ¤���ʸ���ε��� THEN ��ɽ���Ƥ��ޤ�)��
 
137
</p>
 
138
<p>
 
139
���ơ����������ֵ���פȤ�����Τ��ʤ�ʤΤ��񤭤ޤ��礦��
 
140
���������֤��ä򤷤ʤ��Ȥ����ʤ��Τǡ��ޤ���ʹ���Ƥ��Ƥ���������
 
141
����ʸ�Ϥκǽ�ˡ��ѡ����Ȥ�ʸ�����󤫤鹽¤�򸫽Ф���ʬ���ȸ����ޤ�����
 
142
������ʸ�����󤫤餤���ʤ깽¤��õ���Τ����ݤʤΤǡ��ºݤˤϤޤ�
 
143
ʸ�������ñ������ʬ�䤷�ޤ������λ����ǥ��ڡ����䥳���ȤϼΤƤ�
 
144
���ޤ����ʹߤϽ��˥ץ������ΰ�����ʤ���ʬ���������ˤ��ޤ���
 
145
���Ȥ���ʸ��������Ϥ����Τ褦���ä��Ȥ���ȡ�
 
146
</p>
 
147
<pre>
 
148
if flag then   # item found.
 
149
  puts 'ok'
 
150
end
 
151
</pre>
 
152
<p>
 
153
ñ�����ϼ��Τ褦�ˤʤ�ޤ���
 
154
</p>
 
155
<pre>
 
156
if flag then puts 'ok' end
 
157
</pre>
 
158
<p>
 
159
�����ǡ����פ�ɬ�פǤ����ɤ���� flag �ϥ��������ѿ�̾���Ȼפ��ޤ�����
 
160
�ѿ�̾�Ȥ����Τ�¾�ˤ⤤����������ޤ���������̾���� i �������� a ����
 
161
���� vvvvvvvvvvvv �����������ֹ�¤�פ�Ʊ���Ǥ����Ĥޤ�Ʊ�������򤵤��
 
162
�٤��Ǥ����ѿ� a ��񤱤���ʤ� b ��񤱤ʤ��ƤϤ����ޤ��󡣤��ä���
 
163
���Ū��Ʊ��̾�����ɤ�Ǥ⤤������󡣤Ȥ������Ȥǡ�����ñ������ʲ�
 
164
�Τ褦���ɤߤ����ޤ��礦��
 
165
</p>
 
166
<pre>
 
167
IF IDENT THEN IDENT STRING END
 
168
</pre>
 
169
<p>
 
170
���줬�ֵ���פ���Ǥ����ѡ����ǤϤ��ε�����Τۤ��򰷤�����¤���դ�
 
171
�Ƥ����ޤ���
 
172
</p>
 
173
<p>
 
174
����˵���ˤĤ��Ƹ��Ƥ����ޤ��礦��
 
175
�����������ʬ�����ޤ����ֺ��դˤ��뵭��פȡ֤ʤ�����פǤ���
 
176
���դˤ��뵭��ϡ���ü�׵���ȸ����ޤ����ʤ��ۤ��ϡֽ�ü�׵����
 
177
�����ޤ����ǽ����ǤϽ�ü����Ϥ��٤���ʸ������ü����Ͼ�ʸ����
 
178
�񤤤Ƥ���Τǡ��⤦������ä����ʸˡ�򸫤Ƥ���������
 
179
</p>
 
180
<p>
 
181
�ʤ����ζ�ʬ�����פ��ȸ����ȡ����Ϥε�����Ϥ��٤ƽ�ü���������Ǥ���
 
182
��������ü����ϥѡ�������Ǥ�������ü������󤫤�ֺ������פ��Ȥ�
 
183
��äƻϤ��¸�ߤ��ޤ����㤨�м��ε�§��⤦���ٸ��Ƥ���������
 
184
</p>
 
185
<pre>
 
186
  expr   : NUMBER
 
187
         | IDENT
 
188
         | STRING
 
189
</pre>
 
190
<p>
 
191
expr �� NUMBER �� IDENT �� STRING ���ȸ��äƤ��ޤ����դ˸����ȡ�
 
192
IDENT �� expr �ˡ֤ʤ뤳�Ȥ��Ǥ��ޤ��ס�ʸˡ�� expr ��¸�ߤǤ���
 
193
���� IDENT �����ȡ������ expr �ˤʤ�ޤ����㤨�� if �ξ�P��
 
194
��ʬ�� expr �Ǥ����顢������ IDENT ������� expr �ˤʤ�ޤ�������
 
195
�褦��ʸˡŪ�ˡ��礭���׵�����äƤ��äơ��ǽ�Ū�˰�Ĥˤʤ�ȡ�
 
196
�������Ϥ�ʸˡ���������Ƥ��뤳�Ȥˤʤ�ޤ����ºݤˤ��ä������Ϥ�
 
197
��Ƥߤޤ��礦�����ϤϤ����Ǥ�����
 
198
</p>
 
199
<pre>
 
200
IF IDENT THEN IDENT STRING END
 
201
</pre>
 
202
<p>
 
203
�ޤ���IDENT �� expr �ˤʤ�ޤ���
 
204
</p>
 
205
<pre>
 
206
IF expr THEN IDENT STRING END
 
207
</pre>
 
208
<p>
 
209
���� THEN �� then �ˤʤ�ޤ���
 
210
</p>
 
211
<pre>
 
212
IF expr then IDENT STRING END
 
213
</pre>
 
214
<p>
 
215
IDENT STRING ���᥽�åɥ�����ˤʤ�ޤ�����������Ϥ����ۤɤ���ˤ�
 
216
�ʤ��Ǥ������¤Ͼ�ά����Ƥ������ȹͤ��Ƥ��������������Ƥ���������
 
217
������Фơ��ǽ�Ū�ˤ� stmt_list (ʸ�Υꥹ��)�ˤʤ�ޤ���
 
218
</p>
 
219
<pre>
 
220
IF expr then stmt_list END
 
221
</pre>
 
222
<p>
 
223
elsif �� else �Ͼ�ά�Ǥ��롢�Ĥޤ�̵���������Ǥ��ޤ���
 
224
</p>
 
225
<pre>
 
226
IF expr then stmt_list elsif else END
 
227
</pre>
 
228
<p>
 
229
�Ǹ�� if_stmt ����ޤ���
 
230
</p>
 
231
<pre>
 
232
if_stmt
 
233
</pre>
 
234
<p>
 
235
�Ȥ������ȤǤҤȤĤˤʤ�ޤ�����
 
236
�Ĥޤꤳ�����Ϥ�ʸˡŪ���������Ȥ������Ȥ��狼��ޤ�����
 
237
</p>
 
238
 
 
239
<h3>���������</h3>
 
240
<p>
 
241
�����ޤǤ����Ϥ�ʸˡ�����������ɤ������ǧ������ˡ�Ϥ狼��ޤ�������
 
242
��������ǤϤʤ�ˤ�ʤ�ޤ��󡣺ǽ�����������褦�ˡ������ޤǤǤ�
 
243
��¤�������������ǡ��ץ������ϡְ�̣�פ�����Ǥ��ޤ��󡣤����Ƥ���
 
244
��ʬ�� Racc �Ǥϼ�ư�����Ǥ��ʤ��Τǡ��ʹ֤��񤯡��Ȥ�����ޤ�����
 
245
�����񤯤Τ��ʲ�����������֥��������פȤ�����ʬ�Ǥ���
 
246
</p>
 
247
<p>
 
248
����ǡ�������󤬤��������礭��ñ�̤ˤޤȤ���Ƥ��������򸫤ޤ�����
 
249
���ΤޤȤ����ˡ�Ʊ���ˤʤˤ����餻�뤳�Ȥ��Ǥ��ޤ������줬
 
250
���������Ǥ������������ϡ�ʸˡ�ե�����ǰʲ��Τ褦�˽񤭤ޤ���
 
251
</p>
 
252
<pre>
 
253
class MyParser
 
254
rule
 
255
 
 
256
  if_stmt: IF expr then stmt_list elsif else END
 
257
             { puts 'if_stmt found' }
 
258
 
 
259
  then   : THEN
 
260
             { puts 'then found' }
 
261
         |
 
262
             { puts 'then is omitted' }
 
263
 
 
264
  elsif  :
 
265
             { puts 'elsif is omitted' }
 
266
         | ELSIF stmt_list
 
267
             { puts 'elsif found' }
 
268
 
 
269
  else   :
 
270
             { puts 'else omitted' }
 
271
         | ELSE stmt_list
 
272
             { puts 'else found' }
 
273
 
 
274
  expr   : NUMBER
 
275
             { puts 'expr found (NUMBER)' }
 
276
         | IDENT
 
277
             { puts 'expr found (IDENT)' }
 
278
         | STRING
 
279
             { puts 'expr found (STRING)' }
 
280
 
 
281
  stmt_list : �դˤ�դˤ�
 
282
 
 
283
end
 
284
</pre>
 
285
<p>
 
286
���ƤΤȤ��ꡢ��§�Τ��Ȥ� { �� } �ǰϤ�ǽ񤭤ޤ���
 
287
���������ˤϤ������������ʤ褦�� Ruby ������ץȤ��񤱤ޤ���
 
288
</p>
 
289
<p>
 
290
(�����ᡢ̤��)
 
291
</p>
 
292
<hr>
 
293
 
 
294
<p>
 
295
yacc �Ǥ� <code>$$</code> �� Racc �Ǥϥ��������ѿ� <code>result</code>
 
296
�ǡ�<code>$1,$2...</code> ������ <var>val</var>�Ǥ���
 
297
<code>result</code> �� <code>val[0]</code> ($1) ���ͤ˽�������졢
 
298
����������ȴ�����Ȥ��� <code>result</code> ���ͤ������ͤˤʤ�ޤ���
 
299
Racc �Ǥϥ����������� <code>return</code> �ϥ�������󤫤�ȴ��������ǡ�
 
300
�ѡ������ΤϽ����ޤ��󡣥���������椫��ѡ�����λ����ˤϡ�
 
301
�᥽�å� <code>yyaccept</code> ��ȤäƤ���������
 
302
</p>
 
303
<p>
 
304
�黻�Ҥ�ͥ���̡��������ȥ롼��ʤɤ� yacc �ΰ���Ū�ʵ�ǽ���Ѱդ����
 
305
���ޤ���������������⾯��ʸˡ���㤤�ޤ���
 
306
</p>
 
307
<p>
 
308
yacc �Ǥ��������줿�����ɤ�ľ��ž�̤���륳���ɤ�����ޤ�����
 
309
Racc �Ǥ�Ʊ���褦�ˡ��桼������Υ����ɤ��񤱤ޤ���
 
310
Racc �Ǥϥ��饹����������Τǡ����饹�������/��/��λ��Ľ꤬����ޤ���
 
311
Racc �ǤϤ����夫����֤� header inner footer �ȸƤ�Ǥ��ޤ���
 
312
</p>
 
313
 
 
314
<h3>�桼�����Ѱդ��٤�������</h3>
 
315
<p>
 
316
�ѡ����Υ���ȥ�ݥ���ȤȤʤ�᥽�åɤ���Ĥ���ޤ����ҤȤĤ� 
 
317
<code>do_parse</code>�ǡ�������ϥȡ������ 
 
318
<code>Parser#next_token</code> �������ޤ����⤦�ҤȤĤ� 
 
319
<code>yyparse</code> �ǡ�������ϥ�����ʤ��� <code>yield</code> ����
 
320
�뤳�Ȥˤ�äƥȡ���������ޤ����桼��¦�ǤϤ��Τɤ��餫(ξ���Ǥ⤤��
 
321
����)��ư�����ñ�ʥ᥽�åɤ� inner �˽񤤤Ƥ��������������᥽�å�
 
322
�ΰ����ʤɡ��ܤ������Ȥϥ�ե���󥹤򸫤Ƥ���������
 
323
</p>
 
324
<ul>
 
325
<li><a href="parser.html#Racc%3a%3aParser-do_parse">do_parse</a>
 
326
<li><a href="parser.html#Racc%3a%3aParser-yyparse">yyparse</a>
 
327
</ul>
 
328
<p>
 
329
�ɤ���Υ᥽�åɤˤⶦ�̤ʤΤϥȡ�����η����Ǥ���ɬ���ȡ����󥷥�ܥ�
 
330
�Ȥ����ͤ������Ǥ����������֤��褦�ˤ��ޤ����ޤ�������󤬽�λ���ơ�
 
331
�⤦�����Τ��ʤ����� <code>[false,<var>�ʤˤ�</var]</code> ���֤�
 
332
�Ƥ�������������ϰ���֤��н�ʬ�Ǥ� (�դˡ�<code>yyparse</code> ���
 
333
���������ʾ� <code>yield</code> ���ƤϤ����ʤ�)��
 
334
</p>
 
335
<p>
 
336
�ѡ������̤�ʸ��������ˤ����Ȥ����ΤǤϤ���ޤ��󤬡��º�����Ȥ�
 
337
�ơ��ѡ���������̤ǤϤ����Ƥ�ʸ����Υ�����ʤȥ��åȤǻȤ����Ȥ�¿
 
338
���Ǥ��礦��Ruby �ʤ饹����ʤ��餤�ھ��Ǻ��ޤ�������®�ʥ�����ʤ�
 
339
�ʤ�ȼ¤��񤷤��ä��ꤷ�ޤ��������ǹ�®�ʥ�����ʤ�������뤿��Υ饤
 
340
�֥����äƤ��ޤ����ܤ�����
 
341
<a href="#WritingScanner">�֥�����ʤ���פι�</a>�򸫤Ƥ���������
 
342
</p>
 
343
<p>
 
344
Racc �ˤ� error �ȡ������Ȥä����顼������ǽ�⤢��ޤ���yacc ��
 
345
<code>yyerror()</code> �� Racc �Ǥ�
 
346
<a href="parser.html#Racc%3a%3aParser-on_error"><code>Racc::Parser#on_error</code></a>
 
347
�ǡ����顼���������ȡ�����Ȥ����͡��ͥ����å����λ��Ĥΰ�����Ȥ�ޤ���
 
348
<code>on_error</code> �Υǥե���Ȥμ������㳰
 
349
<code>Racc::ParseError</code> ��ȯ�����ޤ���
 
350
</p>
 
351
<p>
 
352
�桼���������������ǥѡ������顼��ȯ���������ϡ��᥽�å�
 
353
<a href="parser.html#Racc%3a%3aParser-yyerror"><code>yyerror</code></a>
 
354
��Ƥ٤Хѡ��������顼�����⡼�ɤ�����ޤ���
 
355
���������ΤȤ� <code>on_error</code>�ϸƤФ�ޤ���
 
356
</p>
 
357
 
 
358
<h3>�ѡ�������������</h3>
 
359
<p>
 
360
�����������Ф��������񤱤�Ȼפ��ޤ������Ȥϡ��ǽ�˼�������ˡ��ʸˡ
 
361
�ե�������������Ruby ������ץȤ����ޤ���
 
362
</p>
 
363
<p>
 
364
���ޤ������Ф����ΤǤ������礭����Τ��Ⱥǽ餫��Ϥ��ޤ������ʤ��Ǥ���
 
365
����racc �� -g ���ץ�����Ĥ��ƥ���ѥ��뤷��@yydebug �� true �ˤ���
 
366
�ȥǥХå��Ѥν��Ϥ������ޤ����ǥХå����Ϥϥѡ����� @racc_debug_out 
 
367
�˽��Ϥ���ޤ�(�ǥե���Ȥ� stderr)���ޤ���racc �� -v ���ץ�����Ĥ�
 
368
��ȡ���������ɽ���ɤߤ䤹�����ǽ��Ϥ����ե�����(*.output)�������ޤ���
 
369
�ɤ����ǥХå��λ��ͤˤʤ�Ǥ��礦��
 
370
</p>
 
371
 
 
372
 
 
373
<h2>��ä��ѡ��������ۤ���</h2>
 
374
<p>
 
375
Racc �����������ѡ�����ư����˥�󥿥���롼����ɬ�פǤ���
 
376
����Ū�ˤ� parser.rb �� cparse.so �Ǥ���
 
377
������ cparse.so ��ñ�˥ѡ������®�����뤿��Υ饤�֥��ʤΤ�
 
378
ɬ�ܤǤϤ���ޤ��󡣤ʤ��Ƥ�ư���ޤ���
 
379
</p>
 
380
<p>
 
381
�ޤ� Ruby 1.8.0 �ʹߤˤϤ��Υ�󥿥��बɸ��ź�դ���Ƥ���Τǡ�
 
382
Ruby 1.8 ������Ķ��ʤ�Х�󥿥���ˤĤ��ƹ�θ����ɬ�פϤ���ޤ���
 
383
Racc 1.4.x �Υ�󥿥���� Ruby 1.8 ��ź�դ���Ƥ����󥿥����
 
384
�����ߴ��Ǥ���
 
385
</p>
 
386
<p>
 
387
����� Ruby 1.8 ����Ǥ��ʤ����Ǥ��� 
 
388
Racc ��桼���ߤ�ʤ˥��󥹥ȡ��뤷�Ƥ�餦�Τ��Ĥμ�Ǥ�����
 
389
����Ǥ��Կ��ڤǤ���������Racc �Ǥϲ�������Ѱդ��ޤ�����
 
390
</p>
 
391
<p>
 
392
racc �� -E ���ץ�����Ĥ��ƥ���ѥ��뤹��ȡ�
 
393
�ѡ����� racc/parser.rb ����Τ����ե��������ϤǤ��ޤ���
 
394
����ʤ�Хե�����ϰ�Ĥ����ʤΤǴ�ñ�˰����ޤ���
 
395
racc/parser.rb �ϵ���Ū�� require �����褦�ʰ����ˤʤ�Τǡ�
 
396
���η����Υѡ�����ʣ�����ä��Ȥ��Ƥ⥯�饹��᥽�åɤ����ͤ��뤳�Ȥ⤢��ޤ���
 
397
������ -E ��Ȥä����� cparse.so ���Ȥ��ޤ���Τǡ�
 
398
ɬ��Ū�˥ѡ�����®�٤�����ޤ���
 
399
</p>
 
400
 
 
401
 
 
402
<h2><a name="WritingScanner">���ޤ��� ������ʤ��</a></h2>
 
403
<p>
 
404
�ѡ�����Ȥ��Ȥ��ϡ������Ƥ�ʸ�����ȡ�������ڤ�櫓�Ƥ���륹�����
 
405
��ɬ�פˤʤ�ޤ����������¤� Ruby ��ʸ����κǽ餫��ȡ�������ڤ�櫓
 
406
�Ƥ����Ȥ�����Ȥ����ޤ����դǤϤ���ޤ���
 
407
���Τ˸����ȡ���ñ�ˤǤ���ΤǤ���������ʤ�Υ����С��إåɤ�������ޤ���
 
408
</p>
 
409
<p>
 
410
���Υ����С��إåɤ���򤷤Ĥġ�
 
411
��ڤ˥�����ʤ����褦�� strscan �Ȥ����ѥå���������ޤ�����
 
412
Ruby 1.8 �ʹߤˤ�ɸ��ź�դ���Ƥ��ޤ�����
 
413
<a href="http://i.loveruby.net/ja/">ɮ�ԤΥۡ���ڡ���</a>�ˤ�
 
414
ñ�Υѥå�����������ޤ���
 
415
</p>
 
416
e
 
417
<h1>Usage</h1>
 
418
 
 
419
<h2>Generating Parser Using Racc</h2>
 
420
<p>
 
421
To compile Racc grammar file, simply type:
 
422
</p>
 
423
<pre>
 
424
$ racc parse.y
 
425
</pre>
 
426
<p>
 
427
This creates ruby script file "parse.tab.y". -o option changes this.
 
428
</p>
 
429
 
 
430
<h2>Writing Racc Grammer File</h2>
 
431
<p>
 
432
If you want your own parser, you have to write grammar file.
 
433
A grammar file contains name of parser class, grammar the parser can parse,
 
434
user code, and any.<br>
 
435
When writing grammar file, yacc's knowledge is helpful.
 
436
If you have not use yacc, also racc is too difficult.
 
437
</p>
 
438
<p>
 
439
Here's example of Racc grammar file.
 
440
</p>
 
441
<pre>
 
442
class Calcparser
 
443
rule
 
444
  target: exp { print val[0] }
 
445
 
 
446
  exp: exp '+' exp
 
447
     | exp '*' exp
 
448
     | '(' exp ')'
 
449
     | NUMBER
 
450
end
 
451
</pre>
 
452
<p>
 
453
Racc grammar file is resembles to yacc file.
 
454
But (of cource), action is Ruby code. yacc's $$ is 'result', $0, $1... is
 
455
an array 'val', $-1, $-2... is an array '_values'.
 
456
</p>
 
457
<p>
 
458
Then you must prepare parse entry method. There's two types of
 
459
racc's parse method, 
 
460
<a href="parser.html#Racc%3a%3aParser-do_parse"><code>do_parse</code></a> and
 
461
<a href="parser.html#Racc%3a%3aParser-yyparse"><code>yyparse</code></a>.
 
462
</p>
 
463
<p>
 
464
"do_parse()" is simple. it is yyparse() of yacc, and "next_token()" is
 
465
yylex(). This method must returns an array like [TOKENSYMBOL, ITS_VALUE].
 
466
EOF is [false, false].
 
467
(token symbol is ruby symbol (got by String#intern) as default.
 
468
 If you want to change this, see <a href="grammar.html#token">grammar reference</a>.
 
469
</p>
 
470
<p>
 
471
"yyparse()" is little complecated, but useful. It does not use "next_token()",
 
472
it gets tokens from any iterator. For example, "yyparse(obj, :scan)" causes
 
473
calling obj#scan, and you can return tokens by yielding them from obj#scan.
 
474
</p>
 
475
<p>
 
476
When debugging, "-v" or/and "-g" option is helpful.
 
477
"-v" causes creating verbose log file (.output).
 
478
"-g" causes creating "Verbose Parser".
 
479
Verbose Parser prints internal status when parsing.
 
480
But it is <em>not</em> automatic.
 
481
You must use -g option and set @yydebug true to get output.
 
482
-g option only creates verbose parser.
 
483
</p>
 
484
 
 
485
<h3>re-distributing Racc runtime</h3>
 
486
<p>
 
487
A parser, which is created by Racc, requires Racc runtime module;
 
488
racc/parser.rb.
 
489
</p>
 
490
<p>
 
491
Ruby 1.8.x comes with racc runtime module,
 
492
you need NOT distribute racc runtime files.
 
493
</p>
 
494
<p>
 
495
If you want to run your parsers on ruby 1.6,
 
496
you need re-distribute racc runtime module with your parser.
 
497
It can be done by using '-E' option:
 
498
<pre>
 
499
$ racc -E -omyparser.rb myparser.y
 
500
</pre>
 
501
<p>
 
502
This command creates myparser.rb which `includes' racc runtime.
 
503
Only you must do is to distribute your parser file (myparser.rb).
 
504
</p>
 
505
<p>
 
506
Note: parser.rb is LGPL, but your parser is not.
 
507
Your own parser is completely yours.
 
508
</p>
 
509
.