forked from ncourvoisier/ASCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtle-parser.y
More file actions
68 lines (51 loc) · 1.22 KB
/
turtle-parser.y
File metadata and controls
68 lines (51 loc) · 1.22 KB
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
60
61
62
63
64
65
66
67
68
%{
#include <stdio.h>
#include "turtle-ast.h"
int yylex();
void yyerror(struct ast *ret, const char *);
%}
%debug
%defines
%define parse.error verbose
%parse-param { struct ast *ret }
%union {
double value;
const char *name;
struct ast_node *node;
}
%token <value> VALUE "value"
%token <name> NAME "name"
%token KW_FORWARD "forward"
%token KW_DOWN "down"
%token KW_HEADING "heading"
%token KW_REPEAT "repeat"
%token KW_RIGHT "right"
%token KW_LEFT "left"
%token KW_CALL "call"
%token KW_POSITION "position"
%token KW_UP "up"
%token KW_FW "fw"
%token KW_BW "bw"
%token KW_PROC "proc"
/* TODO: add other tokens */
%type <node> unit cmds cmd expr
%%
unit:
cmds { $$ = $1; ret->unit = $$; }
;
cmds:
cmd cmds { $1->next = $2; $$ = $1; }
| /* empty */ { $$ = NULL; }
;
cmd:
KW_FORWARD expr { $$ = make_cmd_forward($2); }
;
expr:
VALUE { $$ = make_expr_value($1); }
/* TODO: add identifier */
;
%%
void yyerror(struct ast *ret, const char *msg) {
(void) ret;
fprintf(stderr, "%s\n", msg);
}