Sha256: 20927c3fbfb7618ef84f94a509b70d59ed5584c3d24b92524912e36de5937e2b

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

%require "~> 0.1"
%generator "ruby"

%null.data ruby.error-class { SyntaxError }
%panic-mode true
%output.verbose true
%terminal NUMBER
%terminal MULTIPLY "*"
%terminal EXPONENTIATE "^"
%terminal DIVIDE "/"
%terminal ADD "+"
%terminal SUBTRACT "-"
%terminal LPAREN "("
%terminal RPAREN ")"

%right EXPONENTIATE
%left MULTIPLY DIVIDE
%left ADD SUBTRACT

%%

expression: NUMBER                             { |match| match[0][1]         }
          | expression EXPONENTIATE expression { |match| match[0]** match[2] }
          | expression ADD expression          { |match| match[0] + match[2] }
          | expression SUBTRACT expression     { |match| match[0] - match[2] }
          | expression MULTIPLY expression     { |match| match[0] * match[2] }
          | expression DIVIDE expression       { |match| match[0] / match[2] }
          | LPAREN expression RPAREN           { |match| match[1]            }
          | LPAREN error RPAREN                { |match| match[1]            }

%%

class ExampleParser
  %{write}

  def type(token)
    token[0]
  end
end

input = [
  [:LPAREN],
  [:NUMBER, 2],
  [:ADD],
  [:ADD, 2],
  [:RPAREN],
  [:MULTIPLY],
  [:NUMBER, 3]
]

p ExampleParser.new.parse(input)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
antelope-0.3.2 examples/example.ace