1%{
2/* $OpenBSD: parser.y,v 1.7 2012/04/12 17:00:11 espie Exp $ */
3/*
4 * Copyright (c) 2004 Marc Espie <espie@cvs.openbsd.org>
5 *
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.
9 *
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.
17 */
18
19#include <math.h>
20#include <stdint.h>
21#define YYSTYPE	int32_t
22extern int32_t end_result;
23extern int yylex(void);
24extern int yyerror(const char *);
25%}
26%token NUMBER
27%token ERROR
28%left LOR
29%left LAND
30%left '|'
31%left '^'
32%left '&'
33%left EQ NE
34%left '<' LE '>' GE
35%left LSHIFT RSHIFT
36%left '+' '-'
37%left '*' '/' '%'
38%right EXPONENT
39%right UMINUS UPLUS '!' '~'
40
41%%
42
43top	: expr { end_result = $1; }
44	;
45expr 	: expr '+' expr { $$ = $1 + $3; }
46     	| expr '-' expr { $$ = $1 - $3; }
47	| expr EXPONENT expr { $$ = pow($1, $3); }
48     	| expr '*' expr { $$ = $1 * $3; }
49	| expr '/' expr {
50		if ($3 == 0) {
51			yyerror("division by zero");
52			exit(1);
53		}
54		$$ = $1 / $3;
55	}
56	| expr '%' expr {
57		if ($3 == 0) {
58			yyerror("modulo zero");
59			exit(1);
60		}
61		$$ = $1 % $3;
62	}
63	| expr LSHIFT expr { $$ = $1 << $3; }
64	| expr RSHIFT expr { $$ = $1 >> $3; }
65	| expr '<' expr { $$ = $1 < $3; }
66	| expr '>' expr { $$ = $1 > $3; }
67	| expr LE expr { $$ = $1 <= $3; }
68	| expr GE expr { $$ = $1 >= $3; }
69	| expr EQ expr { $$ = $1 == $3; }
70	| expr NE expr { $$ = $1 != $3; }
71	| expr '&' expr { $$ = $1 & $3; }
72	| expr '^' expr { $$ = $1 ^ $3; }
73	| expr '|' expr { $$ = $1 | $3; }
74	| expr LAND expr { $$ = $1 && $3; }
75	| expr LOR expr { $$ = $1 || $3; }
76	| '(' expr ')' { $$ = $2; }
77	| '-' expr %prec UMINUS { $$ = -$2; }
78	| '+' expr %prec UPLUS  { $$ = $2; }
79	| '!' expr { $$ = !$2; }
80	| '~' expr { $$ = ~$2; }
81	| NUMBER
82	;
83%%
84
85