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

« back to all changes in this revision

Viewing changes to packages/racc/doc/grammar.rd.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
= ��§�ե�����ʸˡ��ե����
 
3
 
 
4
== ʸˡ�˴ؤ������С������Ȥ���ߴ�
 
5
 
 
6
  * (1.2.5) �桼���������ɤ�Ϣ�뤹����������ե��������
 
7
            ��ᤳ��Ǥ��륳���ɤ����Ϣ�뤷�ޤ���
 
8
  * (1.1.6) �������ǥ��쥯�ƥ��� options ���ɲä���ޤ�����
 
9
  * (1.1.5) ͽ��� token �ΰ�̣���ѹ��ˤʤ�ޤ�����
 
10
  * (0.14) �롼��κǸ�Υ��ߥ����󤬾�ά��ǽ�ˤʤ�ޤ�����
 
11
           �ޤ���token prechigh �ʤɤ�ͽ���Ǥʤ��ʤ�ޤ�����
 
12
  * (10.2) prepare �� header �� driver �� footer �ˤʤ�ޤ�����
 
13
           ���Ϥ��ΤޤޤǤ�Ȥ��ޤ�����2.0 ������б����ޤ���
 
14
  * (0.10) class ���б����� end ���ʤ��ʤ�ޤ�����
 
15
  * (0.9) ���������Υԥꥪ����������� { �� } �ǰϤ�褦�ˤ��ޤ�����
 
16
 
 
17
== ���Τι�¤
 
18
 
 
19
�ȥåץ�٥�ϡ���§���ȥ桼��������������ʬ�����ޤ���
 
20
�桼�������������ϥ��饹����θ����ʤ���Ф����ޤ���
 
21
 
 
22
=== ������
 
23
 
 
24
ʸˡ�ե�����ˤϡ������㳰������ơ��ۤȤ�ɤɤ��ˤǤ⥳���Ȥ�
 
25
�񤯤��Ȥ��Ǥ��ޤ��������Ȥϡ�Ruby�� #.....(����) ��������ȡ�
 
26
C�� /*......*/ ���������Ȥ����Ȥ��Ǥ��ޤ���
 
27
 
 
28
=== ��§��
 
29
 
 
30
��§���ϰʲ��Τ褦�ʷ��򤷤Ƥ��ޤ���
 
31
--
 
32
class ���饹̾ [< �����ѡ����饹]
 
33
  [�黻�ҽ��]
 
34
  [�ȡ��������]
 
35
  [���ץ����]
 
36
  [expect]
 
37
  [�ȡ����󥷥�ܥ��ͤ�������]
 
38
  [�������ȵ�§]
 
39
rule
 
40
  ʸˡ����
 
41
--
 
42
"���饹̾"�Ϥ������������ѡ������饹��̾���Ǥ���
 
43
����Ϥ��Τޤ�Ruby�Υ��饹̾�ˤʤ�ޤ���
 
44
 
 
45
�ޤ� M::C �Τ褦�ˡ�::�פ�Ȥä�̾����Ȥ��ȡ����饹�����
 
46
�⥸�塼�� M ����˥ͥ��Ȥ����ޤ����Ĥޤ� class M::C �ʤ��
 
47
--
 
48
module M
 
49
  class C < Racc::Parser
 
50
    ��������
 
51
  end
 
52
end
 
53
--
 
54
�Τ褦�˽��Ϥ��ޤ���
 
55
 
 
56
����ˡ�Ruby ��Ʊ����ʸ�ǥ����ѡ����饹�����Ǥ��ޤ���
 
57
���������λ���򤹤�ȥѡ�����ư��˽���ʱƶ���Ϳ����Τǡ�
 
58
�ä�ɬ�פ��ʤ��¤���ꤷ�ƤϤ����ޤ��󡣤���Ͼ���γ�ĥ��
 
59
������Ѱդ�����Τǡ����߻��ꤹ��ɬ�����Ϥ��ޤꤢ��ޤ���
 
