diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index 699c99dd..b58b4901 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -156,6 +156,8 @@ def prepare fill_symbol_printer fill_symbol_error_token @symbols.sort_by!(&:number) + compute_nullable + compute_first_set end # TODO: More validation methods @@ -167,6 +169,61 @@ def validate! validate_rule_lhs_is_nterm! end + def find_symbol_by_s_value(s_value) + @symbols.find do |sym| + sym.id.s_value == s_value + end + end + + def find_symbol_by_s_value!(s_value) + find_symbol_by_s_value(s_value) || (raise "Symbol not found: #{s_value}") + end + + def find_symbol_by_id(id) + @symbols.find do |sym| + sym.id == id || sym.alias_name == id.s_value + end + end + + def find_symbol_by_id!(id) + find_symbol_by_id(id) || (raise "Symbol not found: #{id}") + end + + def find_symbol_by_number!(number) + sym = @symbols[number] + + raise "Symbol not found: #{number}" unless sym + raise "[BUG] Symbol number mismatch. #{number}, #{sym}" if sym.number != number + + sym + end + + def find_rules_by_symbol!(sym) + find_rules_by_symbol(sym) || (raise "Rules for #{sym} not found") + end + + def find_rules_by_symbol(sym) + @sym_to_rules[sym.number] + end + + def terms_count + terms.count + end + + def terms + @terms ||= @symbols.select(&:term?) + end + + def nterms_count + nterms.count + end + + def nterms + @nterms ||= @symbols.select(&:nterm?) + end + + private + def compute_nullable @rules.each do |rule| case @@ -251,61 +308,6 @@ def compute_first_set end end - def find_symbol_by_s_value(s_value) - @symbols.find do |sym| - sym.id.s_value == s_value - end - end - - def find_symbol_by_s_value!(s_value) - find_symbol_by_s_value(s_value) || (raise "Symbol not found: #{s_value}") - end - - def find_symbol_by_id(id) - @symbols.find do |sym| - sym.id == id || sym.alias_name == id.s_value - end - end - - def find_symbol_by_id!(id) - find_symbol_by_id(id) || (raise "Symbol not found: #{id}") - end - - def find_symbol_by_number!(number) - sym = @symbols[number] - - raise "Symbol not found: #{number}" unless sym - raise "[BUG] Symbol number mismatch. #{number}, #{sym}" if sym.number != number - - sym - end - - def find_rules_by_symbol!(sym) - find_rules_by_symbol(sym) || (raise "Rules for #{sym} not found") - end - - def find_rules_by_symbol(sym) - @sym_to_rules[sym.number] - end - - def terms_count - terms.count - end - - def terms - @terms ||= @symbols.select(&:term?) - end - - def nterms_count - nterms.count - end - - def nterms - @nterms ||= @symbols.select(&:nterm?) - end - - private - def setup_rules @rule_builders.each do |builder| builder.setup_rules diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 101830fd..1284d479 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -678,8 +678,6 @@ def parse reset_precs do_parse @grammar.prepare - @grammar.compute_nullable - @grammar.compute_first_set @grammar.validate! @grammar end diff --git a/parser.y b/parser.y index c7d4e68e..82057156 100644 --- a/parser.y +++ b/parser.y @@ -434,8 +434,6 @@ def parse reset_precs do_parse @grammar.prepare - @grammar.compute_nullable - @grammar.compute_first_set @grammar.validate! @grammar end