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 * $FreeBSD: releng/10.3/usr.bin/m4/parser.y 250226 2013-05-03 23:29:38Z jkim $
19 */
20
21#include <math.h>
22#include <stddef.h>
23#include <stdio.h>
24#include <stdint.h>
25
26#include "mdef.h"
27#include "extern.h"
28
29#define YYSTYPE	int32_t
30
31extern int yylex(void);
32extern int yyerror(const char *);
33%}
34%token NUMBER
35%token ERROR
36%left LOR
37%left LAND
38%left '|'
39%left '^'
40%left '&'
41%left EQ NE
42%left '<' LE '>' GE
43%left LSHIFT RSHIFT
44%left '+' '-'
45%left '*' '/' '%'
46%right EXPONENT
47%right UMINUS UPLUS '!' '~'
48
49%%
50
51top	: expr { end_result = $1; }
52	;
53expr 	: expr '+' expr { $$ = $1 + $3; }
54     	| expr '-' expr { $$ = $1 - $3; }
55	| expr EXPONENT expr { $$ = pow($1, $3); }
56     	| expr '*' expr { $$ = $1 * $3; }
57	| expr '/' expr {
58		if ($3 == 0) {
59			yyerror("division by zero");
60			exit(1);
61		}
62		$$ = $1 / $3;
63	}
64	| expr '%' expr {
65		if ($3 == 0) {
66			yyerror("modulo zero");
67			exit(1);
68		}
69		$$ = $1 % $3;
70	}
71	| expr LSHIFT expr { $$ = $1 << $3; }
72	| expr RSHIFT expr { $$ = $1 >> $3; }
73	| expr '<' expr { $$ = $1 < $3; }
74	| expr '>' expr { $$ = $1 > $3; }
75	| expr LE expr { $$ = $1 <= $3; }
76	| expr GE expr { $$ = $1 >= $3; }
77	| expr EQ expr { $$ = $1 == $3; }
78	| expr NE expr { $$ = $1 != $3; }
79	| expr '&' expr { $$ = $1 & $3; }
80	| expr '^' expr { $$ = $1 ^ $3; }
81	| expr '|' expr { $$ = $1 | $3; }
82	| expr LAND expr { $$ = $1 && $3; }
83	| expr LOR expr { $$ = $1 || $3; }
84	| '(' expr ')' { $$ = $2; }
85	| '-' expr %prec UMINUS { $$ = -$2; }
86	| '+' expr %prec UPLUS  { $$ = $2; }
87	| '!' expr { $$ = !$2; }
88	| '~' expr { $$ = ~$2; }
89	| NUMBER
90	;
91%%
92
93