60
 
 
61
=== ʸˡ�ε���
 
62
 
 
63
racc ����������ѡ���������Ǥ���ʸˡ�򵭽Ҥ��ޤ���
 
64
ʸˡ�ϡ�ͽ��� rule �� end �δ֤ˡ��ʲ��Τ褦�ʽ񼰤ǽ񤭤ޤ���
 
65
--
 
66
�ȡ�����: �ȡ�������¤� ���������
 
67
 
 
68
�ȡ�����: �ȡ�������¤� ���������
 
69
        | �ȡ�������¤� ���������
 
70
        | �ȡ�������¤� ���������
 
71
             (ɬ�פʤ���Ʊ���褦�ˤĤŤ���)
 
72
--
 
73
���������� { } �ǰϤߤޤ������������Ǥ� Ruby ��ʸ�ϤۤȤ��
 
74
�Ȥ��ޤ������������������б��Ǥ����б����Ƥ��ʤ���Τϰʲ��ΤȤ��ꡣ
 
75
 
 
76
  * �ҥ��ɥ������
 
77
  * =begin ... =end ��������
 
78
  * ���ڡ����ǻϤޤ�����ɽ��
 
79
  * �����ޤ�� % �α黻�����̤˱黻�ҤΤޤ��˥��ڡ���������Ƥ��������ʤ�
 
80
 
 
81
���Τ�����˴ؤ��Ƥϴ������б��Ϥޤ�̵���Ǥ����������Ƥ���������
 
82
 
 
83
���դ���($$)�ϡ����ץ����ˤ�ä��֤����������ޤ����ޤ��ǥե���ȤǤ�
 
84
���������ѿ� result (���Υǥե�����ͤ� val[0])�� �����ͤ�ɽ�������������
 
85
�֥��å���ȴ�������� result ���ͤ������ͤˤʤ�ޤ����ޤ�������Ū�� return
 
86
���֤������⤳���ͤˤʤ�ޤ���������options �� no_result_var ����ꤷ��
 
87
��硢�����ͤϥ��������֥��å��κǸ��ʸ���ͤˤʤ�ޤ� (Ruby �Υ᥽�åɤ�
 
88
Ʊ��)��
 
89
 
 
90
�ɤ���ξ��Ǥ⥢�������Ͼ�ά�Ǥ�����ά�������κ����ͤϾ�� val[0] �Ǥ���
 
91
 
 
92
�ʲ���ʸˡ���Ҥ����Τ���򤷤ᤷ�ޤ���
 
93
--
 
94
rule
 
95
  goal: def ruls source
 
96
        {
 
97
          result = val
 
98
        }
 
99
 
 
100
  def : /* none */
 
101
        {
 
102
          result = []
 
103
        }
 
104
      | def startdesig
 
105
        {
 
106
          result[0] = val[1]
 
107
        }
 
108
      | def
 
109
          precrule   # ����Ͼ�ιԤ�³��
 
110
        {
 
111
          result[1] = val[1]
 
112
        }
 
113
(ά)
 
114
--
 
115
�����������Ǥ����̤ʰ�̣���ä��ѿ��������Ĥ��Ȥ��ޤ���
 
116
���Τ褦���ѿ���ʲ��˼����ޤ�����̤���� yacc �Ǥ�ɽ���Ǥ���
 
117
 
 
118
  * result ($$)
 
119
 
 
120
���դ��͡�����ͤ� val[0] �Ǥ���
 
121
 
 
122
  * val ($1,$2,$3��)
 
123
 
 
124
���դε�����ͤ�����Ruby ������ʤΤ���������ǥå����ϥ�������Ϥޤ�ޤ���
 
125
����������������ΤǼ�ͳ���ѹ�������ΤƤ��ꤷ�ƹ����ޤ���
 
126
 
 
127
  * _values (...,$-2,$-1,$0)
 
128
 
 
129
�ͥ����å���Racc �������ȤäƤ��륪�֥������Ȥ����Τޤ��Ϥ���ޤ���
 
130
�����ѿ��ΰ�̣���狼��Ͱʳ���<em>���Ф�</em>�ѹ����ƤϤ����ޤ���
 
131
 
 
132
�ޤ��������������̤ʷ����ˡ���ᤳ�ߥ��������Ȥ�����Τ�����ޤ���
 
133
����ϥȡ������������ι����ʤȤ����˵��Ҥ��뤳�Ȥ��Ǥ��ޤ���
 
134
�ʲ�����ᤳ�ߥ�����������򼨤��ޤ���
 
135
--
 
136
target: A B { puts 'test test' } C D { normal action }
 
137
--
 
138
���Τ褦�˵��Ҥ���� A B �򸡽Ф��������� puts ���¹Ԥ���ޤ���
 
139
�ޤ�����ᤳ�ߥ��������Ϥ��켫�Τ��ͤ�����ޤ����Ĥޤꡢ�ʲ�����ˤ�����
 
140
--
 
141
target: A { result = 1 } B { p val[1] }
 
142
--
 
143
�Ǹ�ˤ��� p val[1] ����ᤳ�ߥ����������� 1 ��ɽ�����ޤ���
 
144
B ���ͤǤϤ���ޤ���
 
145
 
 
146
��̣Ū�ˤϡ���ᤳ�ߥ��������϶��ε�§�������ü������ɲä��뤳�Ȥ�
 
147
����Ʊ��Ư���򤷤ޤ����Ĥޤꡢ�����ϼ��Υ����ɤȴ�����Ʊ����̣�Ǥ���
 
148
--
 
149
target  : A nonterm B { p val[1] }
 
150
nonterm : /* ���ε�§ */ { result = 1 }
 
151
--
 
152
 
 
153
=== �黻��ͥ����
 
154
 
 
155
����ȡ������ǥ��եȡ��Ը����ͤ������ä��Ȥ������Υȡ������
 
156
�黻��ͥ���̤����ꤷ�Ƥ���Ⱦ��ͤ��äǤ����礬����ޤ���
 
157
���Τ褦�ʤ�ΤȤ����ä�ͭ̾�ʤΤϿ����α黻�Ҥ� if...else ��ʸ�Ǥ���
 
158
 
 
159
ͥ���̤Dz��Ǥ���ʸˡ�ϡ����ޤ�ʸˡ�򤯤ߤ����Ƥ���
 
160
ͥ���̤ʤ��Ǥ�Ʊ�����̤����뤳�Ȥ��Ǥ��ޤ��������������Ƥ���
 
161
����ͥ���̤����ꤷ�Ʋ�褹��ۤ���ʸˡ���ñ�ˤǤ��ޤ���
 
162
 
 
163
���եȡ��Ը����ͤ������ä��Ȥ���Racc �Ϥޤ����ε�§�˽�̤�����
 
164
����Ƥ��뤫Ĵ�٤ޤ�����§�ν�̤ϡ����ε�§�ǰ��֤������ˤ���
 
165
��ü�ȡ������ͥ���̤Ǥ������Ȥ���
 
166
--
 
167
target: TERM_A nonterm_a TERM_B nonterm_b
 
168
--
 
169
�Τ褦�ʵ�§�ν�̤�TERM_B��ͥ���̤ˤʤ�ޤ����⤷TERM_B��
 
170
ͥ���̤����ꤵ��Ƥ��ʤ��ä��顢ͥ���̤Ǿ��ͤ��褹�뤳�Ȥ�
 
171
�Ǥ��ʤ���Ƚ�Ǥ�����Shift/Reduce conflict�פ���𤷤ޤ���
 
172
 
 
173
�黻�Ҥ�ͥ���̤ϤĤ��Τ褦�˽񤤤�������ޤ���
 
174
--
 
175
prechigh
 
176
  nonassoc PLUSPLUS
 
177
  left     MULTI DEVIDE
 
178
  left     PLUS MINUS
 
179
  right    '='
 
180
preclow
 
181
--
 
182
prechigh �˶ᤤ�Ԥˤ���ۤ�ͥ���̤ι⤤�ȡ�����Ǥ����岼��ޤ뤴��
 
183
�������ޤˤ��� preclow...prechigh �ν��֤˽񤯤��Ȥ�Ǥ��ޤ���left
 
184
�ʤɤ�ɬ���Ԥκǽ�ˤʤ���Ф����ޤ���
 
185
 
 
186
left right nonassoc �Ϥ��줾��ַ�����פ�ɽ���ޤ���������ˤ�äơ�
 
187
Ʊ����̤α黻�Ҥε�§�����ͤ������˥��եȴԸ��Τɤ����Ȥ뤫��
 
188
��ޤ�ޤ������Ȥ���
 
189
--
 
190
a - b - c
 
191
--
 
192
��
 
193
--
 
194
(a - b) - c
 
195
--
 
196
�ˤʤ�Τ������ (left) �Ǥ�����§�黻�����̤���Ǥ���
 
197
����
 
198
--
 
199
a - (b - c)
 
200
--
 
201
�ˤʤ�Τ������ (right) �Ǥ��������Υ������Ȥ����� right �Ǥ���
 
202
�ޤ����Τ褦�˱黻�Ҥ��Ťʤ�Τϥ��顼�Ǥ����硢���� (nonassoc) �Ǥ���
 
203
C ����� ++ ��ñ��Υޥ��ʥ��ʤɤ�����ˤ�����ޤ���
 
204
 
 
205
�Ȥ����ǡ����������Ȥ����̾�ϴԸ����뵬§�κǸ�Υȡ����󤬽�̤�
 
206
����ΤǤ��������뵬§�˸¤äƤ��Υȡ�����Ȥϰ㤦��̤ˤ��������Ȥ�
 
207
����ޤ����㤨�����ȿž�Υޥ��ʥ��ϰ������Υޥ��ʥ�����̤�⤯
 
208
���ʤ��Ȥ����ޤ��󡣤��Τ褦�ʾ�� yacc �Ǥ� %prec ��Ȥ��ޤ���
 
209
racc �Ǥϥ������뵭���Ȥä�Ʊ�����Ȥ�Ǥ��ޤ���
 
210
--
 
211
prechigh
 
212
  nonassoc UMINUS
 
213
  left '*' '/'
 
214
  left '+' '-'
 
215
preclow
 
216
(ά)
 
217
exp: exp '*' exp
 
218
   | exp '-' exp
 
219
   | '-' exp     = UMINUS    # ����������̤�夲��
 
220
--
 
221
���Τ褦�˵��Ҥ���ȡ�'-' exp �ε�§�ν�̤� UMINUS �ν�̤ˤʤ�ޤ���
 
222
�������뤳�Ȥ����ȿž�� '-' �� '*' �����̤��⤯�ʤ�Τǡ�
 
223
�տޤɤ���ˤʤ�ޤ���
 
224
 
 
225
=== �ȡ��������
 
226
 
 
227
�ȡ�����(��ü����)�ΤĤŤ��ְ㤨��Ȥ����ΤϤ褯���뤳�ȤǤ�����
 
228
ȯ������ΤϤʤ��ʤ��񤷤���ΤǤ���1.1.5 ����ϥȡ����������Ū��
 
229
������뤳�Ȥǡ�����ˤʤ��ȡ����� / ����ˤ�������ȡ�������Ф���
 
230
�ٹ𤬽Ф�褦�ˤʤ�ޤ�����yacc �� %token �Ȼ��Ƥ��ޤ�������ΰ㤤��
 
231
racc �Ǥ�ɬ�ܤǤϤʤ��������⥨�顼�ˤʤ餺�ٹ�������Ȥ������Ǥ���
 
