Skip to content
Snippets Groups Projects
Commit 635d3103 authored by Henrique Margotte's avatar Henrique Margotte
Browse files

procedimento adicionando na TS

parent da0ade69
Branches
No related tags found
No related merge requests found
File added
File added
This diff is collapsed.
This diff is collapsed.
/* A Bison parser, made by GNU Bison 3.8.2. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual,
especially those whose name start with YY_ or yy_. They are
private implementation details that can be changed or removed. */
#ifndef YY_YY_COMPILADOR_TAB_H_INCLUDED
# define YY_YY_COMPILADOR_TAB_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Token kinds. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
enum yytokentype
{
YYEMPTY = -2,
YYEOF = 0, /* "end of file" */
YYerror = 256, /* error */
YYUNDEF = 257, /* "invalid token" */
PROGRAM = 258, /* PROGRAM */
ABRE_PARENTESES = 259, /* ABRE_PARENTESES */
FECHA_PARENTESES = 260, /* FECHA_PARENTESES */
VIRGULA = 261, /* VIRGULA */
PONTO_E_VIRGULA = 262, /* PONTO_E_VIRGULA */
DOIS_PONTOS = 263, /* DOIS_PONTOS */
PONTO = 264, /* PONTO */
T_BEGIN = 265, /* T_BEGIN */
T_END = 266, /* T_END */
VAR = 267, /* VAR */
IDENT = 268, /* IDENT */
ATRIBUICAO = 269, /* ATRIBUICAO */
LABEL = 270, /* LABEL */
TYPE = 271, /* TYPE */
ARRAY = 272, /* ARRAY */
PROCEDURE = 273, /* PROCEDURE */
FUNCTION = 274, /* FUNCTION */
GOTO = 275, /* GOTO */
IF = 276, /* IF */
THEN = 277, /* THEN */
ELSE = 278, /* ELSE */
WHILE = 279, /* WHILE */
DO = 280, /* DO */
OR = 281, /* OR */
DIV = 282, /* DIV */
AND = 283, /* AND */
NOT = 284, /* NOT */
IGUAL = 285, /* IGUAL */
DIFERENTE = 286, /* DIFERENTE */
MENOR_IGUAL = 287, /* MENOR_IGUAL */
MAIOR_IGUAL = 288, /* MAIOR_IGUAL */
MENOR = 289, /* MENOR */
MAIOR = 290, /* MAIOR */
MAIS = 291, /* MAIS */
MENOS = 292, /* MENOS */
ASTERISCO = 293, /* ASTERISCO */
NUMERO = 294, /* NUMERO */
READ = 295, /* READ */
WRITE = 296, /* WRITE */
LOWER_THAN_ELSE = 297 /* LOWER_THAN_ELSE */
};
typedef enum yytokentype yytoken_kind_t;
#endif
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef int YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
int yyparse (void);
#endif /* !YY_YY_COMPILADOR_TAB_H_INCLUDED */
...@@ -102,19 +102,25 @@ tipo : IDENT ...@@ -102,19 +102,25 @@ tipo : IDENT
} else { } else {
yyerror("Tipo não reconhecido. Somente 'integer' ou 'boolean' são permitidos."); yyerror("Tipo não reconhecido. Somente 'integer' ou 'boolean' são permitidos.");
} }
mostra_ts(ts); // Mostra a tabela após a inserção //mostra_ts(ts); // Mostra a tabela após a inserção
} }
; ;
lista_id_var: lista_id_var VIRGULA IDENT lista_id_var: lista_id_var VIRGULA IDENT
{ /* insere �ltima vars na tabela de s�mbolos */ { /* insere �ltima vars na tabela de s�mbolos */
num_vars += 1; num_vars += 1;
insere_ts(token,VS,nivel_lexico,NSEI,desloc,ts); info_t info;
info.vs.tipo = NSEI;
info.vs.desloc = desloc;
insere_ts(token,VS,nivel_lexico,info,ts);
desloc += 1; desloc += 1;
} }
| IDENT { | IDENT {
num_vars += 1; num_vars += 1;
insere_ts(token,VS,nivel_lexico,NSEI,desloc,ts); info_t info;
info.vs.tipo = NSEI;
info.vs.desloc = desloc;
insere_ts(token,VS,nivel_lexico,info,ts);
desloc += 1; desloc += 1;
/* insere vars na tabela de s�mbolos */ /* insere vars na tabela de s�mbolos */
} }
...@@ -129,7 +135,9 @@ parte_declara_subrotinas: declaracao_procedimento ...@@ -129,7 +135,9 @@ parte_declara_subrotinas: declaracao_procedimento
; ;
// REGRA 12 // REGRA 12
declaracao_procedimento: ; declaracao_procedimento: PROCEDURE IDENT
|
;
comando_composto: T_BEGIN comandos T_END comando_composto: T_BEGIN comandos T_END
...@@ -150,10 +158,10 @@ comando_sem_rotulo: atribuicao ...@@ -150,10 +158,10 @@ comando_sem_rotulo: atribuicao
| comando_repetitivo | comando_repetitivo
| leitura | leitura
| escrita | escrita
| chamada_procedimento //| chamada_procedimento
; ;
chamada_procedimento: ; //chamada_procedimento: ;
leitura: READ ABRE_PARENTESES lista_leituras FECHA_PARENTESES leitura: READ ABRE_PARENTESES lista_leituras FECHA_PARENTESES
; ;
...@@ -166,7 +174,7 @@ simb_leitura: IDENT { ...@@ -166,7 +174,7 @@ simb_leitura: IDENT {
geraCodigo(NULL, "LEIT"); geraCodigo(NULL, "LEIT");
l_elem = busca(token,ts); l_elem = busca(token,ts);
char buffer[50]; char buffer[50];
sprintf(buffer,"ARMZ %d,%d",l_elem->nivel,l_elem->desloc); sprintf(buffer,"ARMZ %d,%d",l_elem->nivel,l_elem->info.vs.desloc);
geraCodigo(NULL,buffer); geraCodigo(NULL,buffer);
} }
; ;
...@@ -228,7 +236,7 @@ atribuicao: IDENT { ...@@ -228,7 +236,7 @@ atribuicao: IDENT {
yyerror("Variável de destino não declarada"); yyerror("Variável de destino não declarada");
} else { } else {
destino_nivel = l_elem->nivel; destino_nivel = l_elem->nivel;
destino_desloc = l_elem->desloc; destino_desloc = l_elem->info.vs.desloc;
} }
} ATRIBUICAO expressao_simples { } ATRIBUICAO expressao_simples {
// Gera código ARMZ para armazenar valor // Gera código ARMZ para armazenar valor
...@@ -245,7 +253,7 @@ fator: IDENT { ...@@ -245,7 +253,7 @@ fator: IDENT {
yyerror("Variável não declarada"); yyerror("Variável não declarada");
} else { } else {
char buffer[50]; char buffer[50];
sprintf(buffer, "CRVL %d,%d", l_elem->nivel, l_elem->desloc); sprintf(buffer, "CRVL %d,%d", l_elem->nivel, l_elem->info.vs.desloc);
geraCodigo(NULL, buffer); geraCodigo(NULL, buffer);
} }
} }
......
lex.yy.c 0 → 100644
This diff is collapsed.
File added
...@@ -8,7 +8,7 @@ void inicializa_ts(ts_t *ts){ ...@@ -8,7 +8,7 @@ void inicializa_ts(ts_t *ts){
ts->tam = 0; ts->tam = 0;
} }
void insere_ts(char *ident, int categ, int nivel, int tipo, int desloc, ts_t *ts){ void insere_ts(char *ident, int categ, int nivel, info_t info, ts_t *ts){
if(ts == NULL) return; if(ts == NULL) return;
simb_t *new = (simb_t *)malloc(sizeof(simb_t)); simb_t *new = (simb_t *)malloc(sizeof(simb_t));
...@@ -16,8 +16,19 @@ void insere_ts(char *ident, int categ, int nivel, int tipo, int desloc, ts_t *ts ...@@ -16,8 +16,19 @@ void insere_ts(char *ident, int categ, int nivel, int tipo, int desloc, ts_t *ts
strcpy(new->ident, ident); strcpy(new->ident, ident);
new->categ = categ; new->categ = categ;
new->nivel = nivel; new->nivel = nivel;
new->tipo = tipo; if (categ == VS) {
new->desloc = desloc; new->info.vs.tipo = info.vs.tipo;
new->info.vs.desloc = info.vs.desloc;
} else if (categ == PR) {
new->info.pr.rot = info.pr.rot;
new->info.pr.quant = info.pr.quant;
new->info.pr.param = (int **)malloc(info.pr.quant * sizeof(int *));
for (int i = 0; i < info.pr.quant; i++) {
new->info.pr.param[i] = (int *)malloc(2 * sizeof(int));
new->info.pr.param[i][0] = info.pr.param[i][0];
new->info.pr.param[i][1] = info.pr.param[i][1];
}
}
new->prox = ts->topo; new->prox = ts->topo;
ts->topo = new; ts->topo = new;
...@@ -59,12 +70,12 @@ void atualiza_tipo(int n, int tipo, ts_t *ts){ ...@@ -59,12 +70,12 @@ void atualiza_tipo(int n, int tipo, ts_t *ts){
simb_t *p = ts->topo; simb_t *p = ts->topo;
for (int i = 0; i < n; ++i){ for (int i = 0; i < n; ++i){
p->tipo = tipo; p->info.vs.tipo = tipo;
p = p->prox; p = p->prox;
} }
} }
void mostra_ts(ts_t *ts) { /*void mostra_ts(ts_t *ts) {
if (ts == NULL || ts->topo == NULL) { if (ts == NULL || ts->topo == NULL) {
printf("Tabela de Símbolos está vazia ou não inicializada.\n"); printf("Tabela de Símbolos está vazia ou não inicializada.\n");
return; return;
...@@ -80,7 +91,7 @@ void mostra_ts(ts_t *ts) { ...@@ -80,7 +91,7 @@ void mostra_ts(ts_t *ts) {
while (p != NULL) { while (p != NULL) {
// Identificar o tipo como Inteiro, Boolean ou Indefinido // Identificar o tipo como Inteiro, Boolean ou Indefinido
const char *tipoStr; const char *tipoStr;
switch (p->tipo) { switch (p->info.vs.tipo) {
case INTEIRO: case INTEIRO:
tipoStr = "Inteiro"; tipoStr = "Inteiro";
break; break;
...@@ -97,9 +108,9 @@ void mostra_ts(ts_t *ts) { ...@@ -97,9 +108,9 @@ void mostra_ts(ts_t *ts) {
// Imprimir a entrada // Imprimir a entrada
printf("| %-10s | %-8s | %-8d | %-10s | %-10d |\n", printf("| %-10s | %-8s | %-8d | %-10s | %-10d |\n",
p->ident, categStr, p->nivel, tipoStr, p->desloc); p->ident, categStr, p->nivel, tipoStr, p->info.vs.desloc);
p = p->prox; p = p->prox;
} }
printf("----------------------------------------------------------------------------------\n"); printf("----------------------------------------------------------------------------------\n");
} }*/
\ No newline at end of file \ No newline at end of file
#ifndef __TS__ #ifndef __TS__
#define __TS__ #define __TS__
typedef struct vs_t {
int tipo;
int desloc;
} vs_t;
typedef struct pr_t {
char *rot;
int quant;
int **param;
} pr_t;
typedef union info_t {
vs_t vs;
pr_t pr;
} info_t;
typedef struct simb_t { typedef struct simb_t {
char *ident; char *ident;
int categ; int categ;
int nivel; int nivel;
int tipo; info_t info;
int desloc; // Alterar para outros tipos além de VS
struct simb_t *prox; // Corrigido: use explicitamente struct simb_t struct simb_t *prox; // Corrigido: use explicitamente struct simb_t
} simb_t; } simb_t;
...@@ -26,7 +41,7 @@ typedef struct ts_t { ...@@ -26,7 +41,7 @@ typedef struct ts_t {
void inicializa_ts(ts_t *ts); void inicializa_ts(ts_t *ts);
void insere_ts(char *ident, int categ, int nivel, int tipo, int desloc, ts_t *ts); void insere_ts(char *ident, int categ, int nivel, info_t info, ts_t *ts);
simb_t *busca(char *ident, ts_t *ts); // Corrigido: retorna ponteiro para simb_t simb_t *busca(char *ident, ts_t *ts); // Corrigido: retorna ponteiro para simb_t
...@@ -34,6 +49,6 @@ void retira(int n, ts_t *ts); ...@@ -34,6 +49,6 @@ void retira(int n, ts_t *ts);
void atualiza_tipo(int n, int tipo, ts_t *ts); void atualiza_tipo(int n, int tipo, ts_t *ts);
void mostra_ts(ts_t *ts); //void mostra_ts(ts_t *ts);
#endif #endif
ts/ts.o 0 → 100644
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment