Class: Antelope::Generation::Constructor
- Inherits:
-
Object
- Object
- Antelope::Generation::Constructor
- Defined in:
- lib/antelope/generation/constructor.rb,
lib/antelope/generation/constructor/first.rb,
lib/antelope/generation/constructor/follow.rb,
lib/antelope/generation/constructor/nullable.rb
Overview
Constructs the lookahead sets for all of the rules in the grammar.
Defined Under Namespace
Modules: First, Follow, Nullable
Instance Attribute Summary collapse
-
#grammar ⇒ Ace::Grammar
readonly
The grammar.
-
#productions ⇒ Object
readonly
Returns the value of attribute productions.
Instance Method Summary collapse
-
#augment_rules(state) ⇒ void
Augments every final rule.
-
#augment_state(state) ⇒ void
Augments the given state.
-
#call ⇒ void
Performs the construction.
- #incorrect_argument!(arg, *types) ⇒ Object private
-
#initialize(grammar) ⇒ Constructor
constructor
Initialize.
Methods included from Follow
Methods included from First
#first, #first_array, #firstifying
Methods included from Nullable
Constructor Details
#initialize(grammar) ⇒ Constructor
Initialize.
27 28 29 30 31 |
# File 'lib/antelope/generation/constructor.rb', line 27 def initialize(grammar) @productions = Set.new @grammar = grammar super() end |
Instance Attribute Details
#grammar ⇒ Ace::Grammar (readonly)
The grammar.
20 21 22 |
# File 'lib/antelope/generation/constructor.rb', line 20 def grammar @grammar end |
#productions ⇒ Object (readonly)
Returns the value of attribute productions.
22 23 24 |
# File 'lib/antelope/generation/constructor.rb', line 22 def productions @productions end |
Instance Method Details
#augment_rules(state) ⇒ void
This method returns an undefined value.
Augments every final rule. For every rule in the current state that has a position of zero, it follows the rule through the DFA until the ending state; it then modifies the ending state's lookahead set to be the FOLLOW set of the nonterminal it reduces to.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/antelope/generation/constructor.rb', line 85 def augment_rules(state) state.rules.select { |x| x.position.zero? }.each do |rule| current_state = state rule.right.each do |part| transition = current_state.transitions[part.name] current_state = transition end final = current_state.rule_for(rule) final.lookahead = follow(rule.left) end end |
#augment_state(state) ⇒ void
This method returns an undefined value.
Augments the given state. On every rule within that state that has a position of zero, it follows the rule throughout the DFA until the end; it marks every nonterminal it encounters with the transitions it took on that nonterminal.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/antelope/generation/constructor.rb', line 55 def augment_state(state) state.rules.select { |x| x.position.zero? }.each do |rule| current_state = state rule.left.from = state rule.left.to = state.transitions[rule.left.name] rule.right.each_with_index do |part, pos| transition = current_state.transitions[part.name] if part.nonterminal? part.from = current_state part.to = transition end current_state = transition end productions << rule end end |
#call ⇒ void
This method returns an undefined value.
Performs the construction. First, it goes through every state and augments the state. It then goes through every rule and augments it.
40 41 42 43 44 45 46 |
# File 'lib/antelope/generation/constructor.rb', line 40 def call grammar.states.each do |state| augment_state(state) end.each do |state| augment_rules(state) end end |
#incorrect_argument!(arg, *types) ⇒ Object (private)
102 103 104 |
# File 'lib/antelope/generation/constructor.rb', line 102 def incorrect_argument!(arg, *types) raise ArgumentError, "Expected one of #{types.join(", ")}, got #{arg.class}" end |