Class: Antelope::Generation::Constructor

Inherits:
Object
  • Object
show all
Includes:
First, Follow, Nullable
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

Instance Method Summary collapse

Methods included from Follow

#follow, #generate_follow_set

Methods included from First

#first, #first_array, #firstifying

Methods included from Nullable

#nullable?, #nullifying

Constructor Details

#initialize(grammar) ⇒ Constructor

Initialize.

Parameters:



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

#grammarAce::Grammar (readonly)

The grammar.

Returns:



20
21
22
# File 'lib/antelope/generation/constructor.rb', line 20

def grammar
  @grammar
end

#productionsObject (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.

Parameters:



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

#callvoid

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.

See Also:



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)

Raises:

  • (ArgumentError)


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