From acf55896bf17756ef0b8cb596ce58fad03d71f08 Mon Sep 17 00:00:00 2001 From: Junichi Kobayashi Date: Tue, 19 Sep 2023 23:43:29 +0900 Subject: [PATCH] Update parser --- lib/lrama/grammar.rb | 13 +- lib/lrama/new_lexer.rb | 2 +- lib/lrama/new_parser.rb | 661 +++++++++++++++++++++------------------- parser.y | 27 +- 4 files changed, 378 insertions(+), 325 deletions(-) diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index 8a054d3e..93146fb2 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -318,13 +318,13 @@ def extract_references start = scanner.pos case when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?\$/) # $$, $$ - tag = scanner[1] ? create_token(Token::Tag, scanner[1], line, str.length) : nil + tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil references << [:dollar, "$", tag, start, scanner.pos - 1] when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?(\d+)/) # $1, $2, $1 - tag = scanner[1] ? create_token(Token::Tag, scanner[1], line, str.length) : nil + tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil references << [:dollar, Integer(scanner[2]), tag, start, scanner.pos - 1] when scanner.scan(/\$(<[a-zA-Z0-9_]+>)?([a-zA-Z_.][-a-zA-Z0-9_.]*)/) # $foo, $expr, $program - tag = scanner[1] ? create_token(Token::Tag, scanner[1], line, str.length) : nil + tag = scanner[1] ? Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: scanner[1]) : nil references << [:dollar, scanner[2], tag, start, scanner.pos - 1] when scanner.scan(/@\$/) # @$ references << [:at, "$", nil, start, scanner.pos - 1] @@ -341,6 +341,13 @@ def extract_references end end + def create_token(type, s_value, line, column) + t = Token.new(type: type, s_value: s_value) + t.line = line + t.column = column + + return t + end private def find_nterm_by_id!(id) diff --git a/lib/lrama/new_lexer.rb b/lib/lrama/new_lexer.rb index c6d78e2b..6b5bb556 100644 --- a/lib/lrama/new_lexer.rb +++ b/lib/lrama/new_lexer.rb @@ -69,7 +69,7 @@ def lex_token return [:STRING, @scanner.scan_until(/"/)[0..-2]] when @scanner.scan(/\d+/) return [:INTEGER, @scanner.matched] - when @scanner.scan(/([a-zA-Z_.][-a-zA-Z0-9_.()]*)/) + when @scanner.scan(/([a-zA-Z_.][-a-zA-Z0-9_.]*)/) return [:IDENTIFIER, @scanner.matched] else raise diff --git a/lib/lrama/new_parser.rb b/lib/lrama/new_parser.rb index ec085178..3ee63bfe 100644 --- a/lib/lrama/new_parser.rb +++ b/lib/lrama/new_parser.rb @@ -8,7 +8,7 @@ module Lrama class NewParser < Racc::Parser -module_eval(<<'...end parser.y/module_eval...', 'parser.y', 115) +module_eval(<<'...end parser.y/module_eval...', 'parser.y', 118) def initialize(text) @text = text @@ -34,210 +34,218 @@ def next_token ##### State transition tables begin ### racc_action_table = [ - 69, 107, 131, 3, 31, 32, 31, 32, 104, 4, - 132, 133, 31, 32, 74, 106, 19, 22, 23, 24, - 25, 26, 27, 28, 8, 103, 31, 32, 11, 7, - 12, 13, 73, 14, 15, 16, 17, 75, 116, 18, - 19, 22, 23, 24, 25, 26, 27, 28, 116, 31, - 32, 31, 32, 29, 51, 117, 31, 32, 31, 32, - 38, 31, 32, 31, 32, 117, 19, 22, 23, 24, - 25, 26, 27, 28, 31, 32, 31, 32, 131, 41, - 31, 32, 56, 43, 31, 32, 132, 133, 61, 43, - 31, 32, 61, 43, 31, 32, 61, 46, 31, 32, - 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, - 31, 32, 31, 32, 31, 32, 31, 32, 31, 32, - 31, 32, 31, 32, 31, 32, 47, 48, 70, 71, - 76, 76, 76, 81, 85, 92, 97, 98, 99, 100, - 105, 109, 119, 120, 121, 122, 126, 134, 137 ] + 71, 110, 3, 76, 134, 106, 33, 34, 6, 33, + 34, 7, 135, 136, 8, 109, 20, 23, 24, 25, + 26, 27, 28, 29, 75, 105, 33, 34, 9, 77, + 33, 34, 14, 31, 12, 40, 13, 15, 16, 17, + 18, 119, 43, 19, 20, 23, 24, 25, 26, 27, + 28, 29, 119, 45, 33, 34, 33, 34, 120, 134, + 45, 33, 34, 33, 34, 33, 34, 135, 136, 120, + 20, 23, 24, 25, 26, 27, 28, 29, 33, 34, + 33, 34, 53, 45, 33, 34, 58, 48, 33, 34, + 63, 49, 33, 34, 63, 50, 33, 34, 63, 68, + 33, 34, 33, 34, 33, 34, 33, 34, 33, 34, + 33, 34, 33, 34, 33, 34, 33, 34, 33, 34, + 33, 34, 33, 34, 33, 34, 33, 34, 33, 34, + 72, 73, 78, 78, 78, 83, 87, 99, 100, 101, + 102, 107, 108, 112, 122, 123, 124, 125, 129, 137, + 140 ] racc_action_check = [ - 30, 94, 122, 0, 122, 122, 12, 12, 84, 1, - 122, 122, 50, 50, 39, 94, 30, 30, 30, 30, - 30, 30, 30, 30, 5, 84, 30, 30, 5, 4, - 5, 5, 39, 5, 5, 5, 5, 39, 108, 5, - 5, 5, 5, 5, 5, 5, 5, 5, 123, 51, - 51, 108, 108, 6, 24, 108, 24, 24, 54, 54, - 11, 123, 123, 55, 55, 123, 8, 8, 8, 8, - 8, 8, 8, 8, 56, 56, 8, 8, 127, 13, - 127, 127, 25, 14, 25, 25, 127, 127, 26, 15, - 26, 26, 27, 16, 27, 27, 28, 17, 28, 28, - 59, 59, 60, 60, 61, 61, 64, 64, 65, 65, - 81, 81, 83, 83, 86, 86, 88, 88, 89, 89, - 91, 91, 101, 101, 117, 117, 19, 21, 35, 36, - 42, 44, 45, 49, 53, 66, 77, 78, 79, 80, - 93, 96, 110, 111, 112, 113, 118, 124, 136 ] + 32, 96, 1, 41, 125, 86, 125, 125, 2, 13, + 13, 2, 125, 125, 3, 96, 32, 32, 32, 32, + 32, 32, 32, 32, 41, 86, 32, 32, 4, 41, + 52, 52, 4, 7, 4, 12, 4, 4, 4, 4, + 4, 111, 14, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 126, 15, 111, 111, 53, 53, 111, 130, + 16, 130, 130, 56, 56, 126, 126, 130, 130, 126, + 9, 9, 9, 9, 9, 9, 9, 9, 57, 57, + 9, 9, 25, 17, 25, 25, 26, 18, 26, 26, + 27, 20, 27, 27, 28, 22, 28, 28, 29, 30, + 29, 29, 58, 58, 61, 61, 62, 62, 63, 63, + 66, 66, 67, 67, 83, 83, 85, 85, 88, 88, + 90, 90, 91, 91, 93, 93, 103, 103, 120, 120, + 37, 38, 44, 46, 47, 51, 55, 79, 80, 81, + 82, 94, 95, 98, 113, 114, 115, 116, 121, 127, + 139 ] racc_action_pointer = [ - 0, 9, nil, nil, 29, 22, 49, nil, 48, nil, - nil, 53, -22, 69, 68, 74, 78, 82, nil, 111, - nil, 112, nil, nil, 28, 56, 62, 66, 70, nil, - -2, nil, nil, nil, nil, 111, 99, nil, nil, 4, - nil, nil, 115, nil, 116, 117, nil, nil, nil, 107, - -16, 21, nil, 127, 30, 35, 46, nil, nil, 72, - 74, 76, nil, nil, 78, 80, 130, nil, nil, nil, - nil, nil, nil, nil, nil, nil, nil, 132, 133, 134, - 135, 82, nil, 84, -2, nil, 86, nil, 88, 90, - nil, 92, nil, 136, -16, nil, 137, nil, nil, nil, - nil, 94, nil, nil, nil, nil, nil, nil, 23, nil, - 126, 127, 128, 129, nil, nil, nil, 96, 130, nil, - nil, nil, -24, 33, 143, nil, nil, 52, nil, nil, - nil, nil, nil, nil, nil, nil, 132, nil ] + nil, 2, 5, 14, 26, nil, nil, 26, nil, 52, + nil, nil, 26, -19, 35, 38, 45, 68, 72, nil, + 76, nil, 80, nil, nil, 56, 60, 64, 68, 72, + 95, nil, -2, nil, nil, nil, nil, 113, 101, nil, + nil, -4, nil, nil, 117, nil, 118, 119, nil, nil, + nil, 109, 2, 28, nil, 127, 35, 50, 74, nil, + nil, 76, 78, 80, nil, nil, 82, 84, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, nil, 133, + 134, 135, 136, 86, nil, 88, -2, nil, 90, nil, + 92, 94, nil, 96, 136, 138, -16, nil, 139, nil, + nil, nil, nil, 98, nil, nil, nil, nil, nil, nil, + nil, 26, nil, 128, 129, 130, 131, nil, nil, nil, + 100, 132, nil, nil, nil, -22, 37, 145, nil, nil, + 33, nil, nil, nil, nil, nil, nil, nil, nil, 134, + nil ] racc_action_default = [ - -2, -96, -6, -3, -96, -96, -96, 138, -96, -7, - -8, -96, -96, -96, -96, -96, -96, -96, -18, -96, - -22, -96, -26, -27, -96, -96, -96, -96, -96, -4, - -81, -62, -63, -64, -66, -96, -96, -80, -9, -85, - -84, -11, -12, -53, -13, -14, -15, -19, -23, -28, - -33, -96, -36, -39, -29, -44, -96, -47, -49, -30, - -56, -96, -59, -61, -31, -32, -96, -1, -65, -82, - -67, -69, -10, -86, -87, -88, -50, -96, -96, -96, - -96, -96, -37, -34, -41, -40, -46, -48, -45, -58, - -60, -57, -5, -96, -68, -74, -96, -54, -16, -20, - -24, -35, -38, -42, -43, -83, -71, -73, -70, -51, - -96, -96, -96, -96, -74, -75, -76, -96, -96, -55, - -17, -21, -96, -72, -96, -79, -52, -25, -89, -91, - -92, -93, -94, -95, -77, -90, -96, -78 ] + -2, -98, -8, -98, -98, -3, -4, -98, 141, -98, + -9, -10, -98, -98, -98, -98, -98, -98, -98, -20, + -98, -24, -98, -28, -29, -98, -98, -98, -98, -98, + -98, -7, -83, -64, -65, -66, -68, -98, -98, -82, + -11, -87, -86, -13, -14, -55, -15, -16, -17, -21, + -25, -30, -35, -98, -38, -41, -31, -46, -98, -49, + -51, -32, -58, -98, -61, -63, -33, -34, -5, -1, + -67, -84, -69, -71, -12, -88, -89, -90, -52, -98, + -98, -98, -98, -98, -39, -36, -43, -42, -48, -50, + -47, -60, -62, -59, -98, -98, -70, -76, -98, -56, + -18, -22, -26, -37, -40, -44, -45, -6, -85, -73, + -75, -72, -53, -98, -98, -98, -98, -76, -77, -78, + -98, -98, -57, -19, -23, -98, -74, -98, -81, -54, + -27, -91, -93, -94, -95, -96, -97, -79, -92, -98, + -80 ] racc_goto_table = [ - 37, 50, 87, 82, 40, 33, 108, 128, 55, 90, - 1, 2, 135, 42, 44, 45, 53, 5, 63, 63, - 63, 30, 37, 67, 89, 123, 91, 68, 83, 89, - 89, 6, 66, 87, 9, 87, 82, 86, 90, 88, - 90, 10, 53, 53, 59, 64, 65, 39, 72, 78, - 111, 63, 63, 63, 82, 115, 63, 63, 101, 127, - 79, 112, 80, 113, 125, 49, 54, 84, 102, 129, - 115, 96, 118, 53, 129, 53, 77, 110, 94, 95, - 114, 63, 124, 63, 136, 93, nil, nil, nil, nil, - nil, nil, nil, 53 ] + 39, 52, 89, 92, 42, 35, 57, 111, 91, 84, + 93, 1, 131, 91, 91, 2, 55, 138, 65, 65, + 65, 4, 32, 39, 44, 46, 47, 126, 70, 85, + 69, 5, 92, 89, 92, 89, 88, 30, 90, 61, + 66, 67, 84, 55, 55, 94, 10, 11, 41, 74, + 80, 114, 65, 65, 65, 130, 118, 65, 65, 103, + 84, 81, 115, 82, 116, 128, 51, 56, 86, 104, + 132, 118, 98, 121, 55, 132, 55, 79, 113, 96, + 97, 117, 65, 127, 65, 139, 95, nil, nil, nil, + nil, nil, nil, nil, 55 ] racc_goto_check = [ - 27, 25, 31, 26, 27, 38, 42, 48, 30, 37, - 1, 2, 48, 12, 12, 12, 27, 3, 27, 27, - 27, 4, 27, 5, 36, 42, 36, 38, 25, 36, - 36, 6, 7, 31, 8, 31, 26, 30, 37, 30, - 37, 9, 27, 27, 24, 24, 24, 10, 11, 13, - 14, 27, 27, 27, 26, 31, 27, 27, 25, 17, - 18, 19, 20, 21, 31, 22, 23, 28, 29, 31, - 31, 32, 33, 27, 31, 27, 34, 35, 41, 43, - 44, 27, 45, 27, 46, 47, nil, nil, nil, nil, - nil, nil, nil, 27 ] + 28, 26, 32, 38, 28, 39, 31, 43, 37, 27, + 37, 1, 49, 37, 37, 2, 28, 49, 28, 28, + 28, 3, 4, 28, 13, 13, 13, 43, 39, 26, + 5, 6, 38, 32, 38, 32, 31, 7, 31, 25, + 25, 25, 27, 28, 28, 8, 9, 10, 11, 12, + 14, 15, 28, 28, 28, 18, 32, 28, 28, 26, + 27, 19, 20, 21, 22, 32, 23, 24, 29, 30, + 32, 32, 33, 34, 28, 32, 28, 35, 36, 42, + 44, 45, 28, 46, 28, 47, 48, nil, nil, nil, + nil, nil, nil, nil, 28 ] racc_goto_pointer = [ - nil, 10, 11, 15, 13, -7, 28, 3, 29, 36, - 35, 9, -1, 3, -48, nil, nil, -63, 13, -38, - 14, -37, 41, 41, 18, -23, -47, -8, 14, -16, - -17, -53, -5, -37, 33, -20, -35, -51, -3, nil, - nil, 7, -89, 8, -26, -34, -50, 16, -115, nil ] + nil, 11, 15, 19, 13, -2, 29, 31, -23, 42, + 43, 35, 8, 9, 2, -49, nil, nil, -70, 12, + -39, 13, -38, 41, 41, 12, -24, -43, -9, 13, + -17, -20, -55, -6, -39, 32, -21, -53, -59, -4, + nil, nil, 6, -90, 7, -28, -36, -52, 15, -113, + nil ] racc_goto_default = [ - nil, nil, nil, nil, nil, nil, nil, nil, nil, 35, - nil, nil, nil, nil, nil, 20, 21, nil, nil, nil, - nil, nil, nil, nil, nil, nil, 52, 58, nil, nil, - nil, 57, nil, nil, nil, nil, 60, 62, nil, 34, - 36, nil, nil, nil, nil, nil, nil, nil, nil, 130 ] + nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, + 37, nil, nil, nil, nil, nil, 21, 22, nil, nil, + nil, nil, nil, nil, nil, nil, nil, 54, 60, nil, + nil, nil, 59, nil, nil, nil, nil, 62, 64, nil, + 36, 38, nil, nil, nil, nil, nil, nil, nil, nil, + 133 ] racc_reduce_table = [ 0, 0, :racc_error, 5, 37, :_reduce_none, 0, 38, :_reduce_none, - 0, 42, :_reduce_3, + 2, 38, :_reduce_none, 0, 43, :_reduce_4, - 5, 38, :_reduce_5, - 0, 39, :_reduce_6, + 0, 44, :_reduce_5, + 5, 42, :_reduce_6, + 2, 42, :_reduce_none, + 0, 39, :_reduce_8, 2, 39, :_reduce_none, - 1, 44, :_reduce_none, - 2, 44, :_reduce_none, - 3, 44, :_reduce_none, - 2, 44, :_reduce_none, - 2, 44, :_reduce_none, - 2, 44, :_reduce_none, - 2, 44, :_reduce_none, - 0, 49, :_reduce_15, - 0, 50, :_reduce_16, - 6, 44, :_reduce_none, - 1, 44, :_reduce_none, - 0, 54, :_reduce_19, - 0, 55, :_reduce_20, - 6, 45, :_reduce_21, 1, 45, :_reduce_none, - 0, 56, :_reduce_23, - 0, 57, :_reduce_24, - 7, 45, :_reduce_none, - 1, 52, :_reduce_none, - 1, 52, :_reduce_none, - 2, 51, :_reduce_none, - 2, 51, :_reduce_29, - 2, 51, :_reduce_30, - 2, 51, :_reduce_none, - 2, 51, :_reduce_none, - 1, 58, :_reduce_33, - 2, 58, :_reduce_34, - 3, 58, :_reduce_35, - 1, 61, :_reduce_36, - 2, 61, :_reduce_37, - 3, 62, :_reduce_38, - 0, 64, :_reduce_none, - 1, 64, :_reduce_40, + 2, 45, :_reduce_none, + 3, 45, :_reduce_none, + 2, 45, :_reduce_none, + 2, 45, :_reduce_none, + 2, 45, :_reduce_none, + 2, 45, :_reduce_16, + 0, 50, :_reduce_17, + 0, 51, :_reduce_18, + 6, 45, :_reduce_none, + 1, 45, :_reduce_none, + 0, 55, :_reduce_21, + 0, 56, :_reduce_22, + 6, 46, :_reduce_23, + 1, 46, :_reduce_none, + 0, 57, :_reduce_25, + 0, 58, :_reduce_26, + 7, 46, :_reduce_none, + 1, 53, :_reduce_none, + 1, 53, :_reduce_none, + 2, 52, :_reduce_none, + 2, 52, :_reduce_31, + 2, 52, :_reduce_32, + 2, 52, :_reduce_33, + 2, 52, :_reduce_none, + 1, 59, :_reduce_35, + 2, 59, :_reduce_36, + 3, 59, :_reduce_37, + 1, 62, :_reduce_38, + 2, 62, :_reduce_39, + 3, 63, :_reduce_40, 0, 65, :_reduce_none, - 1, 65, :_reduce_none, - 1, 65, :_reduce_none, - 1, 59, :_reduce_44, - 2, 59, :_reduce_45, - 2, 59, :_reduce_none, - 1, 66, :_reduce_47, - 2, 66, :_reduce_none, - 1, 67, :_reduce_none, - 0, 68, :_reduce_50, - 0, 69, :_reduce_51, - 6, 48, :_reduce_none, + 1, 65, :_reduce_42, + 0, 66, :_reduce_none, + 1, 66, :_reduce_none, + 1, 66, :_reduce_none, + 1, 60, :_reduce_46, + 2, 60, :_reduce_47, + 2, 60, :_reduce_48, + 1, 67, :_reduce_49, + 2, 67, :_reduce_50, + 1, 68, :_reduce_none, + 0, 69, :_reduce_52, 0, 70, :_reduce_53, - 0, 71, :_reduce_54, - 5, 48, :_reduce_none, - 1, 60, :_reduce_56, - 2, 60, :_reduce_57, - 2, 60, :_reduce_58, - 1, 72, :_reduce_59, - 2, 72, :_reduce_60, - 1, 73, :_reduce_none, - 1, 63, :_reduce_62, - 1, 63, :_reduce_63, + 6, 49, :_reduce_54, + 0, 71, :_reduce_55, + 0, 72, :_reduce_56, + 5, 49, :_reduce_57, + 1, 61, :_reduce_58, + 2, 61, :_reduce_59, + 2, 61, :_reduce_60, + 1, 73, :_reduce_61, + 2, 73, :_reduce_62, + 1, 74, :_reduce_none, + 1, 64, :_reduce_64, + 1, 64, :_reduce_65, 1, 40, :_reduce_none, 2, 40, :_reduce_none, - 1, 74, :_reduce_none, - 2, 74, :_reduce_none, - 3, 75, :_reduce_68, - 0, 79, :_reduce_69, - 2, 77, :_reduce_70, + 1, 75, :_reduce_none, + 2, 75, :_reduce_none, + 3, 76, :_reduce_70, 0, 80, :_reduce_71, - 4, 77, :_reduce_72, - 2, 77, :_reduce_none, - 0, 78, :_reduce_74, - 2, 78, :_reduce_75, - 0, 81, :_reduce_76, - 0, 82, :_reduce_77, - 6, 78, :_reduce_78, - 3, 78, :_reduce_none, - 1, 76, :_reduce_none, + 2, 78, :_reduce_72, + 0, 81, :_reduce_73, + 4, 78, :_reduce_74, + 2, 78, :_reduce_none, + 0, 79, :_reduce_76, + 2, 79, :_reduce_77, + 0, 82, :_reduce_78, + 0, 83, :_reduce_79, + 6, 79, :_reduce_80, + 3, 79, :_reduce_none, + 1, 77, :_reduce_none, 0, 41, :_reduce_none, - 0, 83, :_reduce_82, - 3, 41, :_reduce_83, - 1, 46, :_reduce_none, - 0, 47, :_reduce_none, - 1, 47, :_reduce_none, + 0, 84, :_reduce_84, + 3, 41, :_reduce_85, 1, 47, :_reduce_none, - 1, 47, :_reduce_none, - 1, 53, :_reduce_none, - 2, 53, :_reduce_none, - 1, 84, :_reduce_none, - 1, 84, :_reduce_none, + 0, 48, :_reduce_none, + 1, 48, :_reduce_none, + 1, 48, :_reduce_none, + 1, 48, :_reduce_none, + 1, 54, :_reduce_none, + 2, 54, :_reduce_none, 1, 85, :_reduce_none, 1, 85, :_reduce_none, - 1, 85, :_reduce_none ] + 1, 86, :_reduce_none, + 1, 86, :_reduce_none, + 1, 86, :_reduce_none ] -racc_reduce_n = 96 +racc_reduce_n = 98 -racc_shift_n = 138 +racc_shift_n = 141 racc_token_table = { false => 0, @@ -246,11 +254,11 @@ def next_token "%{" => 3, :C_DECLARATION => 4, "%}" => 5, - "%expect" => 6, - :INTEGER => 7, - "%define" => 8, - "%require" => 9, - :STRING => 10, + "%require" => 6, + :STRING => 7, + "%expect" => 8, + :INTEGER => 9, + "%define" => 10, "%param" => 11, "%lex-param" => 12, "%parse-param" => 13, @@ -305,11 +313,11 @@ def next_token "\"%{\"", "C_DECLARATION", "\"%}\"", + "\"%require\"", + "STRING", "\"%expect\"", "INTEGER", "\"%define\"", - "\"%require\"", - "STRING", "\"%param\"", "\"%lex-param\"", "\"%parse-param\"", @@ -337,10 +345,11 @@ def next_token "\"<>\"", "$start", "input", - "prologue_declaration", + "prologue_declarations", "bison_declarations", "grammar", "epilogue_opt", + "prologue_declaration", "@1", "@2", "bison_declaration", @@ -397,37 +406,37 @@ def next_token # reduce 2 omitted -module_eval(<<'.,.,', 'parser.y', 5) - def _reduce_3(val, _values, result) - @lexer.status = :c_declaration; @lexer.end_symbol = '%}'; @grammar.prologue_first_lineno = @lexer.line - result - end -.,., +# reduce 3 omitted -module_eval(<<'.,.,', 'parser.y', 5) +module_eval(<<'.,.,', 'parser.y', 7) def _reduce_4(val, _values, result) - @lexer.status = :initial; @lexer.end_symbol = nil + @lexer.status = :c_declaration; @lexer.end_symbol = '%}'; @grammar.prologue_first_lineno = @lexer.line result end .,., -module_eval(<<'.,.,', 'parser.y', 5) +module_eval(<<'.,.,', 'parser.y', 7) def _reduce_5(val, _values, result) - @grammar.prologue = val[2] + @lexer.status = :initial; @lexer.end_symbol = nil result end .,., module_eval(<<'.,.,', 'parser.y', 7) def _reduce_6(val, _values, result) - result = "" + @grammar.prologue = val[2] result end .,., # reduce 7 omitted -# reduce 8 omitted +module_eval(<<'.,.,', 'parser.y', 10) + def _reduce_8(val, _values, result) + result = "" + result + end +.,., # reduce 9 omitted @@ -441,358 +450,388 @@ def _reduce_6(val, _values, result) # reduce 14 omitted -module_eval(<<'.,.,', 'parser.y', 17) - def _reduce_15(val, _values, result) +# reduce 15 omitted + +module_eval(<<'.,.,', 'parser.y', 19) + def _reduce_16(val, _values, result) + val[1].each {|token| token.references = []; @grammar.parse_param = @grammar.build_code(:parse_param, token).token_code.s_value} + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 20) + def _reduce_17(val, _values, result) @lexer.status = :c_declaration; @lexer.end_symbol = '}' result end .,., -module_eval(<<'.,.,', 'parser.y', 17) - def _reduce_16(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 20) + def _reduce_18(val, _values, result) @lexer.status = :initial; @lexer.end_symbol = nil result end .,., -# reduce 17 omitted +# reduce 19 omitted -# reduce 18 omitted +# reduce 20 omitted -module_eval(<<'.,.,', 'parser.y', 20) - def _reduce_19(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 23) + def _reduce_21(val, _values, result) @lexer.status = :c_declaration; @lexer.end_symbol = '}'; @lineno = @lexer.line result end .,., -module_eval(<<'.,.,', 'parser.y', 20) - def _reduce_20(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 23) + def _reduce_22(val, _values, result) @lexer.status = :initial; @lexer.end_symbol = nil result end .,., -module_eval(<<'.,.,', 'parser.y', 20) - def _reduce_21(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 23) + def _reduce_23(val, _values, result) token = Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::User_code, s_value: "{#{val[3]}}"); token.references = []; @grammar.set_union(@grammar.build_code(:union, token), @lineno) result end .,., -# reduce 22 omitted +# reduce 24 omitted -module_eval(<<'.,.,', 'parser.y', 22) - def _reduce_23(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 25) + def _reduce_25(val, _values, result) @lexer.status = :c_declaration; @lexer.end_symbol = '}' result end .,., -module_eval(<<'.,.,', 'parser.y', 22) - def _reduce_24(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 25) + def _reduce_26(val, _values, result) @lexer.status = :initial; @lexer.end_symbol = nil result end .,., -# reduce 25 omitted - -# reduce 26 omitted - # reduce 27 omitted # reduce 28 omitted -module_eval(<<'.,.,', 'parser.y', 28) - def _reduce_29(val, _values, result) - val[1][:tokens].each {|id| @grammar.add_type(id: id, tag: val[1][:tag]) } +# reduce 29 omitted + +# reduce 30 omitted + +module_eval(<<'.,.,', 'parser.y', 31) + def _reduce_31(val, _values, result) + val[1].each {|hash| hash[:tokens].each {|id| @grammar.add_type(id: id, tag: hash[:tag]) } } result end .,., -module_eval(<<'.,.,', 'parser.y', 29) - def _reduce_30(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 32) + def _reduce_32(val, _values, result) val[1].each {|hash| hash[:tokens].each {|id| sym = @grammar.add_term(id: id); @grammar.add_left(sym, @precedence_number) }; @precedence_number += 1 } result end .,., -# reduce 31 omitted - -# reduce 32 omitted - module_eval(<<'.,.,', 'parser.y', 33) def _reduce_33(val, _values, result) - 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) } + val[1].each {|hash| hash[:tokens].each {|id| sym = @grammar.add_term(id: id); @grammar.add_right(sym, @precedence_number) }; @precedence_number += 1 } result end .,., -module_eval(<<'.,.,', 'parser.y', 34) - def _reduce_34(val, _values, result) - val[1].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), replace: true) } - result - end -.,., +# reduce 34 omitted -module_eval(<<'.,.,', 'parser.y', 35) +module_eval(<<'.,.,', 'parser.y', 36) def _reduce_35(val, _values, result) - val[2].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[1]), replace: true) } + 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) } result end .,., module_eval(<<'.,.,', 'parser.y', 37) def _reduce_36(val, _values, result) - result = [val[0]] + val[1].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), replace: true) } result end .,., module_eval(<<'.,.,', 'parser.y', 38) def _reduce_37(val, _values, result) - result = val[0].append(val[1]) + val[2].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[1]), replace: true) } result end .,., module_eval(<<'.,.,', 'parser.y', 40) def _reduce_38(val, _values, result) - result = val + result = [val[0]] result end .,., -# reduce 39 omitted +module_eval(<<'.,.,', 'parser.y', 41) + def _reduce_39(val, _values, result) + result = val[0].append(val[1]) + result + end +.,., module_eval(<<'.,.,', 'parser.y', 43) def _reduce_40(val, _values, result) - result = Integer(val[0]) + result = val result end .,., # reduce 41 omitted -# reduce 42 omitted +module_eval(<<'.,.,', 'parser.y', 46) + def _reduce_42(val, _values, result) + result = Integer(val[0]) + result + end +.,., # reduce 43 omitted -module_eval(<<'.,.,', 'parser.y', 49) - def _reduce_44(val, _values, result) - result = {tag: nil, tokens: val[0]} +# reduce 44 omitted + +# reduce 45 omitted + +module_eval(<<'.,.,', 'parser.y', 52) + def _reduce_46(val, _values, result) + result = [{tag: nil, tokens: val[0]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 50) - def _reduce_45(val, _values, result) - result = {tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]} +module_eval(<<'.,.,', 'parser.y', 53) + def _reduce_47(val, _values, result) + result = [{tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]}] result end .,., -# reduce 46 omitted +module_eval(<<'.,.,', 'parser.y', 54) + def _reduce_48(val, _values, result) + result = val[0].append({tag: nil, tokens: val[1]}) + result + end +.,., -module_eval(<<'.,.,', 'parser.y', 53) - def _reduce_47(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 56) + def _reduce_49(val, _values, result) result = [val[0]] result end .,., -# reduce 48 omitted - -# reduce 49 omitted - -module_eval(<<'.,.,', 'parser.y', 58) +module_eval(<<'.,.,', 'parser.y', 57) def _reduce_50(val, _values, result) - @lexer.status = :c_declaration; @lexer.end_symbol = '}' + result = val[0].append(val[1]) result end .,., -module_eval(<<'.,.,', 'parser.y', 58) - def _reduce_51(val, _values, result) - @lexer.status = :initial; @lexer.end_symbol = nil +# reduce 51 omitted + +module_eval(<<'.,.,', 'parser.y', 61) + def _reduce_52(val, _values, result) + @lexer.status = :c_declaration; @lexer.end_symbol = '}' result end .,., -# reduce 52 omitted - -module_eval(<<'.,.,', 'parser.y', 59) +module_eval(<<'.,.,', 'parser.y', 61) def _reduce_53(val, _values, result) - @lexer.status = :c_declaration; @lexer.end_symbol = '}' + @lexer.status = :initial; @lexer.end_symbol = nil result end .,., -module_eval(<<'.,.,', 'parser.y', 59) +module_eval(<<'.,.,', 'parser.y', 61) def _reduce_54(val, _values, result) - @lexer.status = :initial; @lexer.end_symbol = nil + result = val[0].append(Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::User_code, s_value: val[3])) result end .,., -# reduce 55 omitted +module_eval(<<'.,.,', 'parser.y', 62) + def _reduce_55(val, _values, result) + @lexer.status = :c_declaration; @lexer.end_symbol = '}' + result + end +.,., -module_eval(<<'.,.,', 'parser.y', 61) +module_eval(<<'.,.,', 'parser.y', 62) def _reduce_56(val, _values, result) - result = [{tag: nil, tokens: val[0]}] + @lexer.status = :initial; @lexer.end_symbol = nil result end .,., module_eval(<<'.,.,', 'parser.y', 62) def _reduce_57(val, _values, result) - result = [{tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]}] + result = [Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::User_code, s_value: val[2])] result end .,., -module_eval(<<'.,.,', 'parser.y', 63) +module_eval(<<'.,.,', 'parser.y', 64) def _reduce_58(val, _values, result) - result = val[0].concat({tag: nil, tokens: val[1]}) + result = [{tag: nil, tokens: val[0]}] result end .,., module_eval(<<'.,.,', 'parser.y', 65) def _reduce_59(val, _values, result) - result = [val[0]] + result = [{tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]}] result end .,., module_eval(<<'.,.,', 'parser.y', 66) def _reduce_60(val, _values, result) - result = val[0].append(val[1]) + result = val[0].concat({tag: nil, tokens: val[1]}) result end .,., -# reduce 61 omitted +module_eval(<<'.,.,', 'parser.y', 68) + def _reduce_61(val, _values, result) + result = [val[0]] + result + end +.,., -module_eval(<<'.,.,', 'parser.y', 70) +module_eval(<<'.,.,', 'parser.y', 69) def _reduce_62(val, _values, result) + result = val[0].append(val[1]) + result + end +.,., + +# reduce 63 omitted + +module_eval(<<'.,.,', 'parser.y', 73) + def _reduce_64(val, _values, result) result = Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Ident, s_value: val[0]) result end .,., -module_eval(<<'.,.,', 'parser.y', 71) - def _reduce_63(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 74) + def _reduce_65(val, _values, result) result = Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Char, s_value: val[0]) result end .,., -# reduce 64 omitted - -# reduce 65 omitted - # reduce 66 omitted # reduce 67 omitted -module_eval(<<'.,.,', 'parser.y', 79) - def _reduce_68(val, _values, result) +# reduce 68 omitted + +# reduce 69 omitted + +module_eval(<<'.,.,', 'parser.y', 82) + def _reduce_70(val, _values, result) val[2].each {|hash| @grammar.add_rule(lhs: val[0], rhs: hash[:rhs], lineno: hash[:lineno]) } result end .,., -module_eval(<<'.,.,', 'parser.y', 81) - def _reduce_69(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 84) + def _reduce_71(val, _values, result) @lineno = @lexer.line result end .,., -module_eval(<<'.,.,', 'parser.y', 81) - def _reduce_70(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 84) + def _reduce_72(val, _values, result) result = [{rhs: val[1], lineno: @lineno}] result end .,., -module_eval(<<'.,.,', 'parser.y', 82) - def _reduce_71(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 85) + def _reduce_73(val, _values, result) @lineno = @lexer.line result end .,., -module_eval(<<'.,.,', 'parser.y', 82) - def _reduce_72(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 85) + def _reduce_74(val, _values, result) result = val[0].append({rhs: val[3], lineno: @lineno}) result end .,., -# reduce 73 omitted +# reduce 75 omitted -module_eval(<<'.,.,', 'parser.y', 85) - def _reduce_74(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 88) + def _reduce_76(val, _values, result) result = [] result end .,., -module_eval(<<'.,.,', 'parser.y', 86) - def _reduce_75(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 89) + def _reduce_77(val, _values, result) result = val[0].append(val[1]) result end .,., -module_eval(<<'.,.,', 'parser.y', 87) - def _reduce_76(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 90) + def _reduce_78(val, _values, result) @lexer.status = :c_declaration; @lexer.end_symbol = '}' result end .,., -module_eval(<<'.,.,', 'parser.y', 87) - def _reduce_77(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 90) + def _reduce_79(val, _values, result) @lexer.status = :initial; @lexer.end_symbol = nil result end .,., -module_eval(<<'.,.,', 'parser.y', 87) - def _reduce_78(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 90) + def _reduce_80(val, _values, result) result = val[0].append(Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::User_code, s_value: val[1..5].join)) result end .,., -# reduce 79 omitted +# reduce 81 omitted -# reduce 80 omitted +# reduce 82 omitted -# reduce 81 omitted +# reduce 83 omitted -module_eval(<<'.,.,', 'parser.y', 93) - def _reduce_82(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 96) + def _reduce_84(val, _values, result) @lexer.status = :c_declaration; @lexer.end_symbol = '\Z'; @grammar.epilogue_first_lineno = @lexer.line + 1 result end .,., -module_eval(<<'.,.,', 'parser.y', 93) - def _reduce_83(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 96) + def _reduce_85(val, _values, result) @lexer.status = :initial; @lexer.end_symbol = nil; @grammar.epilogue = val[2] result end .,., -# reduce 84 omitted - -# reduce 85 omitted - # reduce 86 omitted # reduce 87 omitted @@ -813,6 +852,10 @@ def _reduce_83(val, _values, result) # reduce 95 omitted +# reduce 96 omitted + +# reduce 97 omitted + def _reduce_none(val, _values, result) val[0] end diff --git a/parser.y b/parser.y index cbbee083..22729e8a 100644 --- a/parser.y +++ b/parser.y @@ -1,9 +1,12 @@ class Lrama::NewParser rule - input: prologue_declaration bison_declarations "%%" grammar epilogue_opt + input: prologue_declarations bison_declarations "%%" grammar epilogue_opt - prologue_declaration: /* empty */ - | "%{" {@lexer.status = :c_declaration; @lexer.end_symbol = '%}'; @grammar.prologue_first_lineno = @lexer.line} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "%}" { @grammar.prologue = val[2] } + prologue_declarations: # empty + | prologue_declarations prologue_declaration + + prologue_declaration: "%{" {@lexer.status = :c_declaration; @lexer.end_symbol = '%}'; @grammar.prologue_first_lineno = @lexer.line} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "%}" { @grammar.prologue = val[2] } + | "%require" STRING bison_declarations: /* empty */ { result = "" } | bison_declarations bison_declaration @@ -14,7 +17,7 @@ rule | "%require" STRING | "%param" params | "%lex-param" params - | "%parse-param" params + | "%parse-param" params { val[1].each {|token| token.references = []; @grammar.parse_param = @grammar.build_code(:parse_param, token).token_code.s_value} } | "%initial-action" "{" {@lexer.status = :c_declaration; @lexer.end_symbol = '}'} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "}" | ";" @@ -26,9 +29,9 @@ rule | "%printer" symbol_declaration: "%token" token_declarations - | "%type" symbol_declarations { val[1][:tokens].each {|id| @grammar.add_type(id: id, tag: val[1][:tag]) } } + | "%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 + | "%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 } } | "%nonassoc" token_declarations_for_precedence token_declarations: token_declaration_list { 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) } } @@ -47,17 +50,17 @@ rule | string_as_id | STRING - symbol_declarations: symbol_declaration_list { result = {tag: nil, tokens: val[0]} } - | TAG symbol_declaration_list { result = {tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]} } - | symbol_declarations symbol_declaration_list + symbol_declarations: symbol_declaration_list { result = [{tag: nil, tokens: val[0]}] } + | TAG symbol_declaration_list { result = [{tag: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]}] } + | symbol_declarations symbol_declaration_list { result = val[0].append({tag: nil, tokens: val[1]}) } symbol_declaration_list: symbol { result = [val[0]] } - | symbol_declaration_list symbol + | symbol_declaration_list symbol { result = val[0].append(val[1]) } symbol: id - params: params "{" {@lexer.status = :c_declaration; @lexer.end_symbol = '}'} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "}" - | "{" {@lexer.status = :c_declaration; @lexer.end_symbol = '}'} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "}" + params: params "{" {@lexer.status = :c_declaration; @lexer.end_symbol = '}'} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "}" { result = val[0].append(Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::User_code, s_value: val[3])) } + | "{" {@lexer.status = :c_declaration; @lexer.end_symbol = '}'} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "}" { result = [Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::User_code, s_value: 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: Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Tag, s_value: val[0]), tokens: val[1]}] }