Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IELR(1) Parser Generation #398

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def run(argv)
text = options.y.read
options.y.close if options.y != STDIN
begin
grammar = Lrama::Parser.new(text, options.grammar_file, options.debug).parse
grammar = Lrama::Parser.new(text, options.grammar_file, options.debug, options.define).parse
unless grammar.no_stdlib
stdlib_grammar = Lrama::Parser.new(File.read(STDLIB_FILE_PATH), STDLIB_FILE_PATH, options.debug).parse
grammar.insert_before_parameterizing_rules(stdlib_grammar.parameterizing_rules)
Expand All @@ -34,6 +34,7 @@ def run(argv)
end
states = Lrama::States.new(grammar, trace_state: (options.trace_opts[:automaton] || options.trace_opts[:closure]))
states.compute
states.compute_ielr if grammar.ielr_defined?
context = Lrama::Context.new(states)

if options.report_file
Expand Down
9 changes: 7 additions & 2 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class Grammar
attr_reader :percent_codes, :eof_symbol, :error_symbol, :undef_symbol, :accept_symbol, :aux, :parameterizing_rule_resolver
attr_accessor :union, :expect, :printers, :error_tokens, :lex_param, :parse_param, :initial_action,
:after_shift, :before_reduce, :after_reduce, :after_shift_error_token, :after_pop_stack,
:symbols_resolver, :types, :rules, :rule_builders, :sym_to_rules, :no_stdlib, :locations
:symbols_resolver, :types, :rules, :rule_builders, :sym_to_rules, :no_stdlib, :locations, :define

def_delegators "@symbols_resolver", :symbols, :nterms, :terms, :add_nterm, :add_term,
:find_symbol_by_number!, :find_symbol_by_id!, :token_to_symbol,
:find_symbol_by_s_value!, :fill_symbol_number, :fill_nterm_type,
:fill_printer, :fill_destructor, :fill_error_token, :sort_by_number!

def initialize(rule_counter)
def initialize(rule_counter, define = {})
@rule_counter = rule_counter

# Code defined by "%code"
Expand All @@ -57,6 +57,7 @@ def initialize(rule_counter)
@aux = Auxiliary.new
@no_stdlib = false
@locations = false
@define = define.map {|d| d.split('=') }.to_h

append_special_symbols
end
Expand Down Expand Up @@ -171,6 +172,10 @@ def find_rules_by_symbol(sym)
@sym_to_rules[sym.number]
end

def ielr_defined?
@define.key?('lr.type') && @define['lr.type'] == 'ielr'
end

private

def compute_nullable
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def parse_by_option_parser(argv)
o.on('-S', '--skeleton=FILE', 'specify the skeleton to use') {|v| @options.skeleton = v }
o.on('-t', 'reserved, do nothing') { }
o.on('--debug', 'display debugging outputs of internal parser') {|v| @options.debug = true }
o.on('-D', '--define=NAME[=VALUE]', Array, "similar to '%define NAME VALUE'") {|v| @options.define = v }
o.separator ''
o.separator 'Output:'
o.on('-H', '--header=[FILE]', 'also produce a header file named FILE') {|v| @options.header = true; @options.header_file = v }
Expand Down
3 changes: 2 additions & 1 deletion lib/lrama/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class Options
:report_file, :outfile,
:error_recovery, :grammar_file,
:trace_opts, :report_opts,
:diagnostic, :y, :debug
:diagnostic, :y, :debug, :define

def initialize
@skeleton = "bison/yacc.c"
@define = {}
@header = false
@header_file = nil
@report_file = nil
Expand Down
14 changes: 10 additions & 4 deletions lib/lrama/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading