xref: /openbsd-src/usr.bin/m4/parser.y (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 %{
2 /* $OpenBSD: parser.y,v 1.6 2008/08/21 21:00:14 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 #include <stdint.h>
19 #define YYSTYPE	int32_t
20 extern int32_t end_result;
21 extern int yylex(void);
22 extern int yyerror(const char *);
23 %}
24 %token NUMBER
25 %token ERROR
26 %left LOR
27 %left LAND
28 %left '|'
29 %left '^'
30 %left '&'
31 %left EQ NE
32 %left '<' LE '>' GE
33 %left LSHIFT RSHIFT
34 %left '+' '-'
35 %left '*' '/' '%'
36 %right UMINUS UPLUS '!' '~'
37 
38 %%
39 
40 top	: expr { end_result = $1; }
41 	;
42 expr 	: expr '+' expr { $$ = $1 + $3; }
43      	| expr '-' expr { $$ = $1 - $3; }
44      	| expr '*' expr { $$ = $1 * $3; }
45 	| expr '/' expr {
46 		if ($3 == 0) {
47 			yyerror("division by zero");
48 			exit(1);
49 		}
50 		$$ = $1 / $3;
51 	}
52 	| expr '%' expr {
53 		if ($3 == 0) {
54 			yyerror("modulo zero");
55 			exit(1);
56 		}
57 		$$ = $1 % $3;
58 	}
59 	| expr LSHIFT expr { $$ = $1 << $3; }
60 	| expr RSHIFT expr { $$ = $1 >> $3; }
61 	| expr '<' expr { $$ = $1 < $3; }
62 	| expr '>' expr { $$ = $1 > $3; }
63 	| expr LE expr { $$ = $1 <= $3; }
64 	| expr GE expr { $$ = $1 >= $3; }
65 	| expr EQ expr { $$ = $1 == $3; }
66 	| expr NE expr { $$ = $1 != $3; }
67 	| expr '&' expr { $$ = $1 & $3; }
68 	| expr '^' expr { $$ = $1 ^ $3; }
69 	| expr '|' expr { $$ = $1 | $3; }
70 	| expr LAND expr { $$ = $1 && $3; }
71 	| expr LOR expr { $$ = $1 || $3; }
72 	| '(' expr ')' { $$ = $2; }
73 	| '-' expr %prec UMINUS { $$ = -$2; }
74 	| '+' expr %prec UPLUS  { $$ = $2; }
75 	| '!' expr { $$ = !$2; }
76 	| '~' expr { $$ = ~$2; }
77 	| NUMBER
78 	;
79 %%
80 
81