forked from ruby/lrama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
485 lines (435 loc) · 17.2 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
class Lrama::Parser
expect 0
error_on_expect_mismatch
token C_DECLARATION CHARACTER IDENT_COLON IDENTIFIER INTEGER STRING TAG
rule
input: prologue_declaration* bison_declaration* "%%" rules_or_grammar_declaration+ epilogue_declaration?
prologue_declaration: "%{"
{
begin_c_declaration("%}")
@grammar.prologue_first_lineno = @lexer.line
}
C_DECLARATION
{
end_c_declaration
}
"%}"
{
@grammar.prologue = val[2].s_value
}
| "%require" STRING
bison_declaration: grammar_declaration
| "%expect" INTEGER { @grammar.expect = val[1] }
| "%define" variable value
| "%param" param+
| "%lex-param" param+
{
val[1].each {|token|
@grammar.lex_param = Grammar::Code::NoReferenceCode.new(type: :lex_param, token_code: token).token_code.s_value
}
}
| "%parse-param" param+
{
val[1].each {|token|
@grammar.parse_param = Grammar::Code::NoReferenceCode.new(type: :parse_param, token_code: token).token_code.s_value
}
}
| "%code" IDENTIFIER param
{
@grammar.add_percent_code(id: val[1], code: val[2])
}
| "%initial-action" param
{
@grammar.initial_action = Grammar::Code::InitialActionCode.new(type: :initial_action, token_code: val[1])
}
| "%no-stdlib" { @grammar.no_stdlib = true }
| "%locations" { @grammar.locations = true }
| bison_declaration ";"
grammar_declaration: "%union" param
{
@grammar.set_union(
Grammar::Code::NoReferenceCode.new(type: :union, token_code: val[1]),
val[1].line
)
}
| symbol_declaration
| rule_declaration
| inline_declaration
| "%destructor" param generic_symbol+
{
@grammar.add_destructor(
ident_or_tags: val[2],
token_code: val[1],
lineno: val[1].line
)
}
| "%printer" param generic_symbol+
{
@grammar.add_printer(
ident_or_tags: val[2],
token_code: val[1],
lineno: val[1].line
)
}
| "%error-token" param generic_symbol+
{
@grammar.add_error_token(
ident_or_tags: val[2],
token_code: val[1],
lineno: val[1].line
)
}
| "%after-shift" IDENTIFIER
{
@grammar.after_shift = val[1]
}
| "%before-reduce" IDENTIFIER
{
@grammar.before_reduce = val[1]
}
| "%after-reduce" IDENTIFIER
{
@grammar.after_reduce = val[1]
}
| "%after-shift-error-token" IDENTIFIER
{
@grammar.after_shift_error_token = val[1]
}
| "%after-pop-stack" IDENTIFIER
{
@grammar.after_pop_stack = val[1]
}
symbol_declaration: "%token" token_declarations
| "%type" symbol_declarations
{
val[1].each {|hash|
hash[:tokens].each {|id|
@grammar.add_type(id: id, tag: hash[:tag])
}
}
}
| "%left" token_declarations_for_precedence
{
val[1].each {|hash|
hash[:tokens].each {|id|
sym = @grammar.add_term(id: id)
@grammar.add_left(sym, @precedence_number)
}
}
@precedence_number += 1
}
| "%right" token_declarations_for_precedence
{
val[1].each {|hash|
hash[:tokens].each {|id|
sym = @grammar.add_term(id: id)
@grammar.add_right(sym, @precedence_number)
}
}
@precedence_number += 1
}
| "%precedence" token_declarations_for_precedence
{
val[1].each {|hash|
hash[:tokens].each {|id|
sym = @grammar.add_term(id: id)
@grammar.add_precedence(sym, @precedence_number)
}
}
@precedence_number += 1
}
| "%nonassoc" token_declarations_for_precedence
{
val[1].each {|hash|
hash[:tokens].each {|id|
sym = @grammar.add_term(id: id)
@grammar.add_nonassoc(sym, @precedence_number)
}
}
@precedence_number += 1
}
token_declarations: token_declaration+
{
val[0].each {|token_declaration|
@grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: nil, replace: true)
}
}
| TAG token_declaration+
{
val[1].each {|token_declaration|
@grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: val[0], replace: true)
}
}
| token_declarations TAG token_declaration+
{
val[2].each {|token_declaration|
@grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: val[1], replace: true)
}
}
token_declaration: id INTEGER? alias { result = val }
rule_declaration: "%rule" IDENTIFIER "(" rule_args ")" TAG? ":" rule_rhs_list
{
rule = Grammar::ParameterizingRule::Rule.new(val[1].s_value, val[3], val[7], tag: val[5])
@grammar.add_parameterizing_rule(rule)
}
inline_declaration: "%rule" "%inline" IDENT_COLON ":" rule_rhs_list
{
rule = Grammar::ParameterizingRule::Rule.new(val[2].s_value, [], val[4], is_inline: true)
@grammar.add_parameterizing_rule(rule)
}
| "%rule" "%inline" IDENTIFIER "(" rule_args ")" ":" rule_rhs_list
{
rule = Grammar::ParameterizingRule::Rule.new(val[2].s_value, val[4], val[7], is_inline: true)
@grammar.add_parameterizing_rule(rule)
}
rule_args: IDENTIFIER { result = [val[0]] }
| rule_args "," IDENTIFIER { result = val[0].append(val[2]) }
rule_rhs_list: rule_rhs
{
builder = val[0]
result = [builder]
}
| rule_rhs_list "|" rule_rhs
{
builder = val[2]
result = val[0].append(builder)
}
rule_rhs: empty
{
reset_precs
result = Grammar::ParameterizingRule::Rhs.new
}
| rule_rhs symbol named_ref?
{
token = val[1]
token.alias_name = val[2]
builder = val[0]
builder.symbols << token
result = builder
}
| rule_rhs symbol parameterizing_suffix
{
builder = val[0]
builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]])
result = builder
}
| rule_rhs IDENTIFIER "(" parameterizing_args ")" TAG?
{
builder = val[0]
builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5])
result = builder
}
| rule_rhs midrule_action named_ref?
{
user_code = val[1]
user_code.alias_name = val[2]
builder = val[0]
builder.user_code = user_code
result = builder
}
| rule_rhs "%prec" symbol
{
sym = @grammar.find_symbol_by_id!(val[2])
@prec_seen = true
builder = val[0]
builder.precedence_sym = sym
result = builder
}
alias: # empty
| string_as_id { result = val[0].s_value }
symbol_declarations: symbol+ { result = [{tag: nil, tokens: val[0]}] }
| TAG symbol+ { result = [{tag: val[0], tokens: val[1]}] }
| symbol_declarations TAG symbol+ { result = val[0].append({tag: val[1], tokens: val[2]}) }
symbol: id
| string_as_id
param: "{" {
begin_c_declaration("}")
}
C_DECLARATION
{
end_c_declaration
}
"}"
{
result = val[2]
}
token_declarations_for_precedence: id+ { result = [{tag: nil, tokens: val[0]}] }
| TAG id+ { result = [{tag: val[0], tokens: val[1]}] }
| id TAG id+ { result = val[0].append({tag: val[1], tokens: val[2]}) }
id: IDENTIFIER { on_action_error("ident after %prec", val[0]) if @prec_seen }
| CHARACTER { on_action_error("char after %prec", val[0]) if @prec_seen }
rules_or_grammar_declaration: rules ";"?
| grammar_declaration ";"
rules: IDENT_COLON named_ref? ":" rhs_list
{
lhs = val[0]
lhs.alias_name = val[1]
val[3].each do |builder|
builder.lhs = lhs
builder.complete_input
@grammar.add_rule_builder(builder)
end
}
rhs_list: rhs
{
builder = val[0]
if !builder.line
builder.line = @lexer.line - 1
end
result = [builder]
}
| rhs_list "|" rhs
{
builder = val[2]
if !builder.line
builder.line = @lexer.line - 1
end
result = val[0].append(builder)
}
rhs: empty
{
reset_precs
result = @grammar.create_rule_builder(@rule_counter, @midrule_action_counter)
}
| rhs symbol named_ref?
{
token = val[1]
token.alias_name = val[2]
builder = val[0]
builder.add_rhs(token)
result = builder
}
| rhs symbol parameterizing_suffix named_ref? TAG?
{
token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], alias_name: val[3], location: @lexer.location, args: [val[1]], lhs_tag: val[4])
builder = val[0]
builder.add_rhs(token)
builder.line = val[1].first_line
result = builder
}
| rhs IDENTIFIER "(" parameterizing_args ")" named_ref? TAG?
{
token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, alias_name: val[5], location: @lexer.location, args: val[3], lhs_tag: val[6])
builder = val[0]
builder.add_rhs(token)
builder.line = val[1].first_line
result = builder
}
| rhs midrule_action named_ref? TAG?
{
user_code = val[1]
user_code.alias_name = val[2]
user_code.tag = val[3]
builder = val[0]
builder.user_code = user_code
result = builder
}
| rhs "%prec" symbol
{
sym = @grammar.find_symbol_by_id!(val[2])
@prec_seen = true
builder = val[0]
builder.precedence_sym = sym
result = builder
}
parameterizing_suffix: "?" { result = "option" }
| "+" { result = "nonempty_list" }
| "*" { result = "list" }
parameterizing_args: symbol { result = [val[0]] }
| parameterizing_args ',' symbol { result = val[0].append(val[2]) }
| symbol parameterizing_suffix { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[0])] }
| IDENTIFIER "(" parameterizing_args ")" { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] }
midrule_action: "{"
{
if @prec_seen
on_action_error("multiple User_code after %prec", val[0]) if @code_after_prec
@code_after_prec = true
end
begin_c_declaration("}")
}
C_DECLARATION
{
end_c_declaration
}
"}"
{
result = val[2]
}
named_ref: '[' IDENTIFIER ']' { result = val[1].s_value }
epilogue_declaration: "%%"
{
begin_c_declaration('\Z')
@grammar.epilogue_first_lineno = @lexer.line + 1
}
C_DECLARATION
{
end_c_declaration
@grammar.epilogue = val[2].s_value
}
variable: id
value: # empty
| IDENTIFIER
| STRING
| "{...}"
generic_symbol: symbol
| TAG
empty: /* empty */
| "%empty"
string_as_id: STRING { result = Lrama::Lexer::Token::Ident.new(s_value: val[0]) }
end
---- inner
include Lrama::Report::Duration
def initialize(text, path, debug = false)
@grammar_file = Lrama::Lexer::GrammarFile.new(path, text)
@yydebug = debug
@rule_counter = Lrama::Grammar::Counter.new(0)
@midrule_action_counter = Lrama::Grammar::Counter.new(1)
end
def parse
report_duration(:parse) do
@lexer = Lrama::Lexer.new(@grammar_file)
@grammar = Lrama::Grammar.new(@rule_counter)
@precedence_number = 0
reset_precs
do_parse
@grammar
end
end
def next_token
@lexer.next_token
end
def on_error(error_token_id, error_value, value_stack)
if error_value.is_a?(Lrama::Lexer::Token)
location = error_value.location
value = "'#{error_value.s_value}'"
else
location = @lexer.location
value = error_value.inspect
end
error_message = "parse error on value #{value} (#{token_to_str(error_token_id) || '?'})"
raise_parse_error(error_message, location)
end
def on_action_error(error_message, error_value)
if error_value.is_a?(Lrama::Lexer::Token)
location = error_value.location
else
location = @lexer.location
end
raise_parse_error(error_message, location)
end
private
def reset_precs
@prec_seen = false
@code_after_prec = false
end
def begin_c_declaration(end_symbol)
@lexer.status = :c_declaration
@lexer.end_symbol = end_symbol
end
def end_c_declaration
@lexer.status = :initial
@lexer.end_symbol = nil
end
def raise_parse_error(error_message, location)
raise ParseError, location.generate_error_message(error_message)
end