Skip to content

Commit

Permalink
Merge pull request #36 from arthurgonze/semantico
Browse files Browse the repository at this point in the history
Análise semântica do código
  • Loading branch information
arthurgonze committed Dec 3, 2019
2 parents c22c7d8 + da49f5a commit 407caf3
Show file tree
Hide file tree
Showing 59 changed files with 11,225 additions and 1,692 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.5)
project(Compilador_2019_3)

set(CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

set(CMAKE_CODELITE_USE_TARGETS ON)

add_executable(Compilador_2019_3 main.cpp token.h analyzer.cpp analyzer.h symboltable.cpp symboltable.h parser.cpp parser.h ast.cpp ast.h visitor.cpp visitor.h)
add_executable(Compilador_2019_3 main.cpp token.h analyzer.cpp analyzer.h symboltable.cpp symboltable.h parser.cpp parser.h ast.cpp ast.h visitor.cpp visitor.h semantic.cpp semantic.h symbol.cpp symbol.h)
160 changes: 85 additions & 75 deletions analyzer.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by souzajbr on 10/09/2019.
//

#include "ctype.h"
#include "stdbool.h"
#include "stdio.h"
Expand All @@ -18,44 +14,44 @@ int currentLine = 1;
int currentColumn = 0;

char *lexemeBuffer = nullptr;
char* lastLexemeFound = nullptr;
char *lastLexemeFound = nullptr;
int lexemeLength = 0;
int lexemeBufferSize = 0;

char const * tokens [] = {"ENDOFILE","LT","LE","EQ","NE","GT","GE","IF","BOOL","ELSE","ID",
"INT","FLOAT","COMMA","LPARENT","RPARENT","ASSIGN","SEMICOLON",
"WHILE","LBRACKET","RBRACKET","SWITCH","BREAK","RETURN","PRINT",
"READLN","THROW","TRY","CATCH","CASE","LITERAL","TRUE","FALSE",
"ADDRESS","STAR","DOT","LBRACE","RBRACE","NOT","CHAR","QUOTE",
"SIMPLEQUOTE","BACKSLASH","COLON","PLUS","MINUS","PIPE","SLASH",
"PERCENT","AND","OR","POINTER","TYPEDEF","STRUCT","NUMINT",
"NUMFLOAT","LITERALCHAR"};
char const *tokens[] = {"ENDOFILE", "LT", "LE", "EQ", "NE", "GT", "GE", "IF", "BOOL", "ELSE", "ID",
"INT", "FLOAT", "COMMA", "LPARENT", "RPARENT", "ASSIGN", "SEMICOLON",
"WHILE", "LBRACKET", "RBRACKET", "SWITCH", "BREAK", "RETURN", "PRINT",
"READLN", "THROW", "TRY", "CATCH", "CASE", "LITERAL", "TRUE", "FALSE",
"ADDRESS", "STAR", "DOT", "LBRACE", "RBRACE", "NOT", "CHAR", "QUOTE",
"SIMPLEQUOTE", "BACKSLASH", "COLON", "PLUS", "MINUS", "PIPE", "SLASH",
"PERCENT", "AND", "OR", "POINTER", "TYPEDEF", "STRUCT", "NUMINT",
"NUMFLOAT", "LITERALCHAR"};

ReservedWordsTable reservedWordsTable;
LiteralsTable literalsTable;
IdentifiersTable identifiersTable;
ReservedWordsTable reservedWordsUsedTable;
ReservedWordsTable reservedWordsTable;
LiteralsTable literalsTable;
IdsTable identifiersTable;
NumIntTable numIntTable;
NumFloatTable numFloatTable;

FILE* filePointer;
FILE *filePointer;

LiteralsTable get_literals_table() {
return literalsTable;
}

IdentifiersTable get_identifiers_table() {
IdsTable get_identifiers_table() {
return identifiersTable;
}

ReservedWordsTable get_reserved_words_table() {
return reservedWordsUsedTable;
int lexical_analyzer_getLine() {
return currentLine;
}

/**
* Set the currentInput to the next char in input stream,
* updating the lexeme buffer with the new one and handling the column count
*/
void get_next_char() {

if (lexemeLength == lexemeBufferSize) {
lexemeBufferSize += BUFFER_SIZE;
lexemeBuffer = (char *) realloc(lexemeBuffer, lexemeBufferSize);
Expand All @@ -66,7 +62,6 @@ void get_next_char() {
static int n = 0;
/* Buffer is empty */
if (n == 0) {
;
n = fgets(buf, IO_BUFFER_SIZE, filePointer) != nullptr ? strlen(buf) : 0;
bufp = buf;
}
Expand All @@ -81,7 +76,6 @@ void get_next_char() {
* Clear the dirty entries from lexeme buffer
*/
void clear_lexeme() {

memset(lexemeBuffer, 0, lexemeBufferSize);

lexemeBuffer[0] = (char) currentInput;
Expand Down Expand Up @@ -117,6 +111,7 @@ void remove_last_char_from_lexeme() {
* @param token
* @return found token and lexeme
*/
//int found_token_and_restart(int token)
int found_token_and_restart(int token) {
remove_last_char_from_lexeme();

Expand Down Expand Up @@ -146,13 +141,12 @@ int found_token_and_get_next_input(int token) {
int found_token_and_check_for_reserved_word() {
remove_last_char_from_lexeme();

int token = reservedWordsTable.cSearch(lexemeBuffer);
// int token = reservedWordsTable.cSearch(lexemeBuffer);
ReservedTokenSymbol *token = reservedWordsTable.cSearch(lexemeBuffer);

if(token >= 0) {
reservedWordsUsedTable.cInsert(token, lexemeBuffer);
return found_token_and_restart(token);
}
else {
if (token != nullptr) {
return found_token_and_restart(token->getTokenID());
} else {
identifiersTable.cInsert(lexemeBuffer);
return found_token_and_restart(ID);
}
Expand All @@ -163,17 +157,28 @@ int found_token_and_check_for_reserved_word() {
* @param token
* @return found token
*/
//int found_literal_and_restart(int token)
int found_literal_and_restart(int token) {
remove_last_char_from_lexeme(); //remove the char from next token
literalsTable.cInsert(lexemeBuffer);
return found_token_and_restart(token);
}

int found_numInt_and_restart(int token) {
numIntTable.cInsert(lexemeBuffer);
return found_token_and_restart(token);
}

int found_numFloat_and_restart(int token) {
numFloatTable.cInsert(lexemeBuffer);
return found_token_and_restart(token);
}

/**
* [MANDATORY] Initialize the analyzer module, create the symtable for reserved words and
* prepare the input
*/
void lexical_analyzer_init(FILE* fp) {
void lexical_analyzer_init(FILE *fp) {

filePointer = fp;

Expand Down Expand Up @@ -213,21 +218,22 @@ bool is_digit(char c) {
* Handles the fail state
* @param reason
*/
void fail(char const *reason) {
fprintf(stderr, "[LEXICAL ERROR] %s at %d:%d\n", reason, currentLine, currentColumn - 1);
void fail(const char *reason) {
fprintf(stderr, "[LEXICAL ERROR] %s at %d:%d\n", reason, currentLine, currentColumn - 1);
clear_lexeme();
}

void handle_next_line() {
currentLine++;
currentColumn = 1;
}

/**
* Terminates the analyzer instance
*/
void lexical_analyzer_dispose() {
free(lexemeBuffer);
free(lastLexemeFound);
free(lastLexemeFound);
}

/**
Expand All @@ -238,20 +244,19 @@ void lexical_analyzer_dispose() {
int lexical_analyzer_next_token() {

while (true) {

switch (currentState) {

case INITIAL_STATE:
if (currentInput == '\n')
if (currentInput == '\n') {
handle_next_line();
}
if (isspace(currentInput)) {
get_next_char_and_go_to(INITIAL_STATE);
clear_lexeme();
} else if (is_letter(currentInput)) {
get_next_char_and_go_to(13);
} else if (is_digit(currentInput))
} else if (is_digit(currentInput)) {
get_next_char_and_go_to(19);
else
} else
switch (currentInput) {
case '<':
get_next_char_and_go_to(1);
Expand Down Expand Up @@ -384,16 +389,18 @@ int lexical_analyzer_next_token() {
case 12:
return found_token_and_restart(NOT); //found NOT
case 13:
if (is_letter(currentInput) || is_digit(currentInput))
if (is_letter(currentInput) || is_digit(currentInput)) {
get_next_char_and_go_to(13);
else
} else {
go_to_state(15);
}
break;
case 14:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(55);
else
} else {
go_to_state(53);
}
break;
case 15:
return found_token_and_check_for_reserved_word();
Expand All @@ -406,71 +413,75 @@ int lexical_analyzer_next_token() {
go_to_state(INITIAL_STATE);
break;
case 19:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(19);
else if (currentInput == '.')
} else if (currentInput == '.') {
get_next_char_and_go_to(25);
else if (currentInput == 'e' || currentInput == 'E')
} else if (currentInput == 'e' || currentInput == 'E') {
get_next_char_and_go_to(20);
else if (is_letter(currentInput))
} else if (is_letter(currentInput)) {
go_to_state(94);
// else if (currentInput == ',')
// go_to_state(95);
else
} else {
go_to_state(24);
}
break;
case 20:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(22);
else if (currentInput == '+' || currentInput == '-')
} else if (currentInput == '+' || currentInput == '-') {
get_next_char_and_go_to(21);
else
} else {
go_to_state(93);
}
break;
case 21:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(22);
else
} else {
go_to_state(92);
}
break;
case 22:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(22);
else {
} else {
go_to_state(23);
}
break;
case 23:
return found_token_and_restart(NUMFLOAT); //found NUMFLOAT
return found_numFloat_and_restart(NUMFLOAT); //found NUMFLOAT
case 24:
return found_token_and_restart(NUMINT); //found NUMINT
return found_numInt_and_restart(NUMINT); //found NUMINT
case 25:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(26);
else if (is_letter(currentInput))
} else if (is_letter(currentInput)) {
go_to_state(91);
else
} else {
go_to_state(23);
}
break;
case 26:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(26);
else if (currentInput == 'E' || currentInput == 'e')
} else if (currentInput == 'E' || currentInput == 'e') {
get_next_char_and_go_to(20);
else if (is_letter(currentInput))
} else if (is_letter(currentInput)) {
go_to_state(91);
else
} else {
go_to_state(23);
}
break;
case 27:
return found_token_and_restart(PLUS); //found PLUS
case 28:
return found_token_and_restart(RPARENT); //found RPARENT
case 29:
if (currentInput == '>')
if (currentInput == '>') {
get_next_char_and_go_to(56);
else
} else {
go_to_state(57);
}
break;
case 30:
return found_token_and_restart(LPARENT); //found LPARENT
Expand Down Expand Up @@ -603,11 +614,11 @@ int lexical_analyzer_next_token() {
case 54:
return found_token_and_restart(ENDOFFILE); //found EOF
case 55:
if (is_digit(currentInput))
if (is_digit(currentInput)) {
get_next_char_and_go_to(55);
else if (currentInput == 'e' || currentInput == 'E')
} else if (currentInput == 'e' || currentInput == 'E') {
get_next_char_and_go_to(20);
else if (is_letter(currentInput)) {
} else if (is_letter(currentInput)) {
go_to_state(18);
} else
go_to_state(23);
Expand Down Expand Up @@ -652,17 +663,16 @@ int lexical_analyzer_next_token() {
}
}

char* lexical_analyzer_last_lexeme()
{
return lastLexemeFound;
char *lexical_analyzer_last_lexeme() {
return lastLexemeFound;
}

/**
* Convert token numerical IDs to a textual represent
* @param id Kind of token
* @return Textual value of token
*/
char const* token_id_to_name(int id) {
char const *token_id_to_name(int id) {

return tokens[id];
}
Loading

0 comments on commit 407caf3

Please sign in to comment.