-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
4,694 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# | ||
# UFRGS - Compiladores B - Marcelo Johann - 2017/1 - Etapa 4 | ||
# | ||
# Makefile for single compiler call | ||
# All source files must be included from code embedded in scanner.l | ||
# In our case, you probably need #include "hash.c" at the beginning | ||
# and #include "main.c" in the last part of the scanner.l | ||
# | ||
|
||
etapa4: y.tab.c lex.yy.c | ||
gcc -o etapa4 lex.yy.c ast.c hash.c y.tab.c semantic.c #main.c chamada em scanner.l | ||
|
||
lex.yy.c: scanner.l | ||
lex -d scanner.l | ||
|
||
y.tab.c: parser.y | ||
yacc -d parser.y | ||
|
||
tgz: clean | ||
tar cvzf etapa4.tgz . | ||
|
||
clean: | ||
rm -rf *o lex.yy.* y.tab.* etapa4 etapa4.tgz | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-> Alterar a tabela hash | ||
-> criar o arquivo semantic.h e .c com as regras definidas na especifica��o: | ||
que s�o.. | ||
|
||
Defini��es Sem�nticas: | ||
� H� tr�s tipos diferentes de identificadores: | ||
escalares, vetores, fun��es | ||
� H� cinco tipos de dados para declara��es: | ||
byte, short, long, float, double | ||
� H� tr�s tipos de literais: | ||
inteiros, caracteres, strings | ||
|
||
� Literais string s� devem ser usados no comando print. Literais inteiros e em ponto flutuante s�o intercambi�veis, e podem aparecer em quaisquer express�es aritm�ticas, e serem atribu�dos a dados num�ricos (byte, short, long, float, double). N�o � necess�rio verificar o tamanho (long ou short) nem a precis�o (dados inteiros ou em ponto flutuante) nas express�es e atribui��es - isso n�o deve gerar nem erro nem warning. No acesso a vetor, entretanto, � necess�rio garantir que o �ndice seja um valor inteiro. Express�es que potencialmente resultem em valores flutuantes, n�o podem ser usadas como �ndice de vetor, e devem gerar erro sem�ntico. | ||
|
||
� Os tipos de dados inteiros e flutuante podem ser usados e convertidos livremente em express�es aritm�ticas, ou seja, s�o compat�veis, podendo ser tamb�m usados como argumentos para par�metros definidos assim. O resultado de cada opera��o sempre ter� o tipo de dado mais amplo e preciso. Uma soma entre um long e um float, retorna, portanto, um flutuante. | ||
|
||
� Nas express�es aritm�ticas, existe portanto apenas uma verifica��o necess�ria sobre os tipos de dados, que � entre tipos num�ricos versus booleanos, que apenas s�o gerados pelos operadores booleanos | ||
|
||
� Existe a possibilidade da identifica��o ser feita pelo tipo do nodo filho da �rvore (tipo do operador) ou pelo tipo de dado (dataType) do identificador, se o nodo filho � um identificador (AST_SYMBOL). As opera��es booleanas podem ser verificadas somente no nodo local da �rvore, em rela��o aos seus filhos. | ||
|
||
� Para garantir que os �ndices de vetores sejam valores inteiros e n�o flutuantes, entretanto, � necess�rio que a �rvore seja previamente percorrida das folhas at� a raiz, anotando o tipo de dado correto nos nodos. Isso porque um operador + pode resultar tanto em um escalar inteiro como em um escalar flutuante, e isso s� � poss�vel descobrir e verificar recursivamente � partir das folhas. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
Etapa 4 do trabalho de Compiladores (2017/1) | ||
Professor: Marcelo Johann | ||
Grupo: | ||
Delton Vaz (00229779) (nick: ~davaz) | ||
Flávio Keglevich (00229724) | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "ast.h" | ||
|
||
AST_NODE* ast_insert(int type, HASH_NODE *symbol, AST_NODE* c0, AST_NODE* c1, AST_NODE* c2, AST_NODE* c3) | ||
{ | ||
AST_NODE *astNode; | ||
int i; | ||
|
||
astNode = calloc(1, sizeof(AST_NODE)); | ||
astNode->type = type; | ||
astNode->symbol = symbol; | ||
|
||
astNode->children[0] = c0; | ||
astNode->children[1] = c1; | ||
astNode->children[2] = c2; | ||
astNode->children[3] = c3; | ||
|
||
return astNode; | ||
} | ||
|
||
void print_op_node(AST_NODE *node, char* op_str) | ||
{ | ||
ast_print(node->children[0]); | ||
fprintf(OUT_FILE, "%s", op_str); | ||
ast_print(node->children[1]); | ||
} | ||
|
||
void print_str(char* str) | ||
{ | ||
fprintf(OUT_FILE, "%s", str); | ||
} | ||
|
||
void ast_print_tree(AST_NODE *root) | ||
{ | ||
ast_print(root); | ||
} | ||
|
||
void ast_print(AST_NODE *node) | ||
{ | ||
int i; | ||
|
||
if (node == NULL) return; | ||
|
||
switch(node->type) | ||
{ | ||
case AST_PARENTHESES: print_str("("); ast_print(node->children[0]); print_str(")"); break; | ||
|
||
case AST_ADD: | ||
case AST_SUB: | ||
case AST_MUL: | ||
case AST_DIV: | ||
case AST_GT: | ||
case AST_LT: | ||
ast_print(node->children[0]); | ||
fprintf(OUT_FILE, "%c ", node->type); | ||
ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_LE: print_op_node(node, "<= "); break; | ||
case AST_GE: print_op_node(node, ">= "); break; | ||
case AST_EQ: print_op_node(node, "== "); break; | ||
case AST_NE: print_op_node(node, "!= "); break; | ||
case AST_AND: print_op_node(node, "&& "); break; | ||
case AST_OR: print_op_node(node, "|| "); break; | ||
|
||
case AST_NOT: print_str("! "); ast_print(node->children[0]); break; | ||
|
||
case AST_LITERAL: | ||
case AST_ID: | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); break; | ||
|
||
case AST_ID_VECTOR: | ||
fprintf(OUT_FILE, "%s", node->symbol->text); | ||
print_str("["); ast_print(node->children[0]); print_str("]"); | ||
break; | ||
|
||
case AST_ID_CALL: | ||
fprintf(OUT_FILE, "%s", node->symbol->text); | ||
print_str("("); ast_print(node->children[0]); print_str(")"); | ||
break; | ||
|
||
case AST_PARAM_LIST: | ||
ast_print(node->children[0]); print_str(", "); ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_WHEN: | ||
case AST_WHEN_ELSE: | ||
print_str("when ( "); ast_print(node->children[0]); | ||
print_str(") then "); ast_print(node->children[1]); | ||
if (node->children[2] != NULL) { | ||
print_str("else "); ast_print(node->children[2]); | ||
} | ||
break; | ||
|
||
case AST_WHILE: | ||
print_str("while ( "); ast_print(node->children[0]); | ||
print_str(") "); ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_FOR: | ||
print_str("for ( "); fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
print_str("= "); ast_print(node->children[0]); | ||
print_str("to "); ast_print(node->children[1]); | ||
print_str(") "); ast_print(node->children[2]); | ||
break; | ||
|
||
case AST_ATRIB: | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
print_str("= "); ast_print(node->children[0]); | ||
break; | ||
|
||
case AST_ATRIB_VECTOR: | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
print_str("# "); ast_print(node->children[0]); | ||
print_str("= "); ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_VEC_INIT: | ||
case AST_PROGRAM: | ||
case AST_PRINT_ARGS: | ||
ast_print(node->children[0]); | ||
ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_VAR_DEC: | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
print_str(": "); ast_print(node->children[0]); | ||
print_str(" "); ast_print(node->children[1]); | ||
print_str(";\n"); | ||
break; | ||
|
||
case AST_VEC_DEC: | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
print_str(": "); ast_print(node->children[0]); | ||
print_str("[ "); ast_print(node->children[1]); | ||
print_str("] "); ast_print(node->children[2]); | ||
print_str(";\n"); | ||
break; | ||
|
||
case AST_FUNC_DEC: | ||
ast_print(node->children[0]); print_str(" "); | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
print_str("( "); ast_print(node->children[1]); | ||
print_str(") "); ast_print(node->children[2]); | ||
print_str(";\n"); | ||
break; | ||
|
||
case AST_BYTE: print_str("byte "); break; | ||
case AST_SHORT: print_str("short "); break; | ||
case AST_LONG: print_str("long "); break; | ||
case AST_FLOAT: print_str("float "); break; | ||
case AST_DOUBLE: print_str("double "); break; | ||
case AST_BOOL: print_str("bool "); break; | ||
|
||
case AST_ARGS_LIST: | ||
ast_print(node->children[0]); | ||
print_str(", "); | ||
ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_DEC_ARGS: | ||
ast_print(node->children[0]); | ||
print_str(" "); | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
break; | ||
|
||
case AST_COMMAND_LIST: | ||
print_str("{\n"); | ||
ast_print(node->children[0]); | ||
print_str("}"); | ||
break; | ||
|
||
case AST_READ: | ||
print_str("read "); | ||
fprintf(OUT_FILE, "%s ", node->symbol->text); | ||
break; | ||
|
||
case AST_PRINT: | ||
print_str("print "); ast_print(node->children[0]); | ||
print_str(" "); ast_print(node->children[1]); | ||
break; | ||
|
||
case AST_RETURN: print_str("return "); ast_print(node->children[0]); break; | ||
|
||
case AST_COMMANDS: | ||
ast_print(node->children[0]); | ||
print_str(";\n"); | ||
ast_print(node->children[1]); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Etapa 4 do trabalho de Compiladores (2017/1) | ||
Professor: Marcelo Johann | ||
Grupo: | ||
Delton Vaz (00229779) (nick: ~davaz) | ||
Flávio Keglevich (00229724) | ||
*/ | ||
|
||
#ifndef AST_H_ | ||
#define AST_H_ | ||
|
||
#include "hash.h" | ||
|
||
FILE* OUT_FILE; | ||
|
||
#define NUM_CHILDREN 4 | ||
|
||
#define AST_PARENTHESES 256 | ||
#define AST_ADD '+' | ||
#define AST_SUB '-' | ||
#define AST_MUL '*' | ||
#define AST_DIV '/' | ||
#define AST_GT '>' | ||
#define AST_LT '<' | ||
#define AST_LE 260 | ||
#define AST_GE 261 | ||
#define AST_EQ 262 | ||
#define AST_NE 263 | ||
#define AST_AND 264 | ||
#define AST_OR 265 | ||
#define AST_NOT 266 | ||
|
||
//3 tipos diferentes de identificadores | ||
#define AST_ID 270 | ||
#define AST_ID_VECTOR 271 | ||
#define AST_ID_CALL 272 | ||
//-- | ||
|
||
//Há três tipos de literais: inteiros, caracteres, strings | ||
#define AST_LITERAL 273 | ||
//-- | ||
|
||
#define AST_PARAM_LIST 280 | ||
|
||
#define AST_WHEN 290 | ||
#define AST_WHEN_ELSE 291 | ||
#define AST_WHILE 292 | ||
#define AST_FOR 293 | ||
|
||
#define AST_ATRIB 300 | ||
#define AST_ATRIB_VECTOR 301 | ||
|
||
#define AST_PROGRAM 400 | ||
|
||
#define AST_VAR_DEC 500 //Variavel declaração | ||
#define AST_VEC_DEC 501 //Vetor declaração | ||
#define AST_VEC_INIT 502 //Vetor inicialização | ||
#define AST_FUNC_DEC 503 //Função declaração | ||
|
||
//5 tipos de dados para declarações | ||
// SEIS TIPOS DE DADOS PARA DECLARAÇÕES MWHAHAHA (droga D:) | ||
#define AST_BYTE 600 | ||
#define AST_SHORT 601 | ||
#define AST_LONG 602 | ||
#define AST_FLOAT 603 | ||
#define AST_DOUBLE 604 | ||
#define AST_BOOL 605 | ||
//-- | ||
|
||
#define AST_PRINT_ARGS 700 | ||
#define AST_ARGS_LIST 701 | ||
#define AST_DEC_ARGS 702 | ||
|
||
#define AST_COMMAND_LIST 800 | ||
#define AST_READ 801 | ||
#define AST_PRINT 802 | ||
#define AST_RETURN 803 | ||
#define AST_COMMANDS 804 | ||
|
||
typedef struct ast_node { | ||
int type; | ||
HASH_NODE *symbol; | ||
struct ast_node *children[NUM_CHILDREN]; | ||
} AST_NODE; | ||
|
||
AST_NODE* ast_insert(int type, HASH_NODE *symbol, AST_NODE* c0, AST_NODE* c1, AST_NODE* c2, AST_NODE* c3); | ||
void ast_print(AST_NODE *node); | ||
void ast_print_tree(AST_NODE *root); | ||
|
||
#endif |
Oops, something went wrong.