%right '='
%left '-' '+'
%left '*' '/'
%left NEG
%defaultaction { "$_[1] $_[3] $_[2]" }
# example of incremental lexer
%lexer {
if ($self->YYEndOfInput) {
print "Asking for more input: ";
my $file = $self->YYInputFile;
$_ = <$file>;
return ('', undef) unless $_;
}
m/\G[ \t]/gc;
m/\G([0-9]+(?:\.[0-9]+)?)/gc and return('NUM',$1);
m/\G([A-Za-z][A-Za-z0-9_]*)/gc and return('VAR',$1);
m/\G(.)/gcs and return($1,$1);
}
%%
input: {}
| input line {}
;
line: '\n' {}
| exp '\n' { print "$_[1]\n" }
| error '\n' {}
;
exp: NUM { $_[1] }
| VAR { $_[1] }
| VAR '=' exp
| exp '+' exp
| exp '-' exp
| exp '*' exp
| exp '/' exp
| '-' exp %prec NEG { "$_[2] NEG" }
| '(' exp ')' { $_[2] }
;
%%