Sha256: ef348fdcedbebc1df43719eacd5c838664642c8e521f67abd0d1d58366a8fd61

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

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

%null.data ruby.error-class { SyntaxError }
%output.panic-mode true
%output.verbose true
%ruby.indent 1
%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[0][1]         }
          | expression EXPONENTIATE expression { match[0]** match[2] }
          | expression ADD expression          { match[0] + match[2] }
          | expression SUBTRACT expression     { match[0] - match[2] }
          | expression MULTIPLY expression     { match[0] * match[2] }
          | expression DIVIDE expression       { match[0] / match[2] }
          | LPAREN expression RPAREN           { match[1]            }
          | LPAREN error RPAREN                { 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

2 entries across 2 versions & 1 rubygems

Version Path
antelope-0.4.1 examples/example.ace
antelope-0.4.0 examples/example.ace