232
 
 
233
�ȡ���������ϰʲ��Τ褦�˽񤭤ޤ���
 
234
--
 
235
token A B C D
 
236
        E F G H
 
237
--
 
238
�ȡ�����Υꥹ�Ȥ�ʣ���Ԥˤ錄�äƽ񤱤뤳�Ȥ����ܤ��Ƥ���������
 
239
racc �Ǥϰ��̤ˡ�ͽ���פϹԤ���Ƭ���褿������ͽ���Ȥߤʤ����Τ�
 
240
prechigh �ʤɤ⥷��ܥ�Ȥ��ƻȤ��ޤ�����������ʥ����ͳ���� end ������
 
241
�ɤ���äƤ�ͽ���ˤʤäƤ��ޤ��ޤ���
 
242
 
 
243
=== ���ץ����
 
244
 
 
245
racc �Υ��ޥ�ɥ饤�󥪥ץ����ΰ�����ե�������˥ǥե������
 
246
�Ȥ��Ƶ��Ҥ��뤳�Ȥ��Ǥ��ޤ���
 
247
--
 
248
options ���ץ���� ���ץ���� ��
 
249
--
 
250
���ߤ����ǻȤ���Τ�
 
251
 
 
252
  * omit_action_call
 
253
 
 
254
���Υ��������ƤӽФ����ά����
 
255
 
 
256
  * result_var
 
257
 
 
258
�ѿ� result ��Ȥ�
 
259
 
 
260
�Ǥ���
 
261
���줾�� no_ ��Ƭ�ˤĤ��뤳�Ȥǰ�̣��ȿž�Ǥ��ޤ���
 
262
 
 
263
=== expect
 
264
 
 
265
���Ѥˤʤ�ѡ����Ϥ����Ƥ�̵���� shift/reduce conflict ��ޤߤޤ���
 
266
������ʸˡ�ե������񤤤��ܿͤϤ�����ΤäƤ��뤫�餤���Ǥ�����
 
267
�桼����ʸˡ�ե����������������ˡ�conflict�פ�ɽ�����줿��
 
268
�԰¤˻פ��Ǥ��礦�����Τ褦�ʾ�硢�ʲ��Τ褦�˽񤤤Ƥ�����
 
269
shift/reduce conflict �Υ�å������������Ǥ��ޤ���
 
270
--
 
271
expect 3
 
272
--
 
273
���ξ�� shift/reduce conflict �ϤԤä��껰�ĤǤʤ���Ф����ޤ���
 
274
���ĤǤʤ����Ϥ�Ϥ�ɽ�����Фޤ� (�����Ǥ�Фޤ�)��
 
275
�ޤ� reduce/reduce conflict ��ɽ���������Ǥ��ޤ���
 
276
 
 
277
=== �ȡ����󥷥�ܥ��ͤ��ѹ�
 
278
 
 
279
�ȡ����󥷥�ܥ��ɽ���ͤϡ��ǥե���ȤǤ�
 
280
 
 
281
  * ʸˡ�桢������Ǥ����ޤ�Ƥ��ʤ���� (RULE�Ȥ�XEND�Ȥ�)
 
282
    ������̾����ʸ����� intern ���������륷��ܥ� (1.4 �Ǥ� Fixnum)
 
283
  * ������Ǥ����ޤ�Ƥ�����(':'�Ȥ�'.'�Ȥ�)
 
284
    ������ʸ���󤽤Τޤ�
 
285
 
 
286
�ȤʤäƤ��ޤ��������Ȥ���¾�η����Υ�����ʤ����Ǥ�¸�ߤ�����ʤɤϡ�
 
287
����ˤ��碌�ʤ���Фʤ餺�����ΤޤޤǤ����ؤǤ������Τ褦�ʾ��ˤϡ�
 
288
convert ���ä��뤳�Ȥǡ��ȡ����󥷥�ܥ��ɽ���ͤ��Ѥ��뤳�Ȥ��Ǥ��ޤ���
 
