2 /* $OpenBSD: parser.y,v 1.6 2008/08/21 21:00:14 espie Exp $ */
4 * Copyright (c) 2004 Marc Espie <espie@cvs.openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #define YYSTYPE int32_t
23 extern int32_t end_result;
24 extern int yylex(void);
25 extern int yyerror(const char *);
40 %right UMINUS UPLUS '!' '~'
44 top : expr { end_result = $1; }
46 expr : expr '+' expr { $$ = $1 + $3; }
47 | expr '-' expr { $$ = $1 - $3; }
48 | expr EXPONENT expr { $$ = pow($1, $3); }
49 | expr '*' expr { $$ = $1 * $3; }
52 yyerror("division by zero");
59 yyerror("modulo zero");
64 | expr LSHIFT expr { $$ = $1 << $3; }
65 | expr RSHIFT expr { $$ = $1 >> $3; }
66 | expr '<' expr { $$ = $1 < $3; }
67 | expr '>' expr { $$ = $1 > $3; }
68 | expr LE expr { $$ = $1 <= $3; }
69 | expr GE expr { $$ = $1 >= $3; }
70 | expr EQ expr { $$ = $1 == $3; }
71 | expr NE expr { $$ = $1 != $3; }
72 | expr '&' expr { $$ = $1 & $3; }
73 | expr '^' expr { $$ = $1 ^ $3; }
74 | expr '|' expr { $$ = $1 | $3; }
75 | expr LAND expr { $$ = $1 && $3; }
76 | expr LOR expr { $$ = $1 || $3; }
77 | '(' expr ')' { $$ = $2; }
78 | '-' expr %prec UMINUS { $$ = -$2; }
79 | '+' expr %prec UPLUS { $$ = $2; }
80 | '!' expr { $$ = !$2; }
81 | '~' expr { $$ = ~$2; }