4
Racc ��ʸˡ��§���� Ruby �ǽ줿�ѡ�������������ѡ��������ͥ졼���Ǥ���
5
�ѡ����������르�ꥺ��ˤ� yacc �ʤɤ�Ʊ�� LALR(1) ����Ѥ��Ƥ��ޤ���
8
yacc ���ΤäƤ���ͤϵ���ˡ�ΰ㤤�����狼��лȤ���Ȼפ��ޤ���
10
������Ruby �� 256 �ܻȤ�������� ̵ƻ�ԡ�(������Ϻ����ASCII)
11
�ʤɤ���ɤ��Ƥ��������Τ��褤���Ȼפ��ޤ���
12
¾�� UNIX ���ޥ�ɤʤɤȤϰۤʤꡢ
13
�����ʤ�Ȥ������� Racc ������ΤϤ��ʤ꺤��Ǥ���
18
Racc ��ʸˡ���������ġ���Ǥ���
19
ʸ����Ϥ�����ʸ������ǡ�����ԥ塼���ˤȤäƤϰ�̣������ޤ���
20
�������ʹ֤Ϥ���ʸ���������ˤʤˤ���̣�Ф����Ȥ��Ǥ��ޤ���
21
����ԥ塼���ˤ⤽�Τ褦�ʤ��Ȥ���ʬŪ�ˤǤ⡢������줿�������Ǥ��礦��
22
Racc �Ϥ��μ������Ƥ���ޤ��������ʼ�ư���ǤϤ���ޤ���
23
�ʹ֤������������ڤ��˴�ñ�ˤʤ�ޤ���
26
Racc ����ư�����Ƥ������ʬ�Ȥϡ�ʸ����δޤ�ֹ�¤�פν����Ǥ���
27
���Ȥ��� Ruby �� if ʸ��ͤ��Ƥߤ�ȡ����Τ褦���꼰���Ǥ��ޤ���
42
if ʸ�Ǥ� if �Ȥ���ñ�줬�ǽ�ˤʤ��ƤϤʤ餺��
43
elsif ��� else �������ˤʤ��ƤϤ����ޤ���
44
���Τ褦�����֤δط� (��¤) ����Racc �����������оݤǤ���
47
������Racc �ǽ����Ǥ��ʤ��ΤϤɤ��������ȤǤ��礦��������ϡ����Ȥ���
48
if �ξ�P�ˤ�������ʬ���֤ʤ�Ǥ��뤫�פȤ������ȤǤ����Ĥޤꡢ���
49
���� if �ξ����Ȥ������ȤǤ�������ϡ����ä��Ǿ��Ȥ��ư��������ɤ�
53
�ȸ��äƤ⡢�狼��ˤ����Ǥ��礦�������������Ū�ʤ�Τϼºݤˤ����ä�
59
�ºݤ� Racc ��ɤΤ褦�˻Ȥ����Ȥ����äޤ���Racc �ˤ��ȼ��Υ�����
60
�����ɤߤ����ʤ�Τ����äơ�������˽����������ֹ�¤�פҤ��Ƥ�����
61
�������Υ������ե�������ʸˡ�ե�����פȸƤ֤��Ȥˤ��ޤ��礦������ʸ
62
ˡ�ե������̾���� parse.y �Ȳ��ꤹ��ȡ����ޥ�ɥ饤��ʲ��Τ褦
63
���Ǥ�����С����ι�¤��������뤿��Υ��饹��ޤ���ե����뤬������
70
���������ե�����ϥǥե���ȤǤ� "�ե�����̾.tab.rb" �Ǥ���¾��̾��
71
�ˤ������ʤ顢-o ���ץ������ѹ��Ǥ��ޤ���
74
$ racc parse.y -o myparser.rb
77
���Τ褦�ˤ��ƺ�ä����饹���ޤ��Ϥ��Τ褦�ʽ�����ô������ѡ��ȡ�
78
�Τ��Ȥϥѡ��� (parser) �ȸƤ֤��ȤˤʤäƤ��ޤ������Ϥ����ġ�
79
�Ȥ������餤��Ŭ���ˤȤ館�Ƥ���������
84
Racc ��ʸˡ�ե����뤫�� Ruby �Υ��饹����������ġ�����ȸ����ޤ�����
85
���Υ��饹������ Racc::Parser �β��̥��饹�ǡ�̾����ʸˡ�ե��������
86
���ꤷ�ޤ����ʲ��������˽٤����Ȥ��֤ʤ�ʤΤ��פ��������ޤ���
87
�����Ǥ����Ƥ˽������֤��Τǡ�ʸˡ�ե����뼫�Τ�ʸˡ�ξܺ٤�
88
<a href="grammar.html">ʸˡ��ե����</a>�Ƥ���������
99
if_stmt: IF expr then stmt_list elsif else END
119
Ruby ������ץȤΤ褦�� class �ǥѡ������饹̾����ꤷ��rule ... end
120
�δ֤˥ѡ����˲��Ϥ�������ʸˡ�Ҥ��ޤ���
123
ʸˡ�ϡ�������¤ӤǤ�ä�ɽ���ޤ���rule ... end �δ֤ˤ��륳����ȥС�
124
�ʳ��Τ�Ρ�if_stmt IF expr then �ʤɤ����ơֵ���פǤ��������ƥ�����
125
�����ܸ�Ǹ����֡��ϡߡߤ��פΡ֤ϡפߤ����ʤ��ǡ����κ��ε��椬����
126
��������Ʊ����Τ�ؤ����Ȥ����դ���������ޤ����ޤ����С��ϡ֤ޤ��ϡ�
127
���̣���ޤ�������ȡ�ñ��˥�����κ��ε���Τ��Ȥ��ա������դȤ�
128
�����ޤ����ʲ��Ϥ�����Τۤ���Ȥä��������ޤ��礦��
131
�������դ�ɬ�פ�����Ҥ٤ޤ����ޤ���then �Ρ��С��Τ��Ȥ���� (��§) ��
132
���Ƥ��������������ˤϲ���Ƥ��ʤ��Τǡ�����Ϥ����̤��̵�פǤ���
133
�Ƥ⤤�����Ȥ������Ȥ�ɽ���Ƥ��ޤ����Ĥޤꡢthen �ϵ��� THEN ��Ĥ���
134
�ޤ��Ϥʤˤ�ʤ�(��ά����)�Ǥ褤���Ȥ������ȤǤ������� then �ϼºݤ�
135
Ruby �Υ����������ɤˤ��� then �Ȥ��ڤ�Υ���ƹͤ��ޤ��礦
136
(����ϼ¤���ʸ���ε��� THEN ��ɽ���Ƥ��ޤ�)��
139
���ơ����������ֵ���פȤ�����Τ��ʤ�ʤΤ��ޤ��礦��
140
���������֤��äʤ��Ȥ����ʤ��Τǡ��ޤ���ʹ���Ƥ��Ƥ���������
141
����ʸ�Ϥκǽ�ˡ��ѡ����Ȥ�ʸ�����鹽¤�Ф���ʬ���ȸ����ޤ�����
142
������ʸ�����餤���ʤ깽¤��õ���Τ����ݤʤΤǡ��ºݤˤϤޤ�
143
ʸ�������ñ������ʬ�䤷�ޤ������λ����ǥ��ڡ����䥳���ȤϼΤƤ�
144
���ޤ����ʹߤϽ��˥ץ������ΰ�����ʤ���ʬ���������ˤ��ޤ���
145
���Ȥ���ʸ��������Ϥ����Τ褦���ä��Ȥ���ȡ�
148
if flag then # item found.
156
if flag then puts 'ok' end
159
�����ǡ����פ�ɬ�פǤ����ɤ���� flag �ϥ��������ѿ�̾���Ȼפ��ޤ�����
160
�ѿ�̾�Ȥ����Τ�¾�ˤ⤤����������ޤ���������̾���� i �������� a ����
161
���� vvvvvvvvvvvv �����������ֹ�¤�פ�Ʊ���Ǥ����Ĥޤ�Ʊ���������
162
�٤��Ǥ����ѿ� a �����ʤ� b ��ʤ��ƤϤ����ޤ����ä���
163
���Ū��Ʊ��̾�����ɤ�Ǥ⤤������Ȥ������Ȥǡ�����ñ������ʲ�
167
IF IDENT THEN IDENT STRING END
170
���줬�ֵ���פ���Ǥ����ѡ����ǤϤ��ε�����Τۤ�������¤���դ�
174
����˵���ˤĤ��Ƹ��Ƥ����ޤ��礦��
175
�����������ʬ�����ޤ����ֺ��դˤ��뵭��פȡ֤ʤ�����פǤ���
176
���դˤ��뵭��ϡ���ü����ȸ����ޤ����ʤ��ۤ��ϡֽ�ü�����
177
�����ޤ����ǽ����ǤϽ�ü����Ϥ��٤���ʸ������ü����Ͼ�ʸ����
178
�Ƥ���Τǡ��⤦������ä����ʸˡ�Ƥ���������
181
�ʤ����ζ�ʬ�����פ��ȸ����ȡ����Ϥε�����Ϥ��٤ƽ�ü���������Ǥ���
182
��������ü����ϥѡ�������Ǥ�������ü�������ֺ������פ��Ȥ�
183
��äƻϤ��¸�ߤ��ޤ����㤨�м��ε�§��⤦���ٸ��Ƥ���������
191
expr �� NUMBER �� IDENT �� STRING ���ȸ��äƤ��ޤ����դ˸����ȡ�
192
IDENT �� expr �ˡ֤ʤ뤳�Ȥ��Ǥ��ޤ��ס�ʸˡ�� expr ��¸�ߤǤ���
193
���� IDENT �����ȡ������ expr �ˤʤ�ޤ����㤨�� if �ξ�P��
194
��ʬ�� expr �Ǥ����顢������ IDENT ������� expr �ˤʤ�ޤ�������
195
�褦��ʸˡŪ�ˡ��礭��������äƤ��äơ��ǽ�Ū�˰�Ĥˤʤ�ȡ�
196
�������Ϥ�ʸˡ���������Ƥ��뤳�Ȥˤʤ�ޤ����ºݤˤ��ä������Ϥ�
197
��Ƥߤޤ��礦�����ϤϤ����Ǥ�����
200
IF IDENT THEN IDENT STRING END
203
�ޤ���IDENT �� expr �ˤʤ�ޤ���
206
IF expr THEN IDENT STRING END
209
���� THEN �� then �ˤʤ�ޤ���
212
IF expr then IDENT STRING END
215
IDENT STRING ����åɥ�����ˤʤ�ޤ�����������Ϥ����ۤɤ���ˤ�
216
�ʤ��Ǥ������¤Ͼ�ά����Ƥ������ȹͤ��Ƥ��������������Ƥ���������
217
������Фơ��ǽ�Ū�ˤ� stmt_list (ʸ�Υꥹ��)�ˤʤ�ޤ���
220
IF expr then stmt_list END
223
elsif �� else �Ͼ�ά�Ǥ��롢�Ĥޤ�̵���������Ǥ��ޤ���
226
IF expr then stmt_list elsif else END
229
�Ǹ�� if_stmt ����ޤ���
235
�Ȥ������ȤǤҤȤĤˤʤ�ޤ�����
236
�Ĥޤꤳ�����Ϥ�ʸˡŪ���������Ȥ������Ȥ��狼��ޤ�����
241
�����ޤǤ����Ϥ�ʸˡ�����������ɤ������ǧ������ˡ�Ϥ狼��ޤ�������
242
��������ǤϤʤ�ˤ�ʤ�ޤ��ǽ�����������褦�ˡ������ޤǤǤ�
243
��¤�������������ǡ��ץ������ϡְ�̣�פ�����Ǥ��ޤ������Ƥ���
244
��ʬ�� Racc �Ǥϼ�ư�����Ǥ��ʤ��Τǡ��ʹ֤����Ȥ�����ޤ�����
245
�����Τ��ʲ�����������֥��������פȤ�����ʬ�Ǥ���
248
����ǡ���������������礭��ñ�̤ˤޤȤ���Ƥ��������ޤ�����
249
���ΤޤȤ����ˡ�Ʊ���ˤʤˤ����餻�뤳�Ȥ��Ǥ��ޤ������줬
250
���������Ǥ������������ϡ�ʸˡ�ե�����ǰʲ��Τ褦�˽ޤ���
256
if_stmt: IF expr then stmt_list elsif else END
257
{ puts 'if_stmt found' }
260
{ puts 'then found' }
262
{ puts 'then is omitted' }
265
{ puts 'elsif is omitted' }
267
{ puts 'elsif found' }
270
{ puts 'else omitted' }
272
{ puts 'else found' }
275
{ puts 'expr found (NUMBER)' }
277
{ puts 'expr found (IDENT)' }
279
{ puts 'expr found (STRING)' }
286
���ƤΤȤ��ꡢ��§�Τ��Ȥ� { �� } �ǰϤ�ǽޤ���
287
���������ˤϤ������������ʤ褦�� Ruby ������ץȤ��ޤ���
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> ��ȤäƤ���������
304
�黻�Ҥ�ͥ���̡��������ȥ롼��ʤɤ� yacc �ΰ���Ū�ʵ�ǽ���Ѱդ����
305
���ޤ���������������⾯��ʸˡ���㤤�ޤ���
308
yacc �Ǥ��������줿�����ɤ�ľ��ž�̤���륳���ɤ�����ޤ�����
309
Racc �Ǥ�Ʊ���褦�ˡ��桼������Υ����ɤ��ޤ���
310
Racc �Ǥϥ��饹����������Τǡ����饹�������/��/��λ��Ľ꤬����ޤ���
311
Racc �ǤϤ����夫����֤� header inner footer �ȸƤ�Ǥ��ޤ���
314
<h3>�桼�����Ѱդ��٤�������</h3>
316
�ѡ����Υ���ȥ�ݥ���ȤȤʤ��åɤ���Ĥ���ޤ����ҤȤĤ�
317
<code>do_parse</code>�ǡ�������ϥȡ������
318
<code>Parser#next_token</code> �������ޤ����⤦�ҤȤĤ�
319
<code>yyparse</code> �ǡ�������ϥ�����ʤ��� <code>yield</code> ����
320
�뤳�Ȥˤ�äƥȡ���������ޤ����桼��¦�ǤϤ��Τɤ��餫(ξ���Ǥ⤤��
321
����)��ư�����ñ�ʥ�åɤ� inner �˽Ƥ���������������å�
322
�ΰ����ʤɡ��ܤ������Ȥϥ�ե���Ƥ���������
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>
329
�ɤ���Υ�åɤˤⶦ�̤ʤΤϥȡ�����η����Ǥ���ɬ���ȡ�����ܥ�
330
�Ȥ����ͤ������Ǥ����������֤��褦�ˤ��ޤ����ޤ��������λ���ơ�
331
�⤦�����Τ��ʤ����� <code>[false,<var>�ʤˤ�</var]</code> ���֤�
332
�Ƥ�������������ϰ���֤��н�ʬ�Ǥ� (�դˡ�<code>yyparse</code> ���
333
���������ʾ� <code>yield</code> ���ƤϤ����ʤ�)��
336
�ѡ������̤�ʸ��������ˤ����Ȥ����ΤǤϤ���ޤ����º�����Ȥ�
337
�ơ��ѡ���������̤ǤϤ����Ƥ�ʸ����Υ�����ʤȥ��åȤǻȤ����Ȥ�¿
338
���Ǥ��礦��Ruby �ʤ饹����ʤ��餤�ھ��Ǻ��ޤ�������®�ʥ�����ʤ�
339
�ʤ�ȼ¤����ä��ꤷ�ޤ��������ǹ�®�ʥ�����ʤ�������뤿��Υ饤
340
�֥����äƤ��ޤ����ܤ�����
341
<a href="#WritingScanner">�֥�����ʤ���פι�</a>�Ƥ���������
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> ��ȯ�����ޤ���
352
�桼���������������ǥѡ������顼��ȯ���������ϡ���å�
353
<a href="parser.html#Racc%3a%3aParser-yyerror"><code>yyerror</code></a>
354
��Ƥ٤Хѡ��������顼�����⡼�ɤ�����ޤ���
355
���������ΤȤ� <code>on_error</code>�ϸƤФ�ޤ���
358
<h3>�ѡ�������������</h3>
360
�����������Ф���������Ȼפ��ޤ������Ȥϡ��ǽ�˼�������ˡ��ʸˡ
361
�ե�������������Ruby ������ץȤ����ޤ���
364
���ޤ������Ф����ΤǤ������礭����Τ��Ⱥǽ餫��Ϥ��ޤ������ʤ��Ǥ���
365
����racc �� -g ���ץ�����Ĥ��ƥ���ѥ��뤷��@yydebug �� true �ˤ���
366
�ȥǥХå��Ѥν��Ϥ������ޤ����ǥХå����Ϥϥѡ����� @racc_debug_out
367
�˽��Ϥ���ޤ�(�ǥե���Ȥ� stderr)���ޤ���racc �� -v ���ץ�����Ĥ�
368
��ȡ���������ɽ���ɤߤ䤹�����ǽ��Ϥ����ե�����(*.output)�������ޤ���
369
�ɤ����ǥХå��λ��ͤˤʤ�Ǥ��礦��
373
<h2>��ä��ѡ��������ۤ���</h2>
375
Racc �����������ѡ�����ư����˥����롼����ɬ�פǤ���
376
����Ū�ˤ� parser.rb �� cparse.so �Ǥ���
377
������ cparse.so ��ñ�˥ѡ������®�����뤿��Υ饤�֥��ʤΤ�
378
ɬ�ܤǤϤ���ޤ��ʤ��Ƥ�ư���ޤ���
381
�ޤ� Ruby 1.8.0 �ʹߤˤϤ��Υ���बɸ��ź�դ���Ƥ���Τǡ�
382
Ruby 1.8 ������Ķ��ʤ�Х����ˤĤ��ƹ�θ����ɬ�פϤ���ޤ���
383
Racc 1.4.x �Υ����� Ruby 1.8 ��ź�դ���Ƥ��������
387
����� Ruby 1.8 ����Ǥ��ʤ����Ǥ���
388
Racc ��桼���ߤ�ʤ˥��ȡ��뤷�Ƥ�餦�Τ��Ĥμ�Ǥ�����
389
����Ǥ��Կ��ڤǤ���������Racc �Ǥϲ�������Ѱդ��ޤ�����
392
racc �� -E ���ץ�����Ĥ��ƥ���ѥ��뤹��ȡ�
393
�ѡ����� racc/parser.rb ����Τ����ե��������ϤǤ��ޤ���
394
����ʤ�Хե�����ϰ�Ĥ����ʤΤǴ�ñ�˰����ޤ���
395
racc/parser.rb �ϵ���Ū�� require �����褦�ʰ����ˤʤ�Τǡ�
396
���η����Υѡ�����ʣ�����ä��Ȥ��Ƥ⥯�饹���åɤ����ͤ��뤳�Ȥ⤢��ޤ���
397
������ -E ��Ȥä����� cparse.so ���Ȥ��ޤ���Τǡ�
398
ɬ��Ū�˥ѡ�����®�٤�����ޤ���
402
<h2><a name="WritingScanner">���ޤ��� ������ʤ��</a></h2>
404
�ѡ�����Ȥ��Ȥ��ϡ������Ƥ�ʸ�����ȡ�������ڤ�櫓�Ƥ���륹�����
405
��ɬ�פˤʤ�ޤ����������¤� Ruby ��ʸ����κǽ餫��ȡ�������ڤ�櫓
406
�Ƥ����Ȥ�����Ȥ����ޤ����դǤϤ���ޤ���
407
���Τ˸����ȡ���ñ�ˤǤ���ΤǤ���������ʤ�Υ����С��إåɤ�������ޤ���
410
���Υ����С��إåɤ���Ĥġ�
411
��ڤ˥�����ʤ����褦�� strscan �Ȥ����ѥå���������ޤ�����
412
Ruby 1.8 �ʹߤˤ�ɸ��ź�դ���Ƥ��ޤ�����
413
<a href="http://i.loveruby.net/ja/">ɮ�ԤΥۡ���ڡ���</a>�ˤ�
419
<h2>Generating Parser Using Racc</h2>
421
To compile Racc grammar file, simply type:
427
This creates ruby script file "parse.tab.y". -o option changes this.
430
<h2>Writing Racc Grammer File</h2>
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.
439
Here's example of Racc grammar file.
444
target: exp { print val[0] }
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'.
458
Then you must prepare parse entry method. There's two types of
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>.
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>.
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.
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.
485
<h3>re-distributing Racc runtime</h3>
487
A parser, which is created by Racc, requires Racc runtime module;
491
Ruby 1.8.x comes with racc runtime module,
492
you need NOT distribute racc runtime files.
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:
499
$ racc -E -omyparser.rb myparser.y
502
This command creates myparser.rb which `includes' racc runtime.
503
Only you must do is to distribute your parser file (myparser.rb).
506
Note: parser.rb is LGPL, but your parser is not.
507
Your own parser is completely yours.