expr.y revision 1.1.1.1
1/* $NetBSD: expr.y,v 1.1.1.1 2009/10/26 00:28:33 christos Exp $ */ 2 3/* 4 * expr.y : A simple yacc expression parser 5 * Based on the Bison manual example. 6 */ 7 8%{ 9#include <stdio.h> 10#include <math.h> 11 12%} 13 14%union { 15 float val; 16} 17 18%token NUMBER 19%token PLUS MINUS MULT DIV EXPON 20%token EOL 21%token LB RB 22 23%left MINUS PLUS 24%left MULT DIV 25%right EXPON 26 27%type <val> exp NUMBER 28 29%% 30input : 31 | input line 32 ; 33 34line : EOL 35 | exp EOL { printf("%g\n",$1);} 36 37exp : NUMBER { $$ = $1; } 38 | exp PLUS exp { $$ = $1 + $3; } 39 | exp MINUS exp { $$ = $1 - $3; } 40 | exp MULT exp { $$ = $1 * $3; } 41 | exp DIV exp { $$ = $1 / $3; } 42 | MINUS exp %prec MINUS { $$ = -$2; } 43 | exp EXPON exp { $$ = pow($1,$3);} 44 | LB exp RB { $$ = $2; } 45 ; 46 47%% 48 49yyerror(char *message) 50{ 51 printf("%s\n",message); 52} 53 54int main(int argc, char *argv[]) 55{ 56 yyparse(); 57 return(0); 58} 59 60 61 62 63 64 65 66 67