blob: 98885e5c8766d2e8649cd037f600b50a3d833cec (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
%{
#include <stdio.h>
int yylex();
int yyerror(const char* error);
#define YYDEBUG 1
%}
%start input
%define api.value.type {float}
%token NUMBER
%token PLUS
%token MINUS
%token MULT
%token DIV
/* Define both - precedence and associativity */
%left PLUS MINUS
%left MULT DIV
%%
input: /* empty */
| expr { printf("%f\n", $1); }
;
expr: NUMBER { $$ = $1; }
| '(' expr ')' { $$ = $2; }
| expr PLUS expr { $$ = $1 + $3; }
| expr MINUS expr { $$ = $1 - $3; }
| expr MULT expr { $$ = $1 * $3; }
| expr DIV expr { $$ = $1 / $3; }
%%
int yyerror(const char* error)
{
extern int current_line;
fprintf(stderr, "input:%d: error: %s\n", current_line, error);
return 0;
}
int main()
{
#if YYDEBUG
yydebug = 1;
#endif
yyparse();
return 0;
}
|