Class: LL::ConfigurationCompiler
- Inherits:
-
Object
- Object
- LL::ConfigurationCompiler
- 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 =
{ :eof => -1, :rule => 0, :terminal => 1, :epsilon => 2, :action => 3 }.freeze
- DEFAULT_RUBY_CODE =
'val'.freeze
Instance Method Summary collapse
- #generate(grammar) ⇒ LL::CompiledConfiguration
- #generate_action_bodies(grammar) ⇒ Hash
- #generate_actions(grammar) ⇒ Array
- #generate_name(grammar) ⇒ String
- #generate_namespace(grammar) ⇒ Array
-
#generate_rules(grammar) ⇒ Array
Builds the rules table of the parser.
-
#generate_table(grammar) ⇒ Array
Generates the table array for the parser.
-
#generate_terminals(grammar) ⇒ Array
Returns an Array containing all the terminal names as symbols.
Instance Method Details
#generate(grammar) ⇒ LL::CompiledConfiguration
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ll/configuration_compiler.rb', line 27 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ll/configuration_compiler.rb', line 102 def generate_action_bodies(grammar) bodies = {} index = 0 grammar.rules.each do |rule| rule.branches.each do |branch| bodies[:"_rule_#{index}"] = branch.ruby_code || DEFAULT_RUBY_CODE index += 1 end end return bodies end |
#generate_actions(grammar) ⇒ Array
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ll/configuration_compiler.rb', line 81 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
45 46 47 |
# File 'lib/ll/configuration_compiler.rb', line 45 def generate_name(grammar) return grammar.name.split('::').last end |
#generate_namespace(grammar) ⇒ Array
53 54 55 56 57 |
# File 'lib/ll/configuration_compiler.rb', line 53 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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ll/configuration_compiler.rb', line 123 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?(LL::Terminal) row << TYPES[:terminal] row << term_indices[step] + 1 elsif step.is_a?(LL::Rule) row << TYPES[:rule] row << rule_indices[step] elsif step.is_a?(LL::Epsilon) row << TYPES[:epsilon] 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.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ll/configuration_compiler.rb', line 171 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.
67 68 69 70 71 72 73 74 75 |
# File 'lib/ll/configuration_compiler.rb', line 67 def generate_terminals(grammar) terminals = [:$EOF] grammar.terminals.each do |term| terminals << term.name.to_sym end return terminals end |