# examples/eyapplanguageref/Calc.eyp
%whites = /([ \t]*(?:#.*)?)/
%token NUM = /([0-9]+(?:\.[0-9]+)?)/
%token VAR = /([A-Za-z][A-Za-z0-9_]*)/
%{
my %s; # symbol table
%}
%%
start:
input { \%s }
;
input: line *
;
line:
'\n' { undef }
| exp '\n' {
print "$_[1]\n" if defined($_[1]);
$_[1]
}
| error '\n'
{
$_[0]->YYErrok;
undef
}
;
exp:
NUM
| $VAR { $s{$VAR} }
| $VAR '=' $exp { $s{$VAR} = $exp }
| exp.left '+' exp.right { $left + $right }
| exp.left '-' exp.right { $left - $right }
| exp.left '*' exp.right { $left * $right }
| exp.left '/' exp.right
{
$_[3] and return($_[1] / $_[3]);
$_[0]->YYData->{ERRMSG} = "Illegal division by zero.\n";
$_[0]->YYError;
undef
}
| '-' $exp { -$exp }
| exp.left '^' exp.right { $left ** $right }
| '(' $exp ')' { $exp }
;
%%
=head1 SYNOPSIS
Compile it with
eyapp -C Calc.eyp
run it with:
./Calc.pm
Provide several expressions as input and press CTRL-D
=cut