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