Skip to content

Commit

Permalink
another fix in DFA
Browse files Browse the repository at this point in the history
  • Loading branch information
computer committed Sep 26, 2014
1 parent 8df8dca commit e12c4a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 48 deletions.
80 changes: 34 additions & 46 deletions Compiler/lexical.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ struct{
TokenType separatorsCharacter[QTDE_SEPARATORS] = {SPACE, TAB, FEED_LINE, COMMA, DOT, LEFT_BRACKET, RIGHT_BRACKET, PLUS, MINUS,
MULTIPLICATION, BAR, MOD, LEFT_PARENTHESIS, RIGHT_PARENTHESIS, EQUAL};

StrangerType separatorsStrangers[] = {EXCLAMATION, OLD_GAME, DOLLAR, E_COMMERCIAL, COLON, SEMICOLON, INTERROGATION,
AT, CIRCUMFLEX, UNDERLINE, CRASE, RIGHT_KEY, LEFT_KEY, TIO_ACCENT, UP_BAR};
/*StrangerType separatorsStrangers[] = {EXCLAMATION, OLD_GAME, DOLLAR, E_COMMERCIAL, COLON, SEMICOLON, INTERROGATION,
AT, CIRCUMFLEX, UNDERLINE, CRASE, RIGHT_KEY, LEFT_KEY, TIO_ACCENT, UP_BAR};*/

//const int QTDE_SEPARATORS = 15; //exclusive for dfa

//TODO struct RelationalOperators

Expand All @@ -41,7 +40,7 @@ _Bool insToken(TokenType tokenType, char *tokenName){

Token *t;

if(t = (Token*)malloc(sizeof(Token))){
if( (t = (Token*)malloc(sizeof(Token))) ){

if(!tokens && !actualToken){ //are null, first element to insert

Expand All @@ -51,7 +50,7 @@ _Bool insToken(TokenType tokenType, char *tokenName){
if(tokenType != CONSTANT_NUMBER && tokenType != NUMBER)
tokens->attribute.name = tokenName;
else
tokens->attribute.num = tokenName; //TODO string to long long
tokens->attribute.num = atoll(tokenName); //TODO string to long long

tokens->next = NULL;
actualToken = tokens;
Expand All @@ -68,7 +67,7 @@ _Bool insToken(TokenType tokenType, char *tokenName){
if(tokenType != CONSTANT_NUMBER && tokenType != NUMBER)
actualToken->attribute.name = tokenName;
else
actualToken->attribute.num = tokenName; //TODO string to long long
actualToken->attribute.num = atoll(tokenName); //TODO string to long long

actualToken->next = NULL;

Expand Down Expand Up @@ -156,6 +155,7 @@ TokenType isSeparatorAll(int character){

}

/*
_Bool isStrangerSeparator(int character){
int i;
Expand All @@ -168,7 +168,7 @@ _Bool isStrangerSeparator(int character){
}
return FALSE;
}
}*/


int toLowercase(int c){
Expand Down Expand Up @@ -220,15 +220,15 @@ void lAnalyzer(const char *fileName){
if(verifyingFile(fileName)){

int c; //value of character in it
int qc = 0; //quantity of characters read
int qc = 0; //quantity of characters read nominal
int qcr = 0; //for CONSTANT_STRING, real quantity
int al = 1; //actual line
char *tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);

States s = S1;
_Bool outWhileDFA = FALSE; //exit DFA
_Bool goNextCharacter = TRUE;
_Bool go = TRUE;
_Bool strange = FALSE;

TokenType tt;

Expand All @@ -239,7 +239,7 @@ void lAnalyzer(const char *fileName){

//increase memory allocated for "tokenReaded"
if(qc > INITIAL_TOKEN_BUFFER-1){
tokenReaded = (char*)realloc(tokenReaded, qc + 1);
tokenReaded = (char*)realloc(tokenReaded, qc + 2);
}

switch(s){
Expand Down Expand Up @@ -451,13 +451,13 @@ void lAnalyzer(const char *fileName){
case S11: //err


while( isNumberDigit(c) || isLetter(c) ){
if( isNumberDigit(c) || isLetter(c) ){

tokenReaded[qc] = (char)c;
qc++;

if(qc > INITIAL_TOKEN_BUFFER-1){
tokenReaded = (char*)realloc(tokenReaded, qc + 1);
tokenReaded = (char*)realloc(tokenReaded, qc + 2);
}

if((c = fgetc(sf)) != EOF){
Expand All @@ -470,15 +470,17 @@ void lAnalyzer(const char *fileName){

}

}
}else{

tokenReaded[qc] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
s = S1;
tokenReaded[qc] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
s = S1;

tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
goNextCharacter = FALSE;
tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
goNextCharacter = FALSE;

}

break;

Expand Down Expand Up @@ -513,19 +515,16 @@ void lAnalyzer(const char *fileName){

case S10: //err's treatment

do{
go = TRUE;

if(qc > INITIAL_TOKEN_BUFFER-1){
tokenReaded = (char*)realloc(tokenReaded, qc + 1);
tokenReaded = (char*)realloc(tokenReaded, qc + 2);
}

if( c == SIMPLE_QUOTATION_MARK || c == TAB){
if( c == SIMPLE_QUOTATION_MARK || c == TAB){ //TODO I already go out my doubt about '', it is a err

tokenReaded[qc] = (char)c;
tokenReaded[qc+1] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
go = FALSE;
tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
s = S1;
Expand All @@ -534,7 +533,6 @@ void lAnalyzer(const char *fileName){

tokenReaded[qc] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
go = FALSE;
tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
al++;
Expand All @@ -551,14 +549,11 @@ void lAnalyzer(const char *fileName){

tokenReaded[qc] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
go = FALSE;
outWhileDFA = TRUE;

}
}

}while(go);

break;

case S6:
Expand Down Expand Up @@ -652,19 +647,20 @@ void lAnalyzer(const char *fileName){

if( (c = fgetc(sf)) != EOF ){

if(qc < 258){
if(qcr < 257){

if(isPrintableL(c)){
if(isPrintableL(c) && qcr < 256){

tokenReaded[qc] = (char)c;
qc++;
qcr++;

}else if(c == DOUBLE_QUOTATION_MARK){

tokenReaded[qc] = (char)c;
s = S17;

}else if(c == INVERTED_BAR){
}else if(c == INVERTED_BAR && qcr < 256){

s = S12;
tokenReaded[qc] = (char)c;
Expand Down Expand Up @@ -694,30 +690,27 @@ void lAnalyzer(const char *fileName){

case S13:

do{
go = TRUE;

if(qc > INITIAL_TOKEN_BUFFER-1){
tokenReaded = (char*)realloc(tokenReaded, qc + 1);
tokenReaded = (char*)realloc(tokenReaded, qc + 2);
}

if( c == DOUBLE_QUOTATION_MARK || c == TAB){

tokenReaded[qc] = (char)c;
tokenReaded[qc+1] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
go = FALSE;
tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
qcr = 0;
s = S1;

}else if(c == FEED_LINE){

tokenReaded[qc] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
go = FALSE;
tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
qcr = 0;
al++;
s = S1;

Expand All @@ -732,14 +725,11 @@ void lAnalyzer(const char *fileName){

tokenReaded[qc] = '\0';
printf("LINHA %d: %s\n", al, tokenReaded);
go = FALSE;
outWhileDFA = TRUE;

}
}

}while(go);

break;

case S17: //accepted CONSTANT_STRING
Expand All @@ -750,29 +740,26 @@ void lAnalyzer(const char *fileName){

tokenReaded = (char*)malloc(INITIAL_TOKEN_BUFFER);
qc = 0;
qcr = 0;

break;

case S12:

if( (c = fgetc(sf)) != EOF ){

if(qc < 258){

if(isSE(c)){

s = S5;
tokenReaded[qc] = (char)c;
qc++;
qcr++;

}else{

s = S13;
}

}else
s = S13;

}else{

tokenReaded[qc] = '\0';
Expand Down Expand Up @@ -903,7 +890,8 @@ void lAnalyzer(const char *fileName){

_Bool isSE(int character){

if(character == LETTER_T_LOWER || character == LETTER_N_LOWER || character == INVERTED_BAR ||
if(character == LETTER_T_LOWER || character == (LETTER_T_LOWER-32) || character == LETTER_N_LOWER ||
character == (LETTER_N_LOWER-32) || character == INVERTED_BAR ||
character == SIMPLE_QUOTATION_MARK || character == DOUBLE_QUOTATION_MARK)
return TRUE;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/lexical.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef struct Token{
struct Token *next;
union{
char *name;
unsigned long long num; //TODO see more about .. at least 64bit
long long int num; //TODO see more about ..
}attribute;

}Token;
Expand All @@ -70,7 +70,7 @@ _Bool isPrintableL(int character);
TokenType isSeparatorL(int character); //less < ' > " characters
TokenType isSeparatorAll(int character); //include all symbols separators
_Bool isSE(int character); // t n \ ' "
_Bool isStrangerSeparator(int character);
//_Bool isStrangerSeparator(int character);

int toLowercase(int c);
_Bool verifyingFile(const char *fileName); //it verifies characters invalids in the file, return true if do not exists invalid character
Expand Down

0 comments on commit e12c4a4

Please sign in to comment.