Sha256: f8aa62fb94117585a5fe556dddb668ac24d8d2faa120a884a85f9a09d777d898

Contents?: true

Size: 898 Bytes

Versions: 1

Compression:

Stored size: 898 Bytes

Contents

%require "0.0.1"
%type "ruby"

%terminal NUMBER
%terminal MULTIPLY "*"
%terminal DIVIDE "/"
%terminal ADD "+"
%terminal SUBTRACT "-"
%terminal LPAREN "("
%terminal RPAREN ")"

%nonassoc LPAREN RPAREN
%left MULTIPLY DIVIDE
%left ADD SUBTRACT

%%

expression: NUMBER                         { |a| a[1]        }
          | expression ADD expression      { |a, _, b| a + b }
          | expression SUBTRACT expression { |a, _, b| a - b }
          | expression MULTIPLY expression { |a, _, b| a * b }
          | expression DIVIDE expression   { |a, _, b| a / b }
          | LPAREN expression RPAREN       { |_, a, _| a     }
          | LPAREN error RPAREN

%%

class ExampleParser
  %{write}

  def type(token)
    token[0]
  end
end

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

p ExampleParser.new.parse(input)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
antelope-0.0.1 examples/example.ace