-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.y
93 lines (70 loc) · 2.62 KB
/
python.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
%{
// #pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
int yylex();
void yyerror(const char* msg);
%}
%union {
struct node_t *ast;
char *str;
}
%define parse.error verbose
%token <str> TOKEN_KEYWORD_IMPORT
%token <str> TOKEN_KEYWORD_FROM
%token <str> TOKEN_IDENTIFIER
%token <str> TOKEN_KEYWORD_INT
%token TOKEN_COLON TOKEN_ASSIGN
%token <str> TOKEN_INTEGER
%token <str> TOKEN_KEYWORD_FLOAT
%token <str> TOKEN_FLOAT
%token TOKEN_COMMA TOKEN_OPEN_SQUARE_BRACK TOKEN_CLOSE_SQUARE_BRACK TOKEN_KEYWORD_LIST TOKEN_INDENT
%token TOKEN_EOL
%type <ast> importation instantiate from_def import_def import_kw identifier from_kw inst_int int_kw a_int inst_float float_kw a_float
%start input
%%
input: /* empty */
| ending
| input importation ending
| input instantiate ending
;
ending : TOKEN_EOL
;
/* IMPORTATION RULES */
importation: import_def
| from_def import_def { $$ = ast_init_import($2, $1); print_node($$); };
;
import_def: import_kw identifier { $$ = ast_init_import($2, NULL); };
from_def: from_kw identifier { $$ = ast_init_from($2); };
import_kw: TOKEN_KEYWORD_IMPORT { $$ = ast_init_kw($1); };
from_kw: TOKEN_KEYWORD_FROM { $$ = ast_init_kw($1); };
/* INSTANTIATE RULES */
instantiate: inst_int
| inst_float
// | inst_array_int
;
inst_int: identifier TOKEN_COLON int_kw TOKEN_ASSIGN a_int { $$ = ast_init_inst($1, $3, $5); };
inst_float: identifier TOKEN_COLON float_kw TOKEN_ASSIGN a_float { $$ = ast_init_inst($1, $3, $5); };
identifier: TOKEN_IDENTIFIER { $$ = ast_init_identifier($1); };
int_kw: TOKEN_KEYWORD_INT { $$ = ast_init_type($1); };
float_kw: TOKEN_KEYWORD_FLOAT { $$ = ast_init_type($1); };
a_int: TOKEN_INTEGER { $$ = ast_init_value($1); };
a_float: TOKEN_FLOAT { $$ = ast_init_value($1); };
/* ARRAYS */
// inst_array_int: TOKEN_IDENTIFIER TOKEN_COLON TOKEN_KEYWORD_LIST TOKEN_OPEN_SQUARE_BRACK TOKEN_KEYWORD_INT TOKEN_CLOSE_SQUARE_BRACK TOKEN_ASSIGN array_int;
// array_int: TOKEN_OPEN_SQUARE_BRACK list_int TOKEN_CLOSE_SQUARE_BRACK;
// list_int: /* empty */
// | list_int TOKEN_INTEGER TOKEN_COMMA
// | list_int TOKEN_INTEGER
// ;
// inst_array_float: TOKEN_IDENTIFIER TOKEN_COLON TOKEN_KEYWORD_LIST TOKEN_OPEN_SQUARE_BRACK TOKEN_KEYWORD_FLOAT TOKEN_CLOSE_SQUARE_BRACK TOKEN_ASSIGN array_float;
// array_float: TOKEN_OPEN_SQUARE_BRACK list_float TOKEN_CLOSE_SQUARE_BRACK;
// list_float: /* empty */
// | list_float TOKEN_INTEGER TOKEN_COMMA
// | list_float TOKEN_INTEGER
// ;
%%
void yyerror(const char *msg) {
fprintf(stderr, "error: %s\n", msg);
}