-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
172 lines (140 loc) · 7.15 KB
/
parser.y
File metadata and controls
172 lines (140 loc) · 7.15 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
%{
#include "hash.h"
#include "ast.h"
#include <stdio.h>
#include <stdlib.h>
int getLineNumber();
struct astNode* root = NULL;
%}
%union
{
struct hashNode* symbol;
struct astNode* ast;
}
%token KW_CARA
%token KW_INTE
%token KW_REAL
%token KW_SE
%token KW_ENTAUM
%token KW_SENAUM
%token KW_ENQUANTO
%token KW_ENTRADA
%token KW_ESCREVA
%token KW_RETORNE
%token OPERATOR_LE
%token OPERATOR_GE
%token OPERATOR_EQ
%token OPERATOR_DIF
%token<symbol> TK_IDENTIFIER
%token<symbol> LIT_INTEIRO
%token<symbol> LIT_FLOAT
%token<symbol> LIT_CHAR
%token<symbol> LIT_STRING
%token TOKEN_ERROR
%type<ast> program
%type<ast> decl
%type<ast> tail
%type<ast> dec
%type<ast> litint
%type<ast> type
%type<ast> lit
%type<ast> vecinitl
%type<ast> paraml
%type<ast> param
%type<ast> block
%type<ast> cmdl
%type<ast> cmd
%type<ast> expr
%type<ast> exprl
%type<ast> printl
%type<ast> argl
%type<ast> arg
%left '&' '|'
%left '<' '>' OPERATOR_LE OPERATOR_GE OPERATOR_EQ OPERATOR_DIF
%left '+' '-'
%left '*' '/'
%left '~'
%%
program: { $$ = 0; }
| decl { $$ = $1; root = $$; astPrint(root, 0); }
;
decl: dec tail { $$ = astCreate(AST_DECL, 0, $1, $2, 0, 0); }
;
tail: dec tail { $$ = astCreate(AST_TAIL, 0, $1, $2, 0, 0); }
| { $$ = 0; }
;
dec: type TK_IDENTIFIER '=' lit ';' { $$ = astCreate(AST_DEC_VAR, $2, $1, $4, 0, 0); }
| type TK_IDENTIFIER '[' litint ']' vecinitl ';' { $$ = astCreate(AST_DEC_VEC, $2, $1, $4, $6, 0); }
| type TK_IDENTIFIER '(' paraml ')' block { $$ = astCreate(AST_DEC_FUN, $2, $1, $4, $6, 0); }
;
litint: LIT_INTEIRO { $$ = astCreate(AST_LITSYMBOL, $1, 0, 0, 0, 0); }
;
type: KW_CARA { $$ = astCreate(AST_CARA, 0, 0, 0, 0, 0); }
| KW_INTE { $$ = astCreate(AST_INTE, 0, 0, 0, 0, 0); }
| KW_REAL { $$ = astCreate(AST_REAL, 0, 0, 0, 0, 0); }
;
lit: LIT_CHAR { $$ = astCreate(AST_SYMBOL, $1, 0, 0, 0, 0); }
| LIT_FLOAT { $$ = astCreate(AST_SYMBOL, $1, 0, 0, 0, 0); }
| LIT_INTEIRO { $$ = astCreate(AST_SYMBOL, $1, 0, 0, 0, 0); }
| LIT_STRING { $$ = astCreate(AST_SYMBOL, $1, 0, 0, 0, 0); }
;
vecinitl: lit vecinitl { $$ = astCreate(AST_VECINITL, 0, $1, $2, 0, 0); }
| { $$ = 0; }
;
paraml: param paraml { $$ = astCreate(AST_PARAML, 0, $1, $2, 0, 0); }
| { $$ = 0; }
;
param: type TK_IDENTIFIER { $$ = astCreate(AST_PARAM, $2, $1, 0, 0, 0); }
;
block: '{' cmdl '}' { $$ = astCreate(AST_BLOCK, 0, $2, 0, 0, 0); }
;
cmdl: cmd ';' cmdl { $$ = astCreate(AST_CMDL, 0, $1, $3, 0, 0); }
| cmd { $$ = astCreate(AST_CMDL, 0, $1, 0, 0, 0); }
;
cmd: TK_IDENTIFIER '=' expr { $$ = astCreate(AST_ASSIGN, $1, $3, 0, 0, 0); }
| TK_IDENTIFIER '[' expr ']' '=' expr { $$ = astCreate(AST_VEC_ASSIGN, $1, $3, $6, 0, 0); }
| KW_ESCREVA printl { $$ = astCreate(AST_ESCREVA, 0, $2, 0, 0, 0); }
| KW_ENTAUM cmd KW_SE '(' expr ')' { $$ = astCreate(AST_SE, 0, $2, $5, 0, 0); }
| KW_ENTAUM cmd KW_SENAUM cmd KW_SE '(' expr ')' { $$ = astCreate(AST_SE_SENAUM, 0, $2, $4, $7, 0); }
| cmd KW_ENQUANTO '(' expr ')' { $$ = astCreate(AST_ENQUANTO, 0, $1, $4, 0, 0); }
| KW_RETORNE expr { $$ = astCreate(AST_RETORNE, 0, $2, 0, 0, 0); }
| block { $$ = $1; }
| { $$ = 0; }
;
expr: lit { $$ = $1; }
| TK_IDENTIFIER { $$ = astCreate(AST_SYMBOL, $1, 0, 0, 0, 0); }
| TK_IDENTIFIER '[' expr ']' { $$ = astCreate(AST_VEC_ELEM, $1, $3, 0, 0, 0); }
| expr '+' expr { $$ = astCreate(AST_ADD, 0, $1, $3, 0, 0); }
| expr '-' expr { $$ = astCreate(AST_SUB, 0, $1, $3, 0, 0); }
| expr '/' expr { $$ = astCreate(AST_DIV, 0, $1, $3, 0, 0); }
| expr '*' expr { $$ = astCreate(AST_MUL, 0, $1, $3, 0, 0); }
| expr '<' expr { $$ = astCreate(AST_LT, 0, $1, $3, 0, 0); }
| expr '>' expr { $$ = astCreate(AST_GT, 0, $1, $3, 0, 0); }
| expr OPERATOR_DIF expr { $$ = astCreate(AST_DIF, 0, $1, $3, 0, 0); }
| expr OPERATOR_EQ expr { $$ = astCreate(AST_EQ, 0, $1, $3, 0, 0); }
| expr OPERATOR_GE expr { $$ = astCreate(AST_GE, 0, $1, $3, 0, 0); }
| expr OPERATOR_LE expr { $$ = astCreate(AST_LE, 0, $1, $3, 0, 0); }
| expr '&' expr { $$ = astCreate(AST_AND, 0, $1, $3, 0, 0); }
| expr '|' expr { $$ = astCreate(AST_OR, 0, $1, $3, 0, 0); }
| '~' expr { $$ = astCreate(AST_NOT, 0, $2, 0, 0, 0); }
| '(' expr ')' { $$ = astCreate(AST_BRAC_EXPR, 0, $2, 0, 0, 0); }
| TK_IDENTIFIER '(' argl ')' { $$ = astCreate(AST_FUNC_CALL, $1, $3, 0, 0, 0); }
| KW_ENTRADA { $$ = astCreate(AST_ENTRADA, 0, 0, 0, 0, 0); }
;
exprl: expr exprl { $$ = astCreate(AST_EXPRL, 0, $1, $2, 0, 0); }
| { $$ = 0; }
;
printl: expr printl { $$ = astCreate(AST_PRINTL, 0, $1, $2, 0, 0); }
| { $$ = 0; }
;
argl: arg argl { $$ = astCreate(AST_ARGL, 0, $1, $2, 0, 0); }
| { $$ = 0; }
;
arg: lit { $$ = $1; }
| TK_IDENTIFIER { $$ = astCreate(AST_SYMBOL, $1, 0, 0, 0, 0); }
;
%%
int yyerror() {
fprintf(stderr, "Syntax error at line %d.\n", getLineNumber());
exit(3);
}