-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloc.ypp
135 lines (113 loc) · 3.67 KB
/
floc.ypp
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
126
127
128
129
130
131
132
133
134
135
%{
#include "interpreter.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <forward_list>
using namespace Interpreter;
extern int yylex();
extern int yyparse();
extern FILE* yyin;
extern int yylineno;
void yyerror(const char* s);
int yydebug=1;
Interpreter::Statements* root;
%}
%code requires
{
#include "interpreter.h"
#include <forward_list>
}
%union {
char* name;
char* op;
int val;
Interpreter::Id* id_node;
Interpreter::Val* val_node;
Interpreter::Statements* statements_node;
Interpreter::Statement* statement_node;
Interpreter::Expression* expression_node;
std::forward_list<Interpreter::ProcedureCallArgument*>* funccallargument_node;
std::forward_list<Interpreter::Id*>* funcdefargument_node;
}
%token<val> T_INT
%token<name> T_ID
%token<op> T_PLUS T_MINUS T_MULTIPLY T_DIVIDE
%token<op> T_RELOP
%token T_DEF
%token T_NEWLINE
%left '+' '-'
%left '*' '/'
%nonassoc '<' '>' T_LE T_GE T_EQ T_NE
%type<id_node> id
%type<expression_node> number
%type<statements_node> stmts
%type<statement_node> stmt print assn cond funcdef funccall
%type<expression_node> expr factor
%type<funcdefargument_node> defargs optdefargs
%type<funccallargument_node> callargs optcallargs
%start program
%%
program : stmts { root = $1; $1->Execute(); }
stmts : stmt T_NEWLINE stmts { $$ = new Statements($1,$3); }
| stmt { $$ = new Statements($1,nullptr); }
| T_NEWLINE stmts { $$ = $2; }
| /* e */ { $$ = nullptr; }
;
stmt : assn
| cond
| print
| funcdef
| funccall
;
print : '&' expr { $$ = new Print($2); }
;
assn : '{' expr '}' '|' ':' id { $$ = new Assignment($6,$2); }
;
cond : '(' expr ')' '?' '?' '(' stmts ')' { $$ = new Conditional($2,$7); }
;
expr : expr '<' expr { $$ = new ExpressionNode($1,$3, new Op("<")); }
| expr '>' expr { $$ = new ExpressionNode($1,$3, new Op(">")); }
| expr T_EQ expr { $$ = new ExpressionNode($1,$3, new Op("==")); }
| expr T_NE expr { $$ = new ExpressionNode($1,$3, new Op("!=")); }
| expr T_LE expr { $$ = new ExpressionNode($1,$3, new Op("<=")); }
| expr T_GE expr { $$ = new ExpressionNode($1,$3, new Op(">=")); }
| expr '+' expr { $$ = new ExpressionNode($1,$3, new Op("+")); }
| expr '-' expr { $$ = new ExpressionNode($1,$3, new Op("-")); }
| expr '*' expr { $$ = new ExpressionNode($1,$3, new Op("*")); }
| expr '/' expr { $$ = new ExpressionNode($1,$3, new Op("/")); }
| factor
;
factor : '(' expr ')' { $$ = $2; }
| number { $$ = $1; }
| id { $$ = $1; }
;
funcdef : T_DEF id '(' optdefargs ')' '=' '{' stmts '}' { $$ = new ProcedureDeclaration($2,$4,$8); }
;
funccall : id '(' optcallargs ')' { $$ = new ProcedureCall($1,$3); }
;
optdefargs : defargs
| { $$ = new std::forward_list<Id*>(); }
;
optcallargs : callargs
| { $$ = new std::forward_list<ProcedureCallArgument*>(); }
;
defargs : id { $$ = new std::forward_list<Id*>(); $$->push_front($1); }
| id ',' defargs { $$->push_front($1); }
;
callargs : expr { $$ = new std::forward_list<ProcedureCallArgument*>(); $$->push_front(new ProcedureCallArgument($1)); }
| expr ',' callargs { $$->push_front(new ProcedureCallArgument($1)); }
;
id : T_ID { $$ = new Id(strdup($1)); }
number : T_INT { $$ = new Val($1); }
%%
extern int yy_flex_debug;
int main() {
yy_flex_debug = 0;
yyparse();
return 0;
}
void yyerror(const char* s) {
fprintf(stderr, "syntax error: %s: line %d\n", s, yylineno);
exit(1);
}