# calc.ry. An example expression parser lifted straight out of the bison # info pages. %{ ## Any require statements go here. Methods or constants defined here ## will be part of the parser class. %} %token NUM %left '-' '+' %left '*' '/' %left NEG # negation--unary minus %right '^' # exponentiation %% input: # empty string | input line ; line: '\n' | error '\n' # example error recovery--throw away everything before \n | exp '\n' { printf ("\t%.10g\n", _$1) } ; exp: NUM { _$$ = _$1 } | exp '+' exp { _$$ = _$1 + _$3 } | exp '-' exp { _$$ = _$1 - _$3 } | exp '*' exp { _$$ = _$1 * _$3 } | exp '/' exp { _$$ = _$1 / _$3 } | '-' exp %prec NEG { _$$ = -_$2 } | exp '^' exp { _$$ = _$1 ** _$3 } | '(' exp ')' { _$$ = _$2 } | '(' error ')' { _$$ = 1 } ## example error recovery--a bad expression is interpreted as a 1 ; %% ## Ruby code here will be placed *after* the parser class definition.