From 56912c3cc63b8d6a4b4e4062d85c5b522d8f2589 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Thu, 15 Feb 2024 02:02:07 +0900 Subject: [PATCH] Eliminate parse errors --- lib/lrama/grammar.rb | 6 +++ lib/lrama/grammar/inline.rb | 3 ++ lib/lrama/parser.rb | 84 +++++++++++++++++----------------- parser.y | 2 +- spec/fixtures/inlining/basic.y | 11 +++-- 5 files changed, 58 insertions(+), 48 deletions(-) create mode 100644 lib/lrama/grammar/inline.rb diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index 7ccde1aa..2df06b4c 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -4,6 +4,7 @@ require "lrama/grammar/code" require "lrama/grammar/counter" require "lrama/grammar/error_token" +require "lrama/grammar/inline" require "lrama/grammar/parameterizing_rule" require "lrama/grammar/percent_code" require "lrama/grammar/precedence" @@ -50,6 +51,7 @@ def initialize(rule_counter) @rules = [] @sym_to_rules = {} @parameterizing_rule_resolver = ParameterizingRule::Resolver.new + @inline_resolver = Inline::Resolver.new @empty_symbol = nil @eof_symbol = nil @error_symbol = nil @@ -118,6 +120,10 @@ def insert_before_parameterizing_rules(rules) @parameterizing_rule_resolver.rules = rules + @parameterizing_rule_resolver.rules end + def add_inline_rule(rule) + @inline_resolver.add_inline_rule(rule) + end + def prologue_first_lineno=(prologue_first_lineno) @aux.prologue_first_lineno = prologue_first_lineno end diff --git a/lib/lrama/grammar/inline.rb b/lib/lrama/grammar/inline.rb new file mode 100644 index 00000000..ef0f630d --- /dev/null +++ b/lib/lrama/grammar/inline.rb @@ -0,0 +1,3 @@ +require_relative 'inline/resolver' +require_relative 'inline/rhs' +require_relative 'inline/rule' diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 95f285e9..967b2445 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -755,7 +755,7 @@ def raise_parse_error(error_message, location) 80, 52, 52, 206, 51, 80, 189, 190, 191, 97, 52, 52, 51, 51, 52, 52, 51, 51, 159, 220, 160, 221, 189, 190, 191, 238, 240, 221, 221, 63, - 64, 65, 66, 67, 68, 89, 90, 91, 95, 97, + 64, 65, 66, 67, 68, 89, 48, 91, 95, 97, 102, 102, 102, 104, 110, 114, 115, 118, 118, 118, 118, 121, 122, 125, 126, 128, 130, 131, 132, 133, 134, 137, 141, 144, 145, 146, 149, 150, 151, 153, @@ -802,7 +802,7 @@ def raise_parse_error(error_message, location) nil, nil, nil, nil, 25, 100, 20, 24, 32, 50, 45, nil, nil, 52, nil, 61, 62, 210, 225, 226, 227, 228, 229, 13, 30, 37, 57, 58, 64, 230, - 231, 235, nil, 132, nil, nil, 215, 188, nil, nil, + 232, 235, nil, 132, nil, nil, 215, 188, nil, nil, -5, nil, nil, nil, 221, nil, 222, 223, 224, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 236, 107, 113, nil, 239, 238, 49, 168, nil, nil, nil, @@ -861,8 +861,8 @@ def raise_parse_error(error_message, location) 73, 73, 13, 50, 98, 129, 119, 173, 105, 111, 154, 106, 85, 85, 155, 107, 156, 108, 157, 135, 109, 158, 138, 69, 77, 74, 116, 113, 136, 140, - 202, 228, 241, 142, 183, 218, 127, 171, 103, 152, - 73, 147, 73, 192, 223, 124, nil, 116, 85, nil, + 202, 228, 241, 90, 142, 183, 218, 127, 171, 103, + 73, 152, 73, 147, 192, 223, 124, 116, 85, nil, 85, nil, 162, nil, nil, 182, nil, 168, nil, nil, nil, 187, nil, nil, nil, 73, nil, nil, nil, 85, nil, nil, nil, 184, nil, nil, nil, nil, nil, 193, @@ -874,17 +874,17 @@ def raise_parse_error(error_message, location) 233, nil, nil, 214, nil, 243, 208 ] racc_goto_check = [ - 42, 51, 33, 41, 34, 35, 57, 64, 43, 58, - 63, 1, 48, 15, 15, 15, 44, 68, 40, 59, + 42, 52, 33, 41, 34, 35, 58, 64, 43, 59, + 63, 1, 49, 15, 15, 15, 44, 68, 40, 60, 2, 68, 68, 35, 3, 35, 35, 35, 35, 32, 32, 32, 32, 21, 21, 21, 4, 5, 6, 64, - 33, 57, 7, 51, 41, 41, 34, 58, 43, 44, - 63, 48, 40, 59, 8, 43, 9, 44, 10, 11, - 35, 35, 12, 13, 14, 16, 58, 17, 18, 34, + 33, 58, 7, 52, 41, 41, 34, 59, 43, 44, + 63, 49, 40, 60, 8, 43, 9, 44, 10, 11, + 35, 35, 12, 13, 14, 16, 59, 17, 18, 34, 19, 22, 35, 35, 23, 24, 25, 26, 27, 33, - 28, 29, 51, 30, 41, 31, 41, 36, 37, 38, - 39, 45, 46, 47, 49, 50, 53, 54, 55, 56, - 35, 62, 35, 65, 66, 67, nil, 41, 35, nil, + 28, 29, 52, 30, 41, 31, 41, 36, 37, 38, + 39, 45, 46, 47, 48, 50, 51, 54, 55, 56, + 35, 57, 35, 62, 65, 66, 67, 41, 35, nil, 35, nil, 41, nil, nil, 42, nil, 41, nil, nil, nil, 42, nil, nil, nil, 35, nil, nil, nil, 35, nil, nil, nil, 41, nil, nil, nil, nil, nil, 41, @@ -900,18 +900,18 @@ def raise_parse_error(error_message, location) 54, 55, 58, 48, 14, -3, -39, -86, 9, -60, nil, -143, 11, -57, 14, -56, 15, -55, 17, -53, 50, 51, -6, -31, -66, -10, 14, -25, -32, -89, - -161, -31, -47, -160, -170, -125, -144, -29, -110, -69, - -110, -33, nil, -6, -54, 43, -29, -77, -73, 10, - nil, nil, -24, -115, -181, -66, -107, 11, -179 ] + -161, -31, -47, -160, -170, -125, -144, 53, -28, -110, + -68, -109, -33, nil, -5, -53, 44, -27, -77, -73, + 10, nil, -22, -115, -181, -65, -106, 12, -179 ] racc_goto_default = [ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 46, nil, nil, nil, nil, nil, nil, nil, nil, nil, 24, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 72, 78, nil, nil, nil, nil, - nil, 198, nil, nil, nil, nil, nil, nil, nil, nil, - nil, nil, 79, nil, nil, nil, nil, 82, 84, nil, - 45, 47, nil, nil, nil, nil, nil, nil, 197 ] + nil, 198, nil, nil, nil, nil, nil, 47, nil, nil, + nil, nil, nil, 79, nil, nil, nil, nil, 82, 84, + nil, 45, nil, nil, nil, nil, nil, nil, 197 ] racc_reduce_table = [ 0, 0, :racc_error, @@ -985,15 +985,15 @@ def raise_parse_error(error_message, location) 7, 94, :_reduce_68, 3, 94, :_reduce_69, 4, 66, :_reduce_70, - 1, 101, :_reduce_71, - 3, 101, :_reduce_72, - 0, 102, :_reduce_73, - 1, 102, :_reduce_74, - 3, 102, :_reduce_75, - 0, 103, :_reduce_76, - 0, 104, :_reduce_77, - 7, 102, :_reduce_78, - 3, 102, :_reduce_79, + 1, 102, :_reduce_71, + 3, 102, :_reduce_72, + 0, 103, :_reduce_73, + 1, 103, :_reduce_74, + 3, 103, :_reduce_75, + 0, 104, :_reduce_76, + 0, 105, :_reduce_77, + 7, 103, :_reduce_78, + 3, 103, :_reduce_79, 0, 90, :_reduce_none, 1, 90, :_reduce_none, 0, 91, :_reduce_none, @@ -1001,29 +1001,29 @@ def raise_parse_error(error_message, location) 1, 85, :_reduce_84, 2, 85, :_reduce_85, 3, 85, :_reduce_86, - 1, 105, :_reduce_87, - 2, 105, :_reduce_88, + 1, 106, :_reduce_87, + 2, 106, :_reduce_88, 1, 95, :_reduce_none, 1, 95, :_reduce_none, - 0, 107, :_reduce_91, - 0, 108, :_reduce_92, + 0, 108, :_reduce_91, + 0, 109, :_reduce_92, 6, 69, :_reduce_93, - 0, 109, :_reduce_94, - 0, 110, :_reduce_95, + 0, 110, :_reduce_94, + 0, 111, :_reduce_95, 5, 69, :_reduce_96, 1, 86, :_reduce_97, 2, 86, :_reduce_98, 3, 86, :_reduce_99, - 1, 111, :_reduce_100, - 2, 111, :_reduce_101, - 1, 112, :_reduce_none, + 1, 112, :_reduce_100, + 2, 112, :_reduce_101, + 1, 113, :_reduce_none, 1, 89, :_reduce_103, 1, 89, :_reduce_104, 1, 58, :_reduce_none, 2, 58, :_reduce_none, - 1, 113, :_reduce_none, - 2, 113, :_reduce_none, - 4, 114, :_reduce_109, + 1, 114, :_reduce_none, + 2, 114, :_reduce_none, + 4, 115, :_reduce_109, 1, 116, :_reduce_110, 3, 116, :_reduce_111, 2, 116, :_reduce_none, @@ -1045,7 +1045,7 @@ def raise_parse_error(error_message, location) 4, 98, :_reduce_128, 0, 96, :_reduce_none, 3, 96, :_reduce_130, - 1, 115, :_reduce_none, + 1, 101, :_reduce_none, 0, 59, :_reduce_none, 0, 121, :_reduce_133, 3, 59, :_reduce_134, @@ -1058,7 +1058,7 @@ def raise_parse_error(error_message, location) 2, 75, :_reduce_141, 1, 122, :_reduce_none, 1, 122, :_reduce_none, - 1, 106, :_reduce_144, + 1, 107, :_reduce_144, 0, 118, :_reduce_none, 1, 118, :_reduce_none ] @@ -1245,6 +1245,7 @@ def raise_parse_error(error_message, location) "parameterizing_args", "@15", "@16", + "id_colon", "inline_rhs_list", "inline_rhs", "@17", @@ -1259,7 +1260,6 @@ def raise_parse_error(error_message, location) "token_declaration_for_precedence", "rules_or_grammar_declaration", "rules", - "id_colon", "rhs_list", "rhs", "tag_opt", diff --git a/parser.y b/parser.y index 42f4aedd..b1b2f262 100644 --- a/parser.y +++ b/parser.y @@ -304,7 +304,7 @@ rule result = builder } - inline_declarations: "%inline" IDENTIFIER ":" inline_rhs_list + inline_declarations: "%inline" id_colon ":" inline_rhs_list { rule = Grammar::Inline::Rule.new(val[1].s_value, val[3]) @grammar.add_inline_rule(rule) diff --git a/spec/fixtures/inlining/basic.y b/spec/fixtures/inlining/basic.y index f9e9fe87..0d1bc715 100644 --- a/spec/fixtures/inlining/basic.y +++ b/spec/fixtures/inlining/basic.y @@ -14,15 +14,16 @@ static int yyerror(YYLTYPE *loc, const char *str); %token number -%inline op: '+' { (+) } - | '-' { (-) } - | '*' { (*) } - | '/' { (/) } +%inline op : '+' { (+) } + | '-' { (-) } + | '*' { (*) } + | '/' { (/) } + ; %% expression : number - : expression op expression { $$ = $1 $2 $3; } + | expression op expression { $$ = $1 $2 $3; } ; %%