1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
Simple calculator implemented in Perl.
The grammar is standard YACC, but the actions are in Perl.
This YACC file must be processed by a version of YACC that
supports Perl output.
The calculator handles the basic operations, but nothing fancy.
This is mostly a demonstration of the Perl/Yacc combination.
$P holds the previous value, that is, the value of the most
recent expression. The user can refer to this with "%".
%V holds user-defined variables; variables are defined by assinging
to them. References to an undefined variable produces a warning,
and assigns 0 to the variable.
The user can also get a Perl escape with &, followed by a string.
Ray Lischner
17 August 1990
*/
%token INT FLOAT STRING IDENT
%left '='
%left '|'
%left '&'
%left EQ NE
%left GT GE LT LE
%left L_SHIFT R_SHIFT
%left '+' '-'
%left '*' '/'
%right EXP
%right '!'
%right UNARY
%start stmt_list
%%
stmt_list: /* empty */
| stmt_list stmt
;
stmt: terminator
| expr terminator
{ print $1, "\n" if $2 eq "\n"; $P = $1; }
| error terminator
{ &yyerrok; }
;
terminator: ';'
{ $$ = $1; }
| '\n'
{ $$ = $1; }
;
expr: '(' expr ')'
{ $$ = $2; }
| expr '|' expr
{ $$ = $1 || $3; }
| expr '&' expr
{ $$ = $1 && $3; }
| expr EQ expr
{ $$ = $1 == $3; }
| expr NE expr
{ $$ = $1 != $3; }
| expr GT expr
{ $$ = $1 > $3; }
| expr GE expr
{ $$ = $1 >= $3; }
| expr LT expr
{ $$ = $1 < $3; }
| expr LE expr
{ $$ = $1 <= $3; }
| expr L_SHIFT expr
{ $$ = $1 << $3; }
| expr R_SHIFT expr
{ $$ = $1 >> $3; }
| expr '+' expr
{ $$ = $1 + $3; }
| expr '-' expr
{ $$ = $1 - $3; }
| expr '*' expr
{ $$ = $1 * $3; }
| expr '/' expr
{ $$ = $1 / $3; }
| expr EXP expr
{ $$ = $1 ** $3; }
| expr '!'
{ $$ = &fact($1); }
| '-' expr %prec UNARY
{ $$ = -$2; }
| '!' expr %prec UNARY
{ $$ = !$2; }
| '+' expr %prec UNARY
{ $$ = $2; }
| '&' STRING %prec UNARY
{ $$ = eval($2); }
| IDENT '=' expr
{ eval '$V{'.$1.'}=('.$3.'); 1' || &yyerror($@); $$ = $V{$1}; }
| IDENT
{ if (! defined $V{$1}) {
&yyerror($1.": undefined variable");
$V{$1} = 0;
}
$$ = $V{$1};
}
| INT
{ $$ = $1; }
| FLOAT
{ $$ = $1; }
| STRING
{ $$ = $1; }
| '%'
{ $$ = $P; }
;
%%
# Prompt the user on STDERR, but only prompt if STDERR and the input
# file are both terminals.
# read from STDIN if no files are named on the command line
unshift(@ARGV, '-') if $#ARGV < $[;
# After finishing a file, open the next one. Return whether there
# really is a next one that was opened.
sub next_file
{
while ($ARGV = shift(@ARGV)) {
if (! open(ARGV, $ARGV)) {
print STDERR "$ARGV: cannot open file: $!\n";
next;
}
$prompt = (-t ARGV && -t STDERR) ? '(Calc) ' : '';
last;
}
$ARGV >= $[;
}
# print he prompt
sub prompt
{
print STDERR $prompt if $prompt;
}
# print an error message
sub yyerror
{
print STDERR "\"$ARGV\", " if $ARGV ne '-';
print STDERR "line $.: ", @_, "\n";
}
# Hand-coded lex until I write lex -p, too!
sub yylex
{
lexloop:
{
# get a line of input, if we need it.
if ($line eq '') {
&prompt;
$line = <ARGV>;
if ($line eq '') {
close(ARGV);
&next_file || return(0);
}
}
# Skip over white space, and grab the first character.
# If there is no such character, then grab the next line.
$line =~ s/^[ \t\f\r\v]*(.|\n)// || next lexloop;
local($char) = $1;
if ($char eq '#') {
# comment, so discard the line
$line = "\n";
&yylex;
} elsif ($char =~ /^['"]/) {
# collect the string
if ($line =~ s/^([^$char]*)$char//) {
$yylval = $1;
} else {
&yyerror('unterminated string');
$yylval = '';
}
$STRING;
} elsif ($char =~ /^\d/) {
# number, is it integer or float?
if ($line =~ s/^(\d+)//) {
$yylval = int($char . $1);
} else {
$yylval = int($char);
}
$type = $INT;
if ($line =~ s/^(\.\d*)//) {
$tmp = "0$1";
$yylval += $tmp;
$type = $FLOAT;
}
if ($line =~ s/^[eE]([-+]*\d+)//) {
$yylval *= 10 ** $1;
$type = $FLOAT;
}
$type;
} elsif ($char =~ /^\w/) {
# identifier
$line =~ s/^([\w\d]*)//;
$yylval = $char.$1;
$IDENT;
} elsif ($char eq '*' && $line =~ s/^\*//) {
$EXP;
} elsif ($char eq '!' && $line =~ s/^=//) {
$NE;
} elsif ($char eq '=' && $line =~ s/^=//) {
$EQ;
} elsif ($char =~ /^[<>]/ && $line =~ s/^=//) {
$char eq '<' ? $LE : $GE;
} elsif ($char =~ /^[<>]/ && $line =~ s/^$char//) {
$char eq '<' ? $L_SHIFT : $R_SHIFT;
} else {
$yylval = $char;
ord($char);
}
}
}
# factorial
sub fact
{
local($n) = @_;
local($f) = 1;
$f *= $n-- while ($n > 1) ;
$f;
}
# catch signals
sub catch
{
local($signum) = @_;
print STDERR "\n" if (-t STDERR && -t STDIN);
&yyerror("Floating point exception") if $signum = 8;
next outer;
}
$SIG{'INT'} = 'catch';
$SIG{'FPE'} = 'catch';
select(STDERR); $| = 1;
select(STDOUT);
&next_file;
# main program
outer: while(1)
{
$line = '';
eval '$status = &yyparse;';
exit $status if ! $@;
&yyerror($@);
}
|