Projet-Systemes-Informatiques/yacc.y
2023-04-12 23:48:01 +02:00

160 lines
4.7 KiB
Text

%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