-
Notifications
You must be signed in to change notification settings - Fork 0
/
.goutputstream-3484UW
125 lines (107 loc) · 4 KB
/
.goutputstream-3484UW
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
_____ _ __ _ _ _ __ _ _ _
/ ____| | / _| | | | (_) / _| | | | | |
| (___ | |_ ___| |_ __ _ _ __ __| |_ _ | |_ ___| |_ ___ _ __ ___ ___| |__ _ __ ___ __| | |
\___ \| __/ _ \ _/ _` | '_ \ / _` | | | | | | |/ _ \ _/ _ \ '__/ __|/ __| '_ \ | '_ \ / _ \/ _` | |
____) | || __/ || (_| | | | |_ | (_| | |_| | | | | __/ || __/ | \__ \ (__| | | | | | | | __/ (_| |_|
|_____/ \__\___|_| \__,_|_| |_( ) \__,_|\__,_| |_|_|\___|_| \___|_| |___/\___|_| |_| |_| |_|\___|\__,_(_)
|/
*/
%code top{
#include <stdio.h>
#include <stdlib.h>
extern struct PROGRAM root;
}
%code requires{
#include "AST.h"
}
%union {
int ivalue;
float fvalue;
char *string;
struct PROGRAM *prog;
struct DECLARATION *decl;
struct FUNCTION *func;
struct PARAMETER *param;
struct COMPOUNDSTMT *cmpstmt;
struct STMT *stmt;
struct ASSIGN *assignstmt;
struct CALL *callstmt;
struct ARGLIST *arglist;
struct WHILEs *whilestmt;
struct FORs *forstmt;
struct IFs *ifstmt;
struct EXPR *exp;
struct BINOP *binop;
struct IDs *id;
struct IDENTIFIER *idenf;
Typee typee;
}
%type <typee> type
%type <arglist> argList
%type <ifstmt> ifStmt
%type <stmt> statement stmtList
%type <forstmt> forStmt
%type <assignstmt> assignStmt
%type <callstmt> callStmt
%type <whilestmt> whileStmt
%type <func> funcList function
%type <cmpstmt> compoundStatement
%type <decl> decList declaration
%type <param> paramList
%type <exp> retStmt expr
%type <prog> program
%type <idenf> identifier identList
%token <type> INT
%token <ivalue> INTNUM
%token <type> FLOAT
%token <fvalue> FLOATNUM
%token FOR WHILE DO
%token IF ELSE
%token RETURN
%token <string> ID
%token DIV MULT PLUS MINUS LT NE EQ GT LE GE ASSIGN
%token LPARENT RPARENT LFANCYBRACKET RFANCYBRACKET LBRACKET RBRACKET
%token SEMICOLON COLON
%right ASSIGN
%left LE GE EQ NE LT GT
%right MINUS PLUS DIV MULT
%right RPARENT ELSE
%right COLON
%left LPARENT SEMICOLON
%%
program: decList funcList { struct PROGRAM p; p.DeclList = $1; p.FuncList = $2; root = p; $$ = &p; }
| decList { struct PROGRAM p; p.DeclList = $1; root = p; $$ = &p; }
| funcList { struct PROGRAM p; p.FuncList = $1; root = p; $$ = &p; }
| /* empty */ { struct PROGRAM p; $$ = &p; }
;
funcList: function { $$ = $1; }
| funcList function { $1->prev = $2; }
;
decList: declaration { $$ = $1; }
| decList declaration { $1->prev = $2; }
;
declaration: type identList SEMICOLON { struct DECLARATION d; d.t = $1; d.ilist = $2; $$ = &d; };
identList: identifier { $$ = $1; }
| identList COLON identifier { $1->prev = $3; }
;
identifier: ID { struct IDs i; i.ID = $1; $$ = &i; }
| ID LBRACKET expr RBRACKET { struct IDs i; i.ID = $1; i.expr = $3; $$ = &i; }
;
function: type ID LPARENT paramList RPARENT compoundStatement { struct FUNCTION f; f.t = $1; f.ID = $2; f.ParamList = $4; f.CStmt = $6; f.prev = NULL; $$ = &f; }
| type ID LPARENT RPARENT compoundStatement { struct FUNCTION f; f.t = $1; f.ID = $2; f.CStmt = $5; $$ = &f; }
;
paramList: type identifier { struct PARAMETER p; p.t = $1; p.id = $2; $$ = &p; }
| paramList type identifier { struct PARAMETER p; p.t = $2; p.id = $3; $1->prev = &p; $$ = &p; }
;
type: INT { $$ = eInt; }
| FLOAT { $$ = eFloat; }
;
compoundStatement: LFANCYBRACKET stmtList RFANCYBRACKET { struct COMPOUNDSTMT c; c.StmtList = $2; $$ = &c; }
| LFANCYBRACKET decList stmtList RFANCYBRACKET { struct COMPOUNDSTMT c; c.DeclList = $2; c.StmtList = $3; $$ = &c; }
;
stmtList: statement { $$ = $1; }
| stmtList statement { $1->prev = $2; }
//| /* empty */ { } ----> TODO shift / reduce
;
statement: assignStmt { struct STMT s; s.e_stmt = eA