projet_systeme/analyse_syntaxique.y

231 lines
7.1 KiB
Text
Raw Normal View History

2021-03-18 12:18:42 +01:00
%union {
int nombre;
2021-03-22 12:45:21 +01:00
char id[30];
2021-03-18 12:18:42 +01:00
}
2021-03-22 12:45:21 +01:00
%{
#include <stdio.h>
#include "table_symboles.h"
2021-04-30 17:08:29 +02:00
#include "table_fonctions.h"
2021-04-06 18:22:31 +02:00
#include "gen_assembleur.h"
enum Initialised_Variable init;
enum Symbole_Type type;
2021-04-30 17:08:29 +02:00
enum Return_Type return_type;
Table_Symboles table;
2021-04-30 17:08:29 +02:00
Table_Fonctions table_fonctions;
2021-04-06 18:22:31 +02:00
instructions_array array;
2021-04-12 22:54:52 +02:00
int whileCondition;
2021-04-30 17:08:29 +02:00
int return_value;
2021-03-22 12:45:21 +01:00
%}
2021-03-18 12:18:42 +01:00
%token<nombre> tENTIER
2021-03-22 12:45:21 +01:00
%token<nombre> tENTIEREXP
2021-04-06 18:22:31 +02:00
%type<nombre> E
2021-04-30 17:08:29 +02:00
%type<nombre> Return Instructions
2021-04-06 18:22:31 +02:00
%type<nombre> Cond
2021-04-30 17:08:29 +02:00
%type<nombre> While Else Invocation
2021-04-06 18:22:31 +02:00
2021-04-12 22:54:52 +02:00
2021-03-18 12:18:42 +01:00
%token tADD
%token tSUB
%token tMUL
%token tDIV
2021-04-12 22:54:52 +02:00
%token<nombre> tPO
2021-03-18 12:18:42 +01:00
%token tPF
%token tAO
%token tAF
%token tERROR
2021-04-30 17:08:29 +02:00
%token tAPPERSAND
2021-03-18 12:18:42 +01:00
%token tPV
%token tVIRGULE
%token tAFFECTATION
%token tEGAL
%token tDIFF
%token tLT
%token tGT
%token tGTE
%token tLTE
%token tMAIN
%token tINT
%token tPRINT
%token tRETURN
%token tOR
%token tAND
2021-04-06 18:22:31 +02:00
%token<nombre> tIF
2021-03-18 12:18:42 +01:00
%token tELSE
2021-04-12 22:54:52 +02:00
%token<nombre> tWHILE
2021-03-18 12:18:42 +01:00
%token tCONST
2021-03-22 12:45:21 +01:00
%token<id> tVAR
2021-03-18 12:18:42 +01:00
%token tNOT
%left tADD
%left tSUB
2021-03-22 12:45:21 +01:00
%left tMUL
%left tDIV
2021-03-18 12:18:42 +01:00
%right tEGAL
2021-03-22 12:45:21 +01:00
2021-03-18 12:18:42 +01:00
%%
2021-04-30 17:08:29 +02:00
/*C : Fonctions Main ;
Fonctions : ;
Fonctions : Fonction Fonctions ;
Fonction : tINT tVAR tPO Params tPF Body;*/
2021-05-09 19:55:27 +02:00
C : {generate_instruction_1(&array, JMP, -1);} Fonctions;
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Fonctions: Main;
Fonctions: Fonction Fonctions;
2021-03-18 12:18:42 +01:00
2021-05-09 19:55:27 +02:00
Main : tINT tMAIN {update_jmp(&array, 0, array.index); add_function(&table_fonctions, "Main", RET_INT, array.index); table.depth++;} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
Fonction : Function_type tVAR {{add_function(&table_fonctions, $2, return_type, array.index); table.depth++;}} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Function_type: tINT {type = TYPE_INT;} ;
Function_type: tINT tMUL {type = TYPE_INT_PTR;};
Params : {} ;
2021-03-22 12:45:21 +01:00
Params : Param SuiteParams ;
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Param : Param_type tVAR {add_symbole_top(&table, $2, type, INITIALISED, table.depth);} ;
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Param_type: tINT {type = TYPE_INT;} ;
Param_type: tINT tMUL {type = TYPE_INT_PTR;};
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
SuiteParams : tVIRGULE Param SuiteParams ;
2021-03-22 12:45:21 +01:00
SuiteParams : ;
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Body : tAO Instructions Return tAF {} ;
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Instructions : Instruction Instructions {$$ = array.index;};
Instructions : {$$ = array.index;};
2021-03-18 12:18:42 +01:00
2021-03-22 12:45:21 +01:00
Instruction : Aff ;
Instruction : If ;
Instruction : While ;
Instruction : Print ;
Instruction : Decl ;
Instruction : Invocation tPV ;
2021-03-18 12:18:42 +01:00
Decl : Type Valeur SuiteDecl tPV ;
2021-03-18 12:18:42 +01:00
2021-03-22 12:45:21 +01:00
SuiteDecl: tVIRGULE Valeur SuiteDecl ;
SuiteDecl: ;
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Type : tINT {type = TYPE_INT;} ;
Type : tCONST tINT {type = TYPE_CONST_INT;} ;
2021-04-30 17:08:29 +02:00
Type : tINT tMUL {type = TYPE_INT_PTR;};
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Valeur : tVAR {add_symbole_top(&table, $1, type, INITIALISED, table.depth);} tAFFECTATION E {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $4); free_temp(&table);};
Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
2021-03-18 12:18:42 +01:00
2021-05-09 19:55:27 +02:00
Aff : tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $3); free_temp(&table); };
Aff : tMUL tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP_STR, varAddr, $4); free_temp(&table); };
2021-04-30 17:08:29 +02:00
2021-03-18 12:18:42 +01:00
2021-04-12 22:54:52 +02:00
E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
2021-04-30 17:08:29 +02:00
E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, vt, varAddr); $$ = vt;};
2021-04-12 22:54:52 +02:00
E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
2021-04-06 18:22:31 +02:00
E : tSUB E {printf("Variable negative\n");} ;
2021-04-30 17:08:29 +02:00
E : Invocation {
2021-05-09 19:55:27 +02:00
//int vt = new_temp(&table);
//generate_instruction_2(&array, COP, vt, $1);
2021-04-30 17:08:29 +02:00
remove_symboles(&table);
table.depth--;
2021-05-09 19:55:27 +02:00
$$ = $1;};
2021-04-06 18:22:31 +02:00
E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
2021-04-30 17:08:29 +02:00
E : tAPPERSAND tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, LEA, vt, varAddr); $$ = vt;};
E : tMUL tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP, vt, varAddr); generate_instruction_2(&array, COP_LD, vt, vt); $$ = vt;};
2021-03-18 12:18:42 +01:00
2021-04-06 18:22:31 +02:00
If : tIF tPO Cond tPF {
2021-04-12 22:54:52 +02:00
//gen_jmpf(&table, &array, $3, -1);
generate_instruction_2(&array, JMF, $3, -1);
free_temp(&table);
2021-04-06 18:22:31 +02:00
$1 = array.index;
}
2021-05-04 15:24:57 +02:00
tAO {table.depth++;} Instructions {generate_instruction_1(&array, JMP, -1);} tAF {remove_symboles(&table); table.depth--;}
2021-04-06 18:22:31 +02:00
{
int adr_jmp = array.index;
2021-04-12 22:54:52 +02:00
update_jmf(&array, $1, adr_jmp);
2021-04-06 18:22:31 +02:00
}
2021-04-30 17:08:29 +02:00
Else {printf("updating jump\n"); update_jmp(&array, $8, $13);};
2021-03-18 12:18:42 +01:00
2021-04-30 17:08:29 +02:00
Else : tELSE tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {$$ = array.index;} ;
Else : {$$ = array.index;};
Else : tELSE If {$$ = array.index;} ;
2021-03-18 12:18:42 +01:00
2021-04-13 12:18:28 +02:00
While : tWHILE tPO {
$2 = array.index ;
} Cond tPF {
//gen_jmpf(&table, &array, $4, -1);
generate_instruction_2(&array, JMF, $4, -1);
2021-04-12 22:54:52 +02:00
free_temp(&table);
$1 = array.index;
}
2021-04-30 17:08:29 +02:00
tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {
2021-04-12 22:54:52 +02:00
int adr_jmp = array.index;
update_jmf(&array, $1, adr_jmp);
//gen_jmpf(&table, &array, $1, $2);
generate_instruction_1(&array, JMP, $2);
};
Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;};
Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tGT E {generate_instruction_3(&array, GT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tLTE E {generate_instruction_3(&array, LTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tGTE E {generate_instruction_3(&array, GTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tAND E {generate_instruction_3(&array, AND, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
Cond : E {$$ = $1; };
2021-05-09 19:55:27 +02:00
Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableBottom);} Args tPF
2021-04-30 17:08:29 +02:00
{int function_index = function_exists(&table_fonctions, $1);
int jmp_addr = (table_fonctions.array[function_index]).start_addr;
2021-05-09 19:55:27 +02:00
generate_instruction_2(&array, CALL, jmp_addr, table.indexAvailableTop);
2021-04-30 17:08:29 +02:00
$$ = return_value;
};
Args : Arg SuiteArgs ;
Args :
2021-05-04 15:24:57 +02:00
Arg : E {int arg_addr = prepare_argument_push(&table); generate_instruction_2(&array, COP, arg_addr, $1); free_temp(&table);};
2021-04-30 17:08:29 +02:00
SuiteArgs : tVIRGULE Arg SuiteArgs ;
SuiteArgs : ;
2021-03-18 12:18:42 +01:00
2021-04-12 22:54:52 +02:00
Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
2021-03-18 12:18:42 +01:00
2021-04-12 22:54:52 +02:00
Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
2021-03-18 12:18:42 +01:00
%%
2021-03-22 12:45:21 +01:00
#include <stdio.h>
void main(void){
//TODO: rajouter gestion des erreurs
initialise_table(&table);
2021-04-30 17:08:29 +02:00
initialise_function_table(&table_fonctions);
2021-04-06 18:22:31 +02:00
initialise_asm(&array);
2021-03-22 12:45:21 +01:00
yyparse();
print_table(&table);
2021-04-30 17:08:29 +02:00
printf("\n");
print_fonction_table(&table_fonctions);
2021-04-06 18:22:31 +02:00
//remove_symboles(&table, 0);
//print_table(&table);
2021-04-12 22:54:52 +02:00
exportInstructions(&array);
2021-03-22 12:45:21 +01:00
}