-
Notifications
You must be signed in to change notification settings - Fork 6
/
ce.y
120 lines (106 loc) · 2.4 KB
/
ce.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
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
%{
#include <stdio.h>
#include "ast.h"
#define YYERROR_VERBOSE
void yyerror(const char *msg){printf("ERROR(PARSER): %s\n", msg);}
%}
%union {
int number;
char caracter;
char* string;
ast_t* tree;
}
%start program
%token SEPARADOR ATRIBUICAO L_PAREN R_PAREN L_COLCH R_COLCH
%token <string> SOMA SUBTRACAO DIVISAO MULTIPLICACAO
%token <string> TIPO VARIAVEL
%token <number> NUMERO
%type <tree> expressao termo fator attribution statement
%%
program:
function
;
function:
TIPO VARIAVEL L_PAREN R_PAREN L_COLCH statementList R_COLCH
;
statementList:
statementList statement
{
//printf("[ce.y] statementList\n");
statement_list_t* stmt = new_statement_list ($2);
add_statement ((program_t*) root, stmt);
}
| statement
{
//printf("[ce.y] statement\n");
statement_list_t* stmt = new_statement_list ($1);
add_statement ((program_t*) root, stmt);
}
;
statement:
attribution SEPARADOR
{
//printf("[ce.y] statement attribution\n");
$$ = $1;
}
;
attribution:
TIPO VARIAVEL ATRIBUICAO expressao
{
//printf("[ce.y] attribution. tipo: %s, var: %s\n", $1, $2);
identifier_t* id = new_identifier($2);
$$ = (ast_t*) new_attribution (id, $4);
}
;
expressao:
termo
{
//printf("[ce.y] termo\n");
$$ = $1;
}
| expressao SOMA termo
{
//printf("[ce.y] expressao som termo \n");
$$ = (ast_t*) new_expression ($1, (ast_t*) new_operation($2), $3);
}
| expressao SUBTRACAO termo
{
//printf("[ce.y] expressao som termo \n");
$$ = (ast_t*) new_expression ($1, (ast_t*) new_operation($2), $3);
}
;
termo:
fator
{
//printf("[ce.y] fator\n");
$$ = $1;
}
| termo MULTIPLICACAO fator
{
//printf("[ce.y] fator div fator \n");
$$ = (ast_t*) new_expression ($1, (ast_t*) new_operation($2), $3);
}
| termo DIVISAO fator
{
//printf("[ce.y] fator div fator \n");
$$ = (ast_t*) new_expression ($1, (ast_t*) new_operation($2), $3);
}
;
fator:
NUMERO
{
//printf("[ce.y] number: %d \n", $1);
$$ = (ast_t*) new_number($1);
}
| VARIAVEL
{
//printf("[ce.y] identifier: %s \n", $1);
$$ = (ast_t*) new_identifier($1);
}
;
%%
/*main(int argc, char* argv[])
{
while(1)
yyparse();
}*/