parser.y in lrama-0.6.9 vs parser.y in lrama-0.6.10

- old
+ new

@@ -1,17 +1,15 @@ class Lrama::Parser expect 0 + error_on_expect_mismatch token C_DECLARATION CHARACTER IDENT_COLON IDENTIFIER INTEGER STRING TAG rule - input: prologue_declarations bison_declarations "%%" grammar epilogue_opt + input: prologue_declaration* bison_declaration* "%%" rules_or_grammar_declaration+ epilogue_declaration? - prologue_declarations: # empty - | prologue_declarations prologue_declaration - prologue_declaration: "%{" { begin_c_declaration("%}") @grammar.prologue_first_lineno = @lexer.line } @@ -23,120 +21,70 @@ { @grammar.prologue = val[2].s_value } | "%require" STRING - bison_declarations: /* empty */ { result = "" } - | bison_declarations bison_declaration - bison_declaration: grammar_declaration - | rule_declaration - | inline_declaration | "%expect" INTEGER { @grammar.expect = val[1] } | "%define" variable value - | "%param" params - | "%lex-param" params + | "%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" params + | "%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 "{" + | "%code" IDENTIFIER param { - begin_c_declaration("}") + @grammar.add_percent_code(id: val[1], code: val[2]) } - C_DECLARATION + | "%initial-action" param { - end_c_declaration + @grammar.initial_action = Grammar::Code::InitialActionCode.new(type: :initial_action, token_code: val[1]) } - "}" - { - @grammar.add_percent_code(id: val[1], code: val[4]) - } - | "%initial-action" "{" - { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" - { - @grammar.initial_action = Grammar::Code::InitialActionCode.new(type: :initial_action, token_code: val[3]) - } | "%no-stdlib" { @grammar.no_stdlib = true } - | ";" + | "%locations" { @grammar.locations = true } + | bison_declaration ";" - grammar_declaration: "%union" "{" + grammar_declaration: "%union" param { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" - { @grammar.set_union( - Grammar::Code::NoReferenceCode.new(type: :union, token_code: val[3]), - val[3].line + Grammar::Code::NoReferenceCode.new(type: :union, token_code: val[1]), + val[1].line ) } | symbol_declaration - | "%destructor" "{" + | rule_declaration + | inline_declaration + | "%destructor" param generic_symbol+ { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" generic_symlist - { @grammar.add_destructor( - ident_or_tags: val[6], - token_code: val[3], - lineno: val[3].line + ident_or_tags: val[2], + token_code: val[1], + lineno: val[1].line ) } - | "%printer" "{" + | "%printer" param generic_symbol+ { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" generic_symlist - { @grammar.add_printer( - ident_or_tags: val[6], - token_code: val[3], - lineno: val[3].line + ident_or_tags: val[2], + token_code: val[1], + lineno: val[1].line ) } - | "%error-token" "{" + | "%error-token" param generic_symbol+ { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" generic_symlist - { @grammar.add_error_token( - ident_or_tags: val[6], - token_code: val[3], - lineno: val[3].line + ident_or_tags: val[2], + token_code: val[1], + lineno: val[1].line ) } | "%after-shift" IDENTIFIER { @grammar.after_shift = val[1] @@ -206,45 +154,47 @@ } } @precedence_number += 1 } - token_declarations: token_declaration_list + 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_list + | 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_list + | 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_list: token_declaration { result = [val[0]] } - | token_declaration_list token_declaration { result = val[0].append(val[1]) } + token_declaration: id INTEGER? alias { result = val } - token_declaration: id int_opt alias { result = val } - - rule_declaration: "%rule" IDENTIFIER "(" rule_args ")" tag_opt ":" rule_rhs_list + 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" id_colon ":" rule_rhs_list + 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 @@ -256,22 +206,17 @@ { builder = val[2] result = val[0].append(builder) } - rule_rhs: /* empty */ + rule_rhs: empty { reset_precs result = Grammar::ParameterizingRule::Rhs.new } - | "%empty" + | rule_rhs symbol named_ref? { - reset_precs - result = Grammar::ParameterizingRule::Rhs.new - } - | rule_rhs symbol named_ref_opt - { token = val[1] token.alias_name = val[2] builder = val[0] builder.symbols << token result = builder @@ -280,32 +225,20 @@ { 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_opt + | 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 "{" + | rule_rhs midrule_action named_ref? { - 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 - } - "}" named_ref_opt - { - user_code = val[3] - user_code.alias_name = val[6] + user_code = val[1] + user_code.alias_name = val[2] builder = val[0] builder.user_code = user_code result = builder } | rule_rhs "%prec" symbol @@ -315,88 +248,44 @@ builder = val[0] builder.precedence_sym = sym result = builder } - int_opt: # empty - | INTEGER - alias: # empty - | STRING # TODO: change this to string_as_id + | string_as_id { result = val[0].s_value } - symbol_declarations: symbol_declaration_list - { - result = [{tag: nil, tokens: val[0]}] - } - | TAG symbol_declaration_list - { - result = [{tag: val[0], tokens: val[1]}] - } - | symbol_declarations TAG symbol_declaration_list - { - result = val[0].append({tag: val[1], tokens: val[2]}) - } + 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_declaration_list: symbol { result = [val[0]] } - | symbol_declaration_list symbol { result = val[0].append(val[1]) } - symbol: id | string_as_id - params: params "{" - { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" - { - result = val[0].append(val[3]) - } - | "{" - { - begin_c_declaration("}") - } - C_DECLARATION - { - end_c_declaration - } - "}" - { - result = [val[2]] - } + param: "{" { + begin_c_declaration("}") + } + C_DECLARATION + { + end_c_declaration + } + "}" + { + result = val[2] + } - token_declarations_for_precedence: token_declaration_list_for_precedence - { - result = [{tag: nil, tokens: val[0]}] - } - | TAG token_declaration_list_for_precedence - { - result = [{tag: val[0], tokens: val[1]}] - } - | token_declarations_for_precedence TAG token_declaration_list_for_precedence - { - result = val[0].append({tag: val[1], tokens: 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]}) } - token_declaration_list_for_precedence: token_declaration_for_precedence { result = [val[0]] } - | token_declaration_list_for_precedence token_declaration_for_precedence { result = val[0].append(val[1]) } - - token_declaration_for_precedence: id - 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 } - grammar: rules_or_grammar_declaration - | grammar rules_or_grammar_declaration - rules_or_grammar_declaration: rules + rules_or_grammar_declaration: rules ";"? | grammar_declaration ";" - rules: id_colon named_ref_opt ":" rhs_list + rules: IDENT_COLON named_ref? ":" rhs_list { lhs = val[0] lhs.alias_name = val[1] val[3].each do |builder| builder.lhs = lhs @@ -419,63 +308,45 @@ if !builder.line builder.line = @lexer.line - 1 end result = val[0].append(builder) } - | rhs_list ";" - rhs: /* empty */ + rhs: empty { reset_precs - result = Grammar::RuleBuilder.new(@rule_counter, @midrule_action_counter) + result = @grammar.create_rule_builder(@rule_counter, @midrule_action_counter) } - | "%empty" + | rhs symbol named_ref? { - reset_precs - result = Grammar::RuleBuilder.new(@rule_counter, @midrule_action_counter) - } - | rhs symbol named_ref_opt - { token = val[1] token.alias_name = val[2] builder = val[0] builder.add_rhs(token) result = builder } - | rhs symbol parameterizing_suffix tag_opt + | rhs symbol parameterizing_suffix named_ref? TAG? { - token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]], lhs_tag: val[3]) + 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 ")" tag_opt + | rhs IDENTIFIER "(" parameterizing_args ")" named_ref? TAG? { - token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5]) + 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 "{" + | rhs midrule_action named_ref? TAG? { - 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 - } - "}" named_ref_opt tag_opt - { - user_code = val[3] - user_code.alias_name = val[6] - user_code.tag = val[7] + 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 @@ -494,43 +365,53 @@ 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])] } - named_ref_opt: # empty - | '[' IDENTIFIER ']' { result = val[1].s_value } + 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] + } - id_colon: IDENT_COLON + named_ref: '[' IDENTIFIER ']' { result = val[1].s_value } - epilogue_opt: # empty - | "%%" - { - begin_c_declaration('\Z') - @grammar.epilogue_first_lineno = @lexer.line + 1 - } - C_DECLARATION - { - end_c_declaration - @grammar.epilogue = val[2].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_symlist: generic_symlist_item { result = [val[0]] } - | generic_symlist generic_symlist_item { result = val[0].append(val[1]) } + generic_symbol: symbol + | TAG - generic_symlist_item: symbol - | TAG + empty: /* empty */ + | "%empty" string_as_id: STRING { result = Lrama::Lexer::Token::Ident.new(s_value: val[0]) } - - tag_opt: # empty - | TAG end ---- inner include Lrama::Report::Duration