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