-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
59 lines (42 loc) · 918 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
58
59
%require "3.8"
%code top {
#include <stdio.h> /* fprintf. */
#include "ast.h"
int yylex(char **content);
void yyerror(char **content, char const *);
parser_ast *ast = NULL;
}
%param {char **content}
%define api.header.include {"parser.h"}
%define api.value.type {parser_t}
%token <number> TOK_NUMBER
%type <ast> expr
%left '-' '+'
%%
line:
%empty
| expr { ast = $1; }
;
expr:
TOK_NUMBER { $$ = create_ast("NUMBER", $1); }
| expr '+' expr { $$ = create_ast_operation("OPERATION_PLUS", $1, $3); }
| expr '-' expr { $$ = create_ast_operation("OPERATION_MINUS", $1, $3); }
;
%%
void yyerror (char **content, char const *s)
{
fprintf (stderr, "%s\n", s);
}
parser_ast* parse(char *content) {
yyparse(&content);
return ast;
}
int main(int argc, char *argv[])
{
ast = parse(argv[1]);
if (ast == NULL) {
return 1;
}
dump_ast(ast, 0);
return 0;
}