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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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 @@ -18,7 +18,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 @@ -33,6 +33,7 @@ def run(argv)
end
states = Lrama::States.new(grammar, warning, 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
10 changes: 8 additions & 2 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ class Grammar
:after_shift, :before_reduce, :after_reduce, :after_shift_error_token, :after_pop_stack,
:symbols_resolver, :types,
:rules, :rule_builders,
:sym_to_rules, :no_stdlib
:sym_to_rules, :no_stdlib,
: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 @@ -59,6 +60,7 @@ def initialize(rule_counter)
@accept_symbol = nil
@aux = Auxiliary.new
@no_stdlib = false
@define = define

append_special_symbols
end
Expand Down Expand Up @@ -167,6 +169,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 @@ -59,6 +59,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 @@ -5,10 +5,11 @@ class Options
:report_file, :outfile,
:error_recovery, :grammar_file,
:trace_opts, :report_opts, :y,
:debug
: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.

182 changes: 180 additions & 2 deletions lib/lrama/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
require "lrama/state/resolved_conflict"
require "lrama/state/shift"
require "lrama/state/shift_reduce_conflict"
require "lrama/state/inadequacy_annotation"

module Lrama
class State
attr_reader :id, :accessing_symbol, :kernels, :conflicts, :resolved_conflicts,
:default_reduction_rule, :closure, :items
attr_accessor :shifts, :reduces
:default_reduction_rule, :closure, :items, :predecessors
attr_accessor :shifts, :reduces, :lalr_isocore

def initialize(id, accessing_symbol, kernels)
@id = id
Expand All @@ -21,6 +22,8 @@ def initialize(id, accessing_symbol, kernels)
@conflicts = []
@resolved_conflicts = []
@default_reduction_rule = nil
@predecessors = []
@lalr_isocore = self
end

def closure=(closure)
Expand Down Expand Up @@ -82,6 +85,11 @@ def transitions
@transitions ||= shifts.map {|shift| [shift, @items_to_state[shift.next_items]] }
end

def update_transition(shift, next_state)
set_items_to_state(shift.next_items, next_state)
@transitions = shifts.map {|sh| [sh, @items_to_state[sh.next_items]] }
end

def selected_term_transitions
term_transitions.reject do |shift, next_state|
shift.not_selected
Expand Down Expand Up @@ -140,5 +148,175 @@ def rr_conflicts
conflict.type == :reduce_reduce
end
end

def always_follows(shift, next_state)
internal_dependencies(shift, next_state).union(successor_dependencies(shift, next_state)).reduce([]) {|result, transition| result += transition[1].term_transitions.map {|shift, _| shift.next_sym } }
end

def internal_dependencies(shift, next_state)
nterm_transitions.select {|other_shift, _|
@items.find {|item| item.next_sym == shift.next_sym && item.lhs == other_shift.next_sym && item.symbols_after_dot.all?(&:nullable) }
}.reduce([[shift, next_state]]) {|result, transition|
result += internal_dependencies(*transition)
}
end

def successor_dependencies(shift, next_state)
next_state.nterm_transitions.select {|other_shift, _|
other_shift.next_sym.nullable
}.reduce([[shift, next_state]]) {|result, transition|
result += successor_dependencies(*transition)
}
end

def inspect
"#{id} -> #{@kernels.map(&:to_s).join(', ')}"
end

def inadequacy_list
return @inadequacy_list if @inadequacy_list

shift_contributions = shifts.to_h {|shift|
[shift.next_sym, [shift]]
}
reduce_contributions = reduces.map {|reduce|
(reduce.look_ahead || []).to_h {|sym|
[sym, [reduce]]
}
}.reduce(Hash.new([])) {|hash, cont|
hash.merge(cont) {|_, a, b| a.union(b) }
}

list = shift_contributions.merge(reduce_contributions) {|_, a, b| a.union(b) }
@inadequacy_list = list.select {|token, actions| token.term? && actions.size > 1 }
end

def annotate_manifestation
inadequacy_list.map {|token, actions|
actions.map {|action|
if action.is_a?(Shift)
[InadequacyAnnotation.new(token: token, action: action, item: nil, contributed: false)]
elsif action.is_a?(Reduce)
if action.rule.empty_rule?
lhs_contributions(action.rule.lhs, token).map {|kernel, contributed|
InadequacyAnnotation.new(token: token, action: action, item: kernel, contributed: contributed)
}
else
kernels.map {|kernel|
contributed = kernel.rule == action.rule && kernel.end_of_rule?
InadequacyAnnotation.new(token: token, action: action, item: kernel, contributed: contributed)
}
end
end
}
}
end

def annotate_predecessor(annotation_list)
annotation_list.reduce([]) {|annotation|
next [token, {}] if annotation.no_contributions? || actions.any? {|action, hash|
p action, hash
hash.keys.any? {|item| hash[item] && item.position == 1 && compute_lhs_contributions(state, item.lhs, token).empty? }
}
[
token, actions.to_h {|action, hash|
[
action, hash.to_h {|item, _|
kernel = state.kernels.find {|k| k.rule == item.rule && k.position == item.position - 1 }
[kernel,
hash[item] &&
(
!kernel.nil? && (state.item_lookahead_set[kernel].include?(token)) ||
(item.position == 1 && compute_lhs_contributions(state, item.lhs, token)[item])
)
]
}
]
}
]
}
end

def item_lookahead_set
@item_lookahead_set ||=
kernels.to_h {|item|
value =
if item.position > 1
prev_state, prev_item = predecessor_with_item(item)
prev_state.item_lookahead_set[prev_item]
elsif item.position == 1
prev_state = predecessors.find {|p| p.shifts.any? {|shift| shift.next_sym == item.lhs } }
shift, next_state = prev_state.nterm_transitions.find {|shift, _| shift.next_sym == item.lhs }
prev_state.goto_follows(shift, next_state)
else
[]
end
[item, value]
}
end

def item_lookahead_set=(k)
@item_lookahead_set = k
end

def predecessor_with_item(item)
predecessors.each do |state|
state.kernels.each do |kernel|
return [state, kernel] if kernel.rule == item.rule && kernel.position == item.position - 1
end
end
end

def lhs_contributions(sym, token)
shift, next_state = nterm_transitions.find {|sh, _| sh.next_sym == sym }
if always_follows(shift, next_state).include?(token)
[]
else
kernels.map {|kernel| [kernel, follow_kernel?(kernel) && item_lookahead_set[kernel].include?(token)] }
end
end

def follow_kernel?(item)
item.symbols_after_dot.all?(&:nullable)
end

def follow_kernel_items(shift, next_state, item)
internal_dependencies(shift, next_state).any? {|shift, _| shift.next_sym == item.next_sym } && item.symbols_after_dot.all?(&:nullable)
end

def next_terms
shifts.filter_map {|shift| shift.next_sym.term? && shift.next_sym }
end

def append_predecessor(prev_state)
@predecessors << prev_state
@predecessors.uniq!
end

def goto_follows(shift, next_state)
include_dependencies(shift, next_state).reduce([]) {|result, goto|
st, sh, next_st = goto
result.union(st.always_follows(sh, next_st))
}
end

def include_dependencies(shift, next_state)
internal = internal_dependencies(shift, next_state).map {|sh, next_st| [self, sh, next_st] }
pred = predecessor_dependencies(shift, next_state)

return internal if pred.empty?
dependency = internal.union(pred)

dependency.reduce(dependency) {|result, goto| result.union(compute_include_dependencies(*goto)) }
end

def predecessor_dependencies(shift, next_state)
item = kernels.find {|kernel| kernel.next_sym == shift.next_sym }
return [] unless item.symbols_after_transition.all?(&:nullable)

st = @predecessors.find {|p| p.items.find {|i| i.rule == item.rule && i.position == item.position - 1 } }
sh, next_st = s.nterm_transitions.find {|shift, _| shift.next_token == item.lhs }
[[s, sh, next_st]]
end
end
end
9 changes: 9 additions & 0 deletions lib/lrama/state/inadequacy_annotation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Lrama
class State
class InadequacyAnnotation < Struct.new(:token, :action, :item, :contributed, keyword_init: true)
def no_contributions?
item.nil? && !contributed
end
end
end
end
Loading