1228697Sbapt%{
2250226Sjkim/* $OpenBSD: parser.y,v 1.7 2012/04/12 17:00:11 espie Exp $ */
3228697Sbapt/*
4228697Sbapt * Copyright (c) 2004 Marc Espie <espie@cvs.openbsd.org>
5228697Sbapt *
6228697Sbapt * Permission to use, copy, modify, and distribute this software for any
7228697Sbapt * purpose with or without fee is hereby granted, provided that the above
8228697Sbapt * copyright notice and this permission notice appear in all copies.
9228697Sbapt *
10228697Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11228697Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12228697Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13228697Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14228697Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15228697Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16228697Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17228697Sbapt *
18228697Sbapt * $FreeBSD$
19228697Sbapt */
20241777Sed
21234850Sbapt#include <math.h>
22241777Sed#include <stddef.h>
23241777Sed#include <stdio.h>
24228697Sbapt#include <stdint.h>
25241777Sed
26241777Sed#include "mdef.h"
27241777Sed#include "extern.h"
28241777Sed
29228697Sbapt#define YYSTYPE	int32_t
30241777Sed
31228697Sbaptextern int yylex(void);
32228697Sbaptextern int yyerror(const char *);
33228697Sbapt%}
34228697Sbapt%token NUMBER
35228697Sbapt%token ERROR
36228697Sbapt%left LOR
37228697Sbapt%left LAND
38228697Sbapt%left '|'
39228697Sbapt%left '^'
40228697Sbapt%left '&'
41228697Sbapt%left EQ NE
42228697Sbapt%left '<' LE '>' GE
43228697Sbapt%left LSHIFT RSHIFT
44228697Sbapt%left '+' '-'
45228697Sbapt%left '*' '/' '%'
46234850Sbapt%right EXPONENT
47228697Sbapt%right UMINUS UPLUS '!' '~'
48228697Sbapt
49228697Sbapt%%
50228697Sbapt
51228697Sbapttop	: expr { end_result = $1; }
52228697Sbapt	;
53228697Sbaptexpr 	: expr '+' expr { $$ = $1 + $3; }
54228697Sbapt     	| expr '-' expr { $$ = $1 - $3; }
55228697Sbapt	| expr EXPONENT expr { $$ = pow($1, $3); }
56228697Sbapt     	| expr '*' expr { $$ = $1 * $3; }
57228697Sbapt	| expr '/' expr {
58228697Sbapt		if ($3 == 0) {
59228697Sbapt			yyerror("division by zero");
60228697Sbapt			exit(1);
61228697Sbapt		}
62228697Sbapt		$$ = $1 / $3;
63228697Sbapt	}
64228697Sbapt	| expr '%' expr {
65228697Sbapt		if ($3 == 0) {
66228697Sbapt			yyerror("modulo zero");
67228697Sbapt			exit(1);
68228697Sbapt		}
69228697Sbapt		$$ = $1 % $3;
70228697Sbapt	}
71228697Sbapt	| expr LSHIFT expr { $$ = $1 << $3; }
72228697Sbapt	| expr RSHIFT expr { $$ = $1 >> $3; }
73228697Sbapt	| expr '<' expr { $$ = $1 < $3; }
74228697Sbapt	| expr '>' expr { $$ = $1 > $3; }
75228697Sbapt	| expr LE expr { $$ = $1 <= $3; }
76228697Sbapt	| expr GE expr { $$ = $1 >= $3; }
77228697Sbapt	| expr EQ expr { $$ = $1 == $3; }
78228697Sbapt	| expr NE expr { $$ = $1 != $3; }
79228697Sbapt	| expr '&' expr { $$ = $1 & $3; }
80228697Sbapt	| expr '^' expr { $$ = $1 ^ $3; }
81228697Sbapt	| expr '|' expr { $$ = $1 | $3; }
82228697Sbapt	| expr LAND expr { $$ = $1 && $3; }
83228697Sbapt	| expr LOR expr { $$ = $1 || $3; }
84228697Sbapt	| '(' expr ')' { $$ = $2; }
85228697Sbapt	| '-' expr %prec UMINUS { $$ = -$2; }
86228697Sbapt	| '+' expr %prec UPLUS  { $$ = $2; }
87228697Sbapt	| '!' expr { $$ = !$2; }
88228697Sbapt	| '~' expr { $$ = ~$2; }
89228697Sbapt	| NUMBER
90228697Sbapt	;
91228697Sbapt%%
92228697Sbapt
93