Compare commits
2 commits
cb8854f069
...
0cf05dfd9c
Author | SHA1 | Date | |
---|---|---|---|
|
0cf05dfd9c | ||
|
4a5c84bb3e |
6 changed files with 284 additions and 25 deletions
29
Makefile
Normal file
29
Makefile
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
GRM=yacc.y
|
||||||
|
LEX=lex.l
|
||||||
|
BIN=out
|
||||||
|
|
||||||
|
CC=gcc
|
||||||
|
CFLAGS=-Wall -g
|
||||||
|
|
||||||
|
OBJ=y.tab.o lex.yy.o table.o
|
||||||
|
|
||||||
|
all: $(BIN)
|
||||||
|
@touch testFile # to prevent an error in case of deletion
|
||||||
|
./out < testFile
|
||||||
|
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
|
||||||
|
|
||||||
|
y.tab.c: $(GRM)
|
||||||
|
bison --yacc -d $<
|
||||||
|
|
||||||
|
lex.yy.c: $(LEX)
|
||||||
|
flex $<
|
||||||
|
|
||||||
|
$(BIN): $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) $(CPPFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm $(OBJ) y.tab.c y.tab.h lex.yy.c
|
||||||
|
|
71
lex.l
Normal file
71
lex.l
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
%{
|
||||||
|
#include "y.tab.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
||||||
|
/*options for compiling*/
|
||||||
|
%option noyywrap
|
||||||
|
%option noinput
|
||||||
|
%option nounput
|
||||||
|
|
||||||
|
|
||||||
|
/*definition of the different types of comments*/
|
||||||
|
M_COMMENT (\/\/).*
|
||||||
|
S_COMMENT \/\*(.|\n)*?\*\/
|
||||||
|
|
||||||
|
|
||||||
|
/*definition of the Ids and types of ints*/
|
||||||
|
ID [a-zA-Z][a-zA-Z0-9]*
|
||||||
|
INT_DEC [0-9]+
|
||||||
|
INT_HEX 0x[0-9a-fA-F]+
|
||||||
|
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
|
||||||
|
"if" return(tIF);
|
||||||
|
"else" return(tELSE);
|
||||||
|
"while" return(tWHILE);
|
||||||
|
"print" return(tPRINT);
|
||||||
|
"return" return(tRETURN);
|
||||||
|
"float" return(tFLOAT);
|
||||||
|
"int" return(tINT);
|
||||||
|
"void" return(tVOID);
|
||||||
|
"+" return(tADD);
|
||||||
|
"-" return(tSUB);
|
||||||
|
"*" return(tMUL);
|
||||||
|
"/" return(tDIV);
|
||||||
|
"<" return(tLT);
|
||||||
|
">" return(tGT);
|
||||||
|
"!=" return(tNE);
|
||||||
|
"==" return(tEQ);
|
||||||
|
">=" return(tGE);
|
||||||
|
"<=" return(tLE);
|
||||||
|
"=" return(tASSIGN);
|
||||||
|
"&&" return(tAND);
|
||||||
|
"||" return(tOR);
|
||||||
|
"!" return(tNOT);
|
||||||
|
"{" return(tLBRACE);
|
||||||
|
"}" return(tRBRACE);
|
||||||
|
"(" return(tLPAR);
|
||||||
|
")" return(tRPAR);
|
||||||
|
";" return(tSEMI);
|
||||||
|
"," return(tCOMMA);
|
||||||
|
|
||||||
|
{ID} return(tID);
|
||||||
|
{INT_DEC} return(tNB);
|
||||||
|
{INT_HEX} return(tNB);
|
||||||
|
|
||||||
|
|
||||||
|
/*comments are ignored, same for spaces and lines*/
|
||||||
|
{M_COMMENT} ;
|
||||||
|
{S_COMMENT} ;
|
||||||
|
[ ]+ ;
|
||||||
|
\n ;
|
||||||
|
|
||||||
|
/*anything else is considered an error*/
|
||||||
|
. yyerror(yytext);
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
// SI >> SC
|
23
table.c
23
table.c
|
@ -42,6 +42,7 @@ Symbol getStruct(char* name){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
error("No structure found");
|
error("No structure found");
|
||||||
|
return (createNewStructure("error", 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the index with this name*/
|
/* Returns the index with this name*/
|
||||||
|
@ -52,6 +53,7 @@ int getIndex(char* name){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
error("No index found");
|
error("No index found");
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* removes all symbols associated with the current Depth*/
|
/* removes all symbols associated with the current Depth*/
|
||||||
|
@ -67,7 +69,6 @@ void clearOutOfScopeVariable(){
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delta = currentIndex - i ; // the number of elements we remove
|
|
||||||
int futureCurrentIndex = i;
|
int futureCurrentIndex = i;
|
||||||
|
|
||||||
while(i < currentIndex) {
|
while(i < currentIndex) {
|
||||||
|
@ -156,7 +157,7 @@ void displayTable(){
|
||||||
printf("\n");
|
printf("\n");
|
||||||
doubleLine();
|
doubleLine();
|
||||||
printf("Table of Symbols, depth = %d, length = %d, ESP = %d, EBP = %d\n", currentDepth, currentIndex, esp ,ebp);
|
printf("Table of Symbols, depth = %d, length = %d, ESP = %d, EBP = %d\n", currentDepth, currentIndex, esp ,ebp);
|
||||||
printf("Name | init?, varType, offset, depth\n", currentDepth, currentIndex);
|
printf("Name | init?, varType, offset, depth\n");
|
||||||
doubleLine();
|
doubleLine();
|
||||||
for (int i = 0; i < currentIndex; ++i) {
|
for (int i = 0; i < currentIndex; ++i) {
|
||||||
Symbol a = symbolTable[i];
|
Symbol a = symbolTable[i];
|
||||||
|
@ -173,22 +174,4 @@ void line(){
|
||||||
}
|
}
|
||||||
void doubleLine(){
|
void doubleLine(){
|
||||||
printf("============================================================\n");
|
printf("============================================================\n");
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
initSymbolTable();
|
|
||||||
addElement("variable1", INT);
|
|
||||||
displayTable();
|
|
||||||
increaseDepth();
|
|
||||||
displayTable();
|
|
||||||
toggleInit("variable1");
|
|
||||||
displayTable();
|
|
||||||
addElement("variable2", INT);
|
|
||||||
displayTable();
|
|
||||||
addElement("variable3", FLOAT);
|
|
||||||
displayTable();
|
|
||||||
decreaseDepth();
|
|
||||||
displayTable();
|
|
||||||
addElement("variable4", INT);
|
|
||||||
displayTable();
|
|
||||||
}
|
}
|
8
table.h
8
table.h
|
@ -9,10 +9,10 @@
|
||||||
#define NAME_MAX_LENGTH 30
|
#define NAME_MAX_LENGTH 30
|
||||||
|
|
||||||
// a list of all type
|
// a list of all type
|
||||||
typedef enum enumVarType {INT, FLOAT} enumVarType; // TODO : update
|
typedef enum enumVarType {INT, FLOAT} enumVarType;
|
||||||
|
|
||||||
// a list of all type's sizes
|
// a list of all type's sizes
|
||||||
int memorySizes[2] ={1,1}; // TODO : update
|
extern int memorySizes[2] = {1,1}; // TODO : PROBLEM DOES'NT COMPILE
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[NAME_MAX_LENGTH];
|
char name[NAME_MAX_LENGTH];
|
||||||
|
@ -22,8 +22,6 @@ typedef struct {
|
||||||
int depth;
|
int depth;
|
||||||
} Symbol;
|
} Symbol;
|
||||||
|
|
||||||
// TODO : move comments here
|
|
||||||
|
|
||||||
/*============================
|
/*============================
|
||||||
Array and Reallocation
|
Array and Reallocation
|
||||||
============================*/
|
============================*/
|
||||||
|
@ -62,7 +60,7 @@ Symbol createNewStructure(char* name, enumVarType type);
|
||||||
void toggleInit(char *name);
|
void toggleInit(char *name);
|
||||||
|
|
||||||
/*============================
|
/*============================
|
||||||
Element Acess
|
Element Access
|
||||||
============================*/
|
============================*/
|
||||||
|
|
||||||
/* Returns the index with this name*/
|
/* Returns the index with this name*/
|
||||||
|
|
18
testFile
Normal file
18
testFile
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
int compute(int a, int d) {
|
||||||
|
b = a;
|
||||||
|
while (c > 0) {
|
||||||
|
b = b + a * 4;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
int a;
|
||||||
|
if (a == 3) {
|
||||||
|
print(a);
|
||||||
|
} else {
|
||||||
|
int b = compute(a, 2 * a);
|
||||||
|
print(b);
|
||||||
|
}
|
||||||
|
}
|
160
yacc.y
Normal file
160
yacc.y
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
%define parse.error detailed
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "table.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
%code provides {
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (const char *);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : PROBLEM DOES'NT COMPILE (enumVarType doenst exist)
|
||||||
|
%union {int nbInt; enumVarType int type; char* string;}
|
||||||
|
/*loops keywords*/
|
||||||
|
%token tWHILE tIF tELSE
|
||||||
|
/*reserved keywords*/
|
||||||
|
%token tRETURN tPRINT
|
||||||
|
/*types : integers, floats or void*/
|
||||||
|
%token tFLOAT tINT tVOID
|
||||||
|
/*operations*/
|
||||||
|
%left tDIV tMUL tADD tSUB
|
||||||
|
/*Assignment*/
|
||||||
|
%left tASSIGN
|
||||||
|
/*comparisons*/
|
||||||
|
%left tLT tGT tNE tEQ tGE tLE
|
||||||
|
/*boolean operators*/
|
||||||
|
%left tAND tOR tNOT
|
||||||
|
/*syntaxic symbols*/
|
||||||
|
%token tLBRACE tRBRACE tLPAR tRPAR tSEMI tCOMMA
|
||||||
|
|
||||||
|
/*nametags and values*/
|
||||||
|
%token <string> tID
|
||||||
|
%token <nbInt> tNB
|
||||||
|
|
||||||
|
|
||||||
|
/* represents types with the values used in the table, see table.h */
|
||||||
|
%type <type> Type
|
||||||
|
|
||||||
|
|
||||||
|
%start Lines
|
||||||
|
%%
|
||||||
|
|
||||||
|
/* Lines = Any line in the code that is not within an if/while statement*/
|
||||||
|
Lines : Line
|
||||||
|
| Line Lines;
|
||||||
|
|
||||||
|
Line : IfStatement
|
||||||
|
| WhileStatement
|
||||||
|
| Assignment
|
||||||
|
| FunctionDef
|
||||||
|
| Declaration
|
||||||
|
| FunctionCall
|
||||||
|
| Return
|
||||||
|
| Print;
|
||||||
|
|
||||||
|
/*Innerblock = the inside of an if/else/while statement = { ... } or function = f(){...}*/
|
||||||
|
InnerBlock : tLBRACE tRBRACE // a function or while loop can be empty cf GCC
|
||||||
|
| tLBRACE {increaseDepth();} Lines tRBRACE {decreaseDepth();};
|
||||||
|
|
||||||
|
/*Condition = the evaluated boolean expression for an if or while = ( ... ) */
|
||||||
|
Condition : tLPAR ConditionalExpression tRPAR;
|
||||||
|
|
||||||
|
/*ConditionalExpression = expression that evaluates to a boolean*/
|
||||||
|
ConditionalExpression : tID
|
||||||
|
| tNB
|
||||||
|
| tLPAR ConditionalExpression tRPAR // for cases like if((a or b) and (a or c)) where there are parenthesis inside
|
||||||
|
| NbOrVariable NumericalOperator NbOrVariable
|
||||||
|
| tNOT ConditionalExpression
|
||||||
|
/* replaced by the two following lines
|
||||||
|
| ConditionalExpression BinaryLogicalOperator ConditionalExpression
|
||||||
|
*/
|
||||||
|
| ConditionalExpression tOR ConditionalExpression
|
||||||
|
| ConditionalExpression tAND ConditionalExpression;
|
||||||
|
/*end of added bloat*/
|
||||||
|
|
||||||
|
|
||||||
|
/*NbOrVariable is either a number or a variable of type int*/
|
||||||
|
NbOrVariable : tID | tNB;
|
||||||
|
|
||||||
|
/*List of all numerical operators*/
|
||||||
|
NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT;
|
||||||
|
|
||||||
|
/*any arithmetic operation
|
||||||
|
Operation: tADD | tMUL | tSUB | tDIV;
|
||||||
|
*/
|
||||||
|
|
||||||
|
IfStatement : tIF Condition InnerBlock
|
||||||
|
| tIF Condition InnerBlock tELSE InnerBlock
|
||||||
|
|
||||||
|
WhileStatement : tWHILE Condition InnerBlock;
|
||||||
|
|
||||||
|
Assignment : tID tASSIGN Expression tSEMI {toggleInit($1);};
|
||||||
|
|
||||||
|
/*Expression operation applied on variables or values*/
|
||||||
|
Expression : NbOrVariable
|
||||||
|
| FunctionCall
|
||||||
|
| tLPAR Expression tRPAR
|
||||||
|
/* replaced by the four following lines
|
||||||
|
//| Expression Operation Expression
|
||||||
|
*/
|
||||||
|
| Expression tADD Expression
|
||||||
|
| Expression tSUB Expression
|
||||||
|
| Expression tMUL Expression
|
||||||
|
| Expression tDIV Expression;
|
||||||
|
/*end of added bloat*/
|
||||||
|
|
||||||
|
Expressions : Expression
|
||||||
|
| Expression tCOMMA Expressions;
|
||||||
|
|
||||||
|
FunctionCall : tID tLPAR Expressions tRPAR;
|
||||||
|
|
||||||
|
FunctionDef : Type tID FunctionParams InnerBlock;
|
||||||
|
|
||||||
|
/*FunctionParams = the parameters of a function*/
|
||||||
|
FunctionParams : tLPAR tRPAR
|
||||||
|
| tLPAR tVOID tRPAR
|
||||||
|
| tLPAR VarsWithType tRPAR
|
||||||
|
|
||||||
|
VarsWithType : VarWithType
|
||||||
|
| VarWithType tCOMMA VarsWithType;
|
||||||
|
|
||||||
|
/*VarWithType = a variable associated to its type = int a*/
|
||||||
|
VarWithType : Type tID;
|
||||||
|
|
||||||
|
/*the return type or argument type*/
|
||||||
|
Type : tINT {$$ = INT;}
|
||||||
|
| tFLOAT {$$ = FLOAT;};
|
||||||
|
|
||||||
|
Declaration : Type tID tSEMI {addElement($2, $1);}
|
||||||
|
| Type tID tASSIGN Expression tSEMI {addElement($2, $1);toggleInit($2);}
|
||||||
|
|
||||||
|
/* Potential improvement : take care of multiple definition on same line*/
|
||||||
|
| Type tID tCOMMA VarsCommaSeparated tSEMI {yyerror("[Beta] you cannot -still- define several variable on the same line");}
|
||||||
|
| Type tID tCOMMA VarsCommaSeparated tASSIGN Expression tSEMI {yyerror("[Beta] you cannot -still- define several variable on the same line");};
|
||||||
|
// yes this is perfectly valid in C, the last variable will hold the value, the others won't
|
||||||
|
|
||||||
|
|
||||||
|
// this is only used in case of a declaration of several same-typed
|
||||||
|
// variables, ex : `int a,b,c;`
|
||||||
|
VarsCommaSeparated : tID
|
||||||
|
| tID tCOMMA VarsCommaSeparated;
|
||||||
|
|
||||||
|
Return : tRETURN Expression tSEMI {decreaseDepth();};
|
||||||
|
|
||||||
|
Print : tPRINT tLPAR Expression tRPAR tSEMI;
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
void yyerror(const char *msg) {
|
||||||
|
fprintf(stderr, "\033[1m\033[31m[/!\\]\033[0m Error : %s\n", msg);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
initSymbolTable();
|
||||||
|
yyparse();
|
||||||
|
}
|
||||||
|
// SI >> SC
|
Loading…
Reference in a new issue