289
�ʲ���������Ǥ���
 
290
--
 
291
convert
 
292
  PLUS 'PlusClass'      #�� PlusClass
 
293
  MIN  'MinusClass'     #�� MinusClass
 
294
end
 
295
--
 
296
�ǥե���ȤǤϥȡ����󥷥�ܥ� PLUS ���Ф��Ƥϥȡ����󥷥�ܥ��ͤ�
 
297
:PLUS �Ǥ�������Τ褦�ʵ��Ҥ�������� PlusClass �ˤʤ�ޤ���
 
298
�Ѵ�����ͤ� false��nil �ʳ��ʤ�ʤ�Ǥ�Ȥ��ޤ���
 
299
 
 
300
�Ѵ�����ͤȤ���ʸ�����Ȥ��Ȥ��ϡ����Τ褦�˰������Ťͤ�ɬ�פ�����ޤ���
 
301
--
 
302
convert
 
303
  PLUS '"plus"'       #�� "plus"
 
304
end
 
305
--
 
306
�ޤ�����'�פ�ȤäƤ��������줿 Ruby �Υ����ɾ�Ǥϡ�"�פˤʤ�Τ�
 
307
���դ��Ƥ����������Хå�����å���ˤ�륯�����Ȥ�ͭ���Ǥ������Хå�
 
308
����å���Ͼä����ˤ��Τޤ޻Ĥ�ޤ���
 
309
--
 
310
PLUS '"plus\n"'          #�� "plus\n"
 
311
MIN  "\"minus#{val}\""   #�� \"minus#{val}\"
 
312
--
 
313
 
 
314
=== �������ȵ�§
 
315
 
 
316
�ѡ�����Ĥ��뤿��ˤϡ��ɤε�§���ֺǽ�Ρ׵�§�����Ȥ������Ȥ� Racc �ˤ�������
 
317
���ʤ���Ф����ޤ��󡣤��������Ū�˽񤯤Τ��������ȵ�§�Ǥ����������ȵ�§��
 
318
���Τ褦�˽񤭤ޤ���
 
319
--
 
320
start real_target
 
321
--
 
322
start �ϹԤκǽ�ˤ��ʤ���Ф����ޤ��󡣤��Τ褦�˽񤯤ȡ��ե������
 
323
���ֺǽ�˽ФƤ��� real_target �ε�§�򥹥����ȵ�§�Ȥ��ƻȤ��ޤ���
 
324
��ά�������ϡ��ե�����κǽ�ε�§���������ȵ�§�ˤʤ�ޤ������̤�
 
325
�ǽ�ε�§����־�ˤ����ۤ����񤭤䤹�����狼��䤹���ʤ�ޤ����顢
 
326
���ε�ˡ�Ϥ��ޤ�Ĥ���ɬ�פϤʤ��Ǥ��礦��
 
327
 
 
328
=== �桼������������
 
329
 
 
330
�桼���������ɤϡ��ѡ������饹���񤭤��ޤ��ե�����ˡ�
 
331
����������¾�ˤ⥳���ɤ�ޤ᤿�����˻Ȥ��ޤ������Τ褦�ʤ�Τ�
 
332
�񤭤��ޤ����˱����ƻ���¸�ߤ����ѡ������饹�����������
 
333
header�����饹�������(����Ƭ)�� inner������θ夬 footer �Ǥ���
 
334
�桼�������ɤȤ��ƽ񤤤���Τ��������ä����ˤ��Τޤ�Ϣ�뤵��ޤ���
 
335
 
 
336
�桼�������������ν񼰤ϰʲ����̤�Ǥ���
 
337
--
 
338
---- ���̻�
 
339
  ruby ��ʸ
 
340
  ruby ��ʸ
 
341
  ruby ��ʸ
 
342
 
 
343
---- ���̻�
 
344
  ruby ��ʸ
 
345
     :
 
346
--
 
347
�Ԥ���Ƭ����ͤİʾ�Ϣ³������-��(�ޥ��ʥ�)������ȥ桼���������ɤ�
 
348
�ߤʤ���ޤ������̻Ҥϰ�Ĥ�ñ��ǡ����Τ��Ȥˤϡ�=�װʳ��ʤ鲿��
 
349
�񤤤Ƥ⤫�ޤ��ޤ���
 
350
e
 
351
= Racc Grammar File Reference
 
352
 
 
353
== Global Structure
 
354
 
 
355
== Class Block and User Code Block
 
356
 
 
357
There's two block on toplevel.
 
358
one is 'class' block, another is 'user code' block. 'user code' block MUST
 
359
places after 'class' block.
 
360
 
 
361
== Comment
 
362
 
 
363
You can insert comment about all places. Two style comment can be used,
 
364
Ruby style (#.....) and C style (/*......*/) .
 
365
 
 
366
== Class Block
 
367
 
 
368
The class block is formed like this:
 
369
--
 
370
class CLASS_NAME
 
371
  [precedance table]
 
372
  [token declearations]
 
373
  [expected number of S/R conflict]
 
374
  [options]
 
375
  [semantic value convertion]
 
376
  [start rule]
 
377
rule
 
378
  GRAMMARS
 
379
--
 
380
CLASS_NAME is a name of parser class.
 
381
This is the name of generating parser class.
 
382
 
 
383
If CLASS_NAME includes '::', Racc outputs module clause.
 
384
For example, writing "class M::C" causes creating the code bellow:
 
385
--
 
386
module M
 
387
  class C
 
388
    :
 
389
    :
 
390
  end
 
391
end
 
392
--
 
393
 
 
394
== Grammar Block
 
395
 
 
396
The grammar block discripts grammar which is able
 
397
to be understood by parser.  Syntax is:
 
398
--
 
399
(token): (token) (token) (token).... (action)
 
400
 
 
401
(token): (token) (token) (token).... (action)
 
402
       | (token) (token) (token).... (action)
 
403
       | (token) (token) (token).... (action)
 
404
--
 
405
(action) is an action which is executed when its (token)s are found.
 
406
(action) is a ruby code block, which is surrounded by braces:
 
407
--
 
408
{ print val[0]
 
409
  puts val[1] }
 
410
--
 
411
Note that you cannot use '%' string, here document, '%r' regexp in action.
 
412
 
 
413
Actions can be omitted.
 
414
When it is omitted, '' (empty string) is used.
 
415
 
 
416
A return value of action is a value of left side value ($$).
 
417
It is value of result, or returned value by "return" statement.
 
418
 
 
419
Here is an example of whole grammar block.
 
420
--
 
421
rule
 
422
  goal: definition ruls source { result = val }
 
423
 
 
424
  definition: /* none */   { result = [] }
 
425
    | definition startdesig  { result[0] = val[1] }
 
426
    | definition
 
427
             precrule   # this line continue from upper line
 
428
      {
 
429
        result[1] = val[1]
 
430
      }
 
431
 
 
432
  startdesig: START TOKEN
 
433
--
 
434
You can use following special local variables in action.
 
435
 
 
436
  * result ($$)
 
437
 
 
438
The value of left-hand side (lhs). A default value is val[0].
 
439
 
 
440
  * val ($1,$2,$3...)
 
441
 
 
442
An array of value of right-hand side (rhs).
 
443
 
 
444
  * _values (...$-2,$-1,$0)
 
445
 
 
446
A stack of values.
 
447
DO NOT MODIFY this stack unless you know what you are doing.
 
448
 
 
449
== Operator Precedance
 
450
 
 
451
This function is equal to '%prec' in yacc.
 
452
To designate this block:
 
453
--
 
454
prechigh
 
455
  nonassoc '++'
 
456
  left     '*' '/'
 
457
  left     '+' '-'
 
458
  right    '='
 
459
preclow
 
460
--
 
461
`right' is yacc's %right, `left' is yacc's %left.
 
462
 
 
463
`=' + (symbol) means yacc's %prec:
 
464
--
 
465
prechigh
 
466
  nonassoc UMINUS
 
467
  left '*' '/'
 
468
  left '+' '-'
 
469
preclow
 
470
 
 
471
rule
 
472
  exp: exp '*' exp
 
473
     | exp '-' exp
 
474
     | '-' exp       =UMINUS   # equals to "%prec UMINUS"
 
475
         :
 
476
         :
 
477
--
 
478
 
 
479
== expect
 
480
 
 
481
Racc has bison's "expect" directive.
 
482
--
 
483
# Example
 
484
 
 
485
class MyParser
 
486
rule
 
487
  expect 3
 
488
    :
 
489
    :
 
490
--
 
491
This directive declears "expected" number of shift/reduce conflict.
 
492
If "expected" number is equal to real number of conflicts,
 
493
racc does not print confliction warning message.
 
494
 
 
495
== Declaring Tokens
 
496
 
 
497
By declaring tokens, you can avoid many meanless bugs.
 
498
If decleared token does not exist/existing token does not decleared,
 
499
Racc output warnings.  Declearation syntax is:
 
500
--
 
501
token TOKEN_NAME AND_IS_THIS
 
502
      ALSO_THIS_IS AGAIN_AND_AGAIN THIS_IS_LAST
 
503
--
 
504
 
 
505
== Options
 
506
 
 
507
You can write options for racc command in your racc file.
 
508
--
 
509
options OPTION OPTION ...
 
510
--
 
511
Options are:
 
512
 
 
513
  * omit_action_call
 
514
 
 
515
omit empty action call or not.
 
516
 
 
517
  * result_var
 
518
 
 
519
use/does not use local variable "result"
 
520
 
 
521
You can use 'no_' prefix to invert its meanings.
 
522
 
 
523
== Converting Token Symbol
 
524
 
 
525
Token symbols are, as default,
 
526
 
 
527
  * naked token string in racc file (TOK, XFILE, this_is_token, ...)
 
528
    --&gt; symbol (:TOK, :XFILE, :this_is_token, ...)
 
529
  * quoted string (':', '.', '(', ...)
 
530
    --&gt; same string (':', '.', '(', ...)
 
531
 
 
532
You can change this default by "convert" block.
 
533
Here is an example:
 
534
--
 
535
convert
 
536
  PLUS 'PlusClass'      # We use PlusClass for symbol of `PLUS'
 
537
  MIN  'MinusClass'     # We use MinusClass for symbol of `MIN'
 
538
end
 
539
--
 
540
We can use almost all ruby value can be used by token symbol,
 
541
except 'false' and 'nil'.  These are causes unexpected parse error.
 
542
 
 
543
If you want to use String as token symbol, special care is required.
 
544
For example:
 
545
--
 
546
convert
 
547
  class '"cls"'            # in code, "cls"
 
548
  PLUS '"plus\n"'          # in code, "plus\n"
 
549
  MIN  "\"minus#{val}\""   # in code, \"minus#{val}\"
 
550
end
 
551
--
 
552
 
 
553
== Start Rule
 
554
 
 
555
'%start' in yacc. This changes start rule.
 
556
--
 
557
start real_target
 
558
--
 
559
This statement will not be used forever, I think.
 
560
 
 
561
== User Code Block
 
562
 
 
563
"User Code Block" is a Ruby source code which is copied to output.
 
564
There are three user code block, "header" "inner" and "footer".
 
565
 
 
566
Format of user code is like this:
 
567
--
 
568
---- header
 
569
  ruby statement
 
570
  ruby statement
 
571
  ruby statement
 
572
 
 
573
---- inner
 
574
  ruby statement
 
575
     :
 
576
     :
 
577
--
 
578
If four '-' exist on line head,
 
579
racc treat it as beginning of user code block.
 
580
A name of user code must be one word.
 
581
.