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 | %%
|
---|
28 | input :
|
---|
29 | | input line
|
---|
30 | ;
|
---|
31 |
|
---|
32 | line : EOL
|
---|
33 | | exp EOL { printf("%g\n",$1);}
|
---|
34 |
|
---|
35 | exp : 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 |
|
---|
47 | yyerror(char *message)
|
---|
48 | {
|
---|
49 | printf("%s\n",message);
|
---|
50 | }
|
---|
51 |
|
---|
52 | int 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.