Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created September 8, 2015 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/9b94533a720287f838bb to your computer and use it in GitHub Desktop.
Save yorickpeterse/9b94533a720287f838bb to your computer and use it in GitHub Desktop.
# This file is automatically generated by ruby-ll. Manually changing this file
# is not recommended as any changes will be lost the next time this parser is
# re-generated.
require 'll/setup'
require 'ast'
class MathParser < LL::Driver
CONFIG = LL::DriverConfig.new
CONFIG.terminals = [
:$EOF, # 0
:T_NUMBER, # 1
:T_ADD, # 2
:T_DIV, # 3
:T_MUL, # 4
:T_SUB, # 5
:T_AND, # 6
:T_OR, # 7
].freeze
CONFIG.rules = [
[3, 0, 4, 5, 6, 0, 0, 4], # 0
[3, 1, 0, 4, 0, 2], # 1
[3, 2, 0, 0, 0, 3], # 2
[3, 3, 1, 2], # 3
[3, 4, 1, 3], # 4
[3, 5, 1, 4], # 5
[3, 6, 1, 5], # 6
[3, 7, 1, 6], # 7
[3, 8, 1, 7], # 8
[3, 9, 1, 1], # 9
[3, 10, 0, 1], # 10
].freeze
CONFIG.table = [
[-1, 0, -1, -1, -1, -1, -1, -1], # 0
[-1, -1, 1, 1, 1, 1, 2, 2], # 1
[-1, -1, 3, 4, 5, 6, -1, -1], # 2
[-1, -1, -1, -1, -1, -1, 7, 8], # 3
[-1, 9, -1, -1, -1, -1, -1, -1], # 4
[-1, -1, 10, 10, 10, 10, 10, 10], # 5
].freeze
CONFIG.actions = [
[:_rule_0, 2], # 0
[:_rule_1, 2], # 1
[:_rule_2, 2], # 2
[:_rule_3, 1], # 3
[:_rule_4, 1], # 4
[:_rule_5, 1], # 5
[:_rule_6, 1], # 6
[:_rule_7, 1], # 7
[:_rule_8, 1], # 8
[:_rule_9, 1], # 9
[:_rule_10, 1], # 10
].freeze
def each_token
yield [:T_NUMBER, 10]
yield [:T_ADD, '+']
yield [:T_NUMBER, 20]
yield [:T_ADD, '+']
yield [:T_NUMBER, 30]
yield [:T_AND, 'and']
yield [:T_NUMBER, 40]
yield [:T_DIV, '+']
yield [:T_NUMBER, 50]
yield [:T_MUL, '*']
yield [:T_NUMBER, 2]
yield [-1, -1]
end
def s(type, children)
return AST::Node.new(type, children)
end
def _rule_0(val)
ret = val[0]
# Combines all the operators together in a left-associative manner.
if val[1]
val[1].each do |pair|
ret = s(pair[0], [ret, pair[1]])
end
end
ret
end
def _rule_1(val)
val
end
def _rule_2(val)
val
end
def _rule_3(val)
:plus
end
def _rule_4(val)
:div
end
def _rule_5(val)
:mul
end
def _rule_6(val)
:sub
end
def _rule_7(val)
:and
end
def _rule_8(val)
:or
end
def _rule_9(val)
s(:number, [val[0]])
end
def _rule_10(val)
val[0]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment