-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.y
More file actions
152 lines (118 loc) · 2.62 KB
/
expr.y
File metadata and controls
152 lines (118 loc) · 2.62 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
%{
#include <stdio.h>
#include <stdlib.h>
#include "lista.h"
#define YYSTYPE atributo
%}
%token ID LITERAL TNUM VOID INT STRING IF ELSE WHILE READ PRINT RETURN TADD TSUB TMUL TDIV RMEN RMAI RMENEQ RMAIEQ REQU RDIF LAND LOR LNOT TATRIB TPTOVRGL TVIRGULA TAPAR TFPAR TACHAVE TFCHAVE TFIM
%%
Linha :Programa TFIM {print(); exit(0);}
;
Programa: ListaFuncao BlocoPrincipal
| BlocoPrincipal
;
ListaFuncao: ListaFuncao Funcao
| Funcao
;
Funcao: TipoRetorno ID TAPAR DeclParametros TFPAR BlocoPrincipal
| TipoRetorno ID TAPAR TFPAR BlocoPrincipal
;
TipoRetorno: Tipo
| VOID
;
DeclParametros: DeclParametros TVIRGULA Parametro
| Parametro
;
Parametro: Tipo ID
;
BlocoPrincipal: TACHAVE Declaracoes ListaCmd TFCHAVE
| TACHAVE ListaCmd TFCHAVE
;
Declaracoes: Declaracoes Declaracao
| Declaracao
;
Declaracao: Tipo ListaId TPTOVRGL {tabela($1.tipo, $2.listaID);}
;
Tipo: INT {$$.tipo = T_INT;}
| STRING {$$.tipo = T_STRING;}
;
ListaId: ID {$$.listaID = iniciaLista($1.id);}
| ListaId TVIRGULA ID {$$.listaID = insereLista($$.listaID, $3.id);}
;
Bloco: TACHAVE ListaCmd TFCHAVE
;
ListaCmd: ListaCmd Comando
| Comando
;
Comando: CmdSe
| CmdEnquanto
| CmdAtrib
| CmdEscrita
| CmdLeitura
| ChamadaFuncao
| Retorno
;
Retorno: RETURN Expr TPTOVRGL
;
CmdSe: IF TAPAR ExprLogico TFPAR Bloco
| IF TAPAR ExprLogico TFPAR Bloco ELSE Bloco
;
CmdEnquanto: WHILE TAPAR ExprLogico TFPAR Bloco
;
CmdAtrib: ID TATRIB Expr TPTOVRGL
| ID TATRIB LITERAL TPTOVRGL
;
CmdEscrita: PRINT TAPAR Expr TFPAR TPTOVRGL
| PRINT TAPAR LITERAL TFPAR TPTOVRGL
;
CmdLeitura: READ TAPAR ID TFPAR TPTOVRGL
;
ChamadaFuncao: ID TAPAR ListaParametros TFPAR TPTOVRGL
| ID TAPAR TFPAR TPTOVRGL
;
ListaParametros: ListaParametros TVIRGULA Expr
| Expr
;
ExprLogico: ExprLogico LAND TermoLogico
| ExprLogico LOR TermoLogico
| TermoLogico
;
TermoLogico: LNOT TermoLogico
| FatorLogico
;
FatorLogico: TAPAR ExprLogico TFPAR
| ExprRelacional
;
ExprRelacional: Expr RMENEQ Expr
| Expr RMAIEQ Expr
| Expr RMEN Expr
| Expr RMAI Expr
| Expr REQU Expr
| Expr RDIF Expr
;
Expr: Expr TADD Termo
| Expr TSUB Termo
| Termo
;
Termo: Termo TMUL Fator
| Termo TDIV Fator
| Fator
;
Fator: TNUM
| TAPAR Expr TFPAR
| TSUB Fator
| TADD Fator
;
%%
#include "lex.yy.c"
int yyerror (char *str)
{
extern int yylineno;
extern char *yytext;
printf("%s <- antes\nyytex -> %s\n", str, yytext);
printf("linha: %d\n", yylineno);
}
int yywrap()
{
return 1;
}