Class: LL::ConfigurationCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ll/configuration_compiler.rb

Overview

Compiles an instance of CompiledConfiguration which is used by CodeGenerator to actually generate Ruby source code.

Constant Summary collapse

TYPES =

Returns:

  • (Hash)
{
  :eof                => -1,
  :rule               => 0,
  :terminal           => 1,
  :epsilon            => 2,
  :action             => 3,
  :star               => 4,
  :plus               => 5,
  :add_value_stack    => 6,
  :append_value_stack => 7
}.freeze
DEFAULT_RUBY_CODE =

Returns:

  • (String)
'val'.freeze

Instance Method Summary collapse

Instance Method Details

#generate(grammar) ⇒ LL::CompiledConfiguration

Parameters:

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ll/configuration_compiler.rb', line 31

def generate(grammar)
  return CompiledConfiguration.new(
    :name          => generate_name(grammar),
    :namespace     => generate_namespace(grammar),
    :inner         => grammar.inner,
    :header        => grammar.header,
    :terminals     => generate_terminals(grammar),
    :actions       => generate_actions(grammar),
    :action_bodies => generate_action_bodies(grammar),
    :rules         => generate_rules(grammar),
    :table         => generate_table(grammar)
  )
end

#generate_action_bodies(grammar) ⇒ Hash

Parameters:

Returns:

  • (Hash)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ll/configuration_compiler.rb', line 106

def generate_action_bodies(grammar)
  bodies = {}
  index  = 0

  grammar.rules.each do |rule|
    rule.branches.each do |branch|
      if branch.ruby_code
        code = branch.ruby_code

      # If a branch only contains a single, non-epsilon step we can just
      # return that value as-is. This makes parsing code a little bit
      # easier.
      elsif !branch.ruby_code and branch.steps.length == 1 \
      and !branch.steps[0].is_a?(Epsilon)
        code = 'val[0]'

      else
        code = DEFAULT_RUBY_CODE
      end

      bodies[:"_rule_#{index}"] = code

      index += 1
    end
  end

  return bodies
end

#generate_actions(grammar) ⇒ Array

Parameters:

Returns:

  • (Array)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ll/configuration_compiler.rb', line 85

def generate_actions(grammar)
  actions = []
  index   = 0

  grammar.rules.each do |rule|
    rule.branches.each do |branch|
      args = branch.steps.reject { |step| step.is_a?(Epsilon) }.length

      actions << [:"_rule_#{index}", args]

      index += 1
    end
  end

  return actions
end

#generate_name(grammar) ⇒ String

Parameters:

Returns:

  • (String)


49
50
51
# File 'lib/ll/configuration_compiler.rb', line 49

def generate_name(grammar)
  return grammar.name.split('::').last
end

#generate_namespace(grammar) ⇒ Array

Parameters:

Returns:

  • (Array)


57
58
59
60
61
# File 'lib/ll/configuration_compiler.rb', line 57

def generate_namespace(grammar)
  parts = grammar.name.split('::')

  return parts.length > 1 ? parts[0..-2] : []
end

#generate_rules(grammar) ⇒ Array

Builds the rules table of the parser. Each row is built in reverse order.

Parameters:

Returns:

  • (Array)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ll/configuration_compiler.rb', line 141

def generate_rules(grammar)
  rules        = []
  action_index = 0
  rule_indices = grammar.rule_indices
  term_indices = grammar.terminal_indices

  grammar.rules.each_with_index do |rule, rule_index|
    rule.branches.each do |branch|
      row = [TYPES[:action], action_index]

      action_index += 1

      branch.steps.reverse_each do |step|
        if step.is_a?(Terminal)
          row << TYPES[:terminal]
          row << term_indices[step] + 1

        elsif step.is_a?(Rule)
          row << TYPES[:rule]
          row << rule_indices[step]

        elsif step.is_a?(Epsilon)
          row << TYPES[:epsilon]
          row << 0

        elsif step.is_a?(Operator)
          row << TYPES[step.type]
          row << rule_indices[step.receiver]

          row << TYPES[:add_value_stack]
          row << 0
        end
      end

      rules << row
    end
  end

  return rules
end

#generate_table(grammar) ⇒ Array

Generates the table array for the parser. This array has the following structure:

[
  [EOF, TERMINAL 1, TERMINAL 2, TERMINAL 3, ...]
]

EOF is always the first column and is used when running out of input while processing a rule.

Parameters:

Returns:

  • (Array)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ll/configuration_compiler.rb', line 196

def generate_table(grammar)
  branch_index = 0
  term_indices = grammar.terminal_indices
  columns      = grammar.terminals.length + 1

  table = Array.new(grammar.rules.length) do
    Array.new(columns, -1)
  end

  grammar.rules.each_with_index do |rule, rule_index|
    rule.branches.each do |branch|
      branch.first_set.each do |step|
        # For terminals we'll base the column index on the terminal index.
        if step.is_a?(Terminal)
          terminal_index = term_indices[step]

          table[rule_index][terminal_index + 1] = branch_index

        # For the rest (= epsilon) we'll update all columns that haven't
        # been updated yet.
        else
          table[rule_index].each_with_index do |col, col_index|
            table[rule_index][col_index] = branch_index if col == -1
          end
        end
      end

      branch_index += 1
    end
  end

  return table
end

#generate_terminals(grammar) ⇒ Array

Returns an Array containing all the terminal names as symbols. The first terminal is always :$EOF to ensure the array has the same amount of rows as there are columns in the table array.

Parameters:

Returns:

  • (Array)


71
72
73
74
75
76
77
78
79
# File 'lib/ll/configuration_compiler.rb', line 71

def generate_terminals(grammar)
  terminals = [:$EOF]

  grammar.terminals.each do |term|
    terminals << term.name.to_sym
  end

  return terminals
end