Class: Aurum::Builder::AugmentedGrammar
- Inherits:
-
Object
- Object
- Aurum::Builder::AugmentedGrammar
- Defined in:
- lib/aurum/grammar/builder/augmented_grammar.rb
Instance Attribute Summary collapse
-
#all_productions ⇒ Object
readonly
Returns the value of attribute all_productions.
-
#start_production ⇒ Object
readonly
Returns the value of attribute start_production.
Instance Method Summary collapse
- #compute_first_sets ⇒ Object
- #compute_nullables ⇒ Object
- #first_set(symbol) ⇒ Object
-
#initialize(syntax_rules, start_symbol) ⇒ AugmentedGrammar
constructor
A new instance of AugmentedGrammar.
- #nullable?(symbol) ⇒ Boolean
- #productions(nonterminal = nil) ⇒ Object
Constructor Details
#initialize(syntax_rules, start_symbol) ⇒ AugmentedGrammar
Returns a new instance of AugmentedGrammar.
11 12 13 14 15 16 17 18 19 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 11 def initialize syntax_rules, start_symbol @syntax_rules, @start_symbol = syntax_rules, start_symbol @start_production = Aurum::Grammar.production(StartSymbol, [start_symbol]) @all_productions = [@start_production] @nonterminals = [StartSymbol, @start_symbol].to_set @nullables = {StartSymbol => false, @start_symbol => false} @first_sets = {StartSymbol => [].to_set, @start_symbol => [].to_set} initialize_for_used_symbols end |
Instance Attribute Details
#all_productions ⇒ Object (readonly)
Returns the value of attribute all_productions.
10 11 12 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 10 def all_productions @all_productions end |
#start_production ⇒ Object (readonly)
Returns the value of attribute start_production.
10 11 12 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 10 def start_production @start_production end |
Instance Method Details
#compute_first_sets ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 41 def compute_first_sets fixed_point do |nonterminal| for production in productions(nonterminal) for symbol in production.symbols is_updated = @first_sets[symbol].inject(false) {|r, s| r |= @first_sets[nonterminal].add?(s) } break unless nullable?(symbol) and !is_updated end end is_updated end end |
#compute_nullables ⇒ Object
34 35 36 37 38 39 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 34 def compute_nullables fixed_point do |nonterminal| break true if nullable? nonterminal @nullables[nonterminal] = productions(nonterminal).any? {|prod| production_nullable?(prod)} end end |
#first_set(symbol) ⇒ Object
25 26 27 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 25 def first_set symbol @first_sets[symbol] end |
#nullable?(symbol) ⇒ Boolean
21 22 23 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 21 def nullable? symbol @nullables[symbol] end |
#productions(nonterminal = nil) ⇒ Object
29 30 31 32 |
# File 'lib/aurum/grammar/builder/augmented_grammar.rb', line 29 def productions(nonterminal = nil) return [@start_production] if nonterminal == StartSymbol @syntax_rules.productions(nonterminal) end |