source: trunk/essentials/sys-devel/flex/examples/manual/expr.y

Last change on this file was 3031, checked in by bird, 18 years ago

flex 2.5.33.

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