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

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,
  :question           => 8
}.freeze
SKIP_VALUE_STACK =

Operators which don’t require a value stack.

Returns:

  • (Array)
[:question]
DEFAULT_RUBY_CODE =

Returns:

  • (String)
'val'.freeze

Instance Method Summary (collapse)

Instance Method Details

- (LL::CompiledConfiguration) generate(grammar)

Parameters:

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ll/configuration_compiler.rb', line 39

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

- (Hash) generate_action_bodies(grammar)

Parameters:

Returns:

  • (Hash)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ll/configuration_compiler.rb', line 114

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

- (Array) generate_actions(grammar)

Parameters:

Returns:

  • (Array)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ll/configuration_compiler.rb', line 93

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

- (String) generate_name(grammar)

Parameters:

Returns:

  • (String)


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

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

- (Array) generate_namespace(grammar)

Parameters:

Returns:

  • (Array)


65
66
67
68
69
# File 'lib/ll/configuration_compiler.rb', line 65

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

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

- (Array) generate_rules(grammar)

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

Parameters:

Returns:

  • (Array)


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
181
182
183
184
185
186
187
188
189
190
# File 'lib/ll/configuration_compiler.rb', line 149

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]

          unless SKIP_VALUE_STACK.include?(step.type)
            row << TYPES[:add_value_stack]
            row << 0
          end
        end
      end

      rules << row
    end
  end

  return rules
end

- (Array) generate_table(grammar)

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)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ll/configuration_compiler.rb', line 206

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

- (Array) generate_terminals(grammar)

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)


79
80
81
82
83
84
85
86
87
# File 'lib/ll/configuration_compiler.rb', line 79

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

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

  return terminals
end