-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
57 lines (40 loc) · 812 Bytes
/
parser.y
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
52
53
54
55
56
57
%{
#include "parser.h"
#include "scanner.h"
void yyerror(yyscan_t scanner, int* value, const char* msg);
%}
%code requires {
#ifndef YYSTYPE
#define YYSTYPE int
#endif
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
}
%output "parser.c"
%defines "parser.h"
%error-verbose
%define api.pure full
%parse-param {yyscan_t* scanner}
%parse-param {int *value}
%lex-param {yyscan_t *scanner}
%token T_NUMBER
%%
start:
expr { *value = $1; }
expr:
expr '+' term { $$ = $1 + $3; }
| expr '-' term { $$ = $1 - $3; }
| term { $$ = $1; }
;
term:
term '*' factor { $$ = $1 * $3; }
| term '/' factor { $$ = $1 / $3; }
| factor { $$ = $1; }
;
factor:
T_NUMBER { $$ = $1; }
| '(' expr ')' { $$ = $2; }
;
%%