Avancée compilation maiis bug

This commit is contained in:
Elies Tali 2021-04-04 17:56:21 +02:00
parent e34720da2c
commit 2be358e747
18 changed files with 1330 additions and 973 deletions

4
Lex_Yacc/ProgC Normal file
View file

@ -0,0 +1,4 @@
int main(int a, int b)
{
int c;
}

BIN
Lex_Yacc/a.exe Executable file

Binary file not shown.

Binary file not shown.

View file

@ -1,6 +1,13 @@
%{
#include "as.tab.h"
int yywrap(void){return 1;}
void
yyerror (char const *s)
{
fprintf (stderr, "%s\n", s);
}
%}
%%

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,9 @@
/* A Bison parser, made by GNU Bison 3.0.4. */
/* A Bison parser, made by GNU Bison 3.5.1. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -30,6 +31,9 @@
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Undocumented macros, especially those whose name start with YY_,
are private implementation details. Do not rely on them. */
#ifndef YY_YY_AS_TAB_H_INCLUDED
# define YY_YY_AS_TAB_H_INCLUDED
/* Debug traces. */
@ -78,17 +82,16 @@ extern int yydebug;
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 1 "as.y" /* yacc.c:1909 */
#line 1 "as.y"
int nombre;
char id[30];
#line 90 "as.tab.h" /* yacc.c:1909 */
};
#line 93 "as.tab.h"
};
typedef union YYSTYPE YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1

View file

@ -7,13 +7,10 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../Symboles/tab_instruc.h"
#define TAILLE 1024
int addr = 0;
enum type_t type_courant;
int * tab_instruc = malloc(sizeof(int)*TAILLE);
int index_instruc = 0;
%}
@ -22,7 +19,7 @@ int index_instruc = 0;
%token tOBRACE tCBRACE
%token tINT
%token tCONST
%token tPV tCOMA#include <string.h>
%token tPV tCOMA
%token tMUL tDIV tADD tSUB tEQ
%token<nombre> tNB tNBEXP
%token<id> tID
@ -39,6 +36,8 @@ int index_instruc = 0;
%left tADD tSUB
%left tMUL tDIV
%type<nombre> E Invocation
//%type<nombre> E
@ -47,7 +46,7 @@ int index_instruc = 0;
%%
Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(pile); printf("addr = %d\n",addr);} ;
Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
Params : { printf("Sans Params\n"); } ;
Params : Param SuiteParams ;
@ -55,16 +54,16 @@ Param : DeclType tID { printf("Prametre : %s\n", $2); };
SuiteParams : tCOMA Param SuiteParams ;
SuiteParams : ;
Body : tOBRACKET Instructions tCBRACKET { struct symbole_t symbole = {"Salut", 0x77b58af, INT, 1}; push(symbole, pile); } ;
Body : tOBRACKET Instructions tCBRACKET { } ;
Instructions : Instruction Instructions ;
Instructions : ;
Instruction : Aff ;
Instruction : Decl ;
Instruction : Invocation tPV ;
Instruction : If;
Instruction : While;
Instruction : Aff {reset_temp_vars();};
Instruction : Decl {reset_temp_vars();};
Instruction : Invocation tPV{reset_temp_vars();};
Instruction : If {reset_temp_vars();};
Instruction : While {reset_temp_vars();};
If : tIF tOBRACE Cond tCBRACE Body Else { printf("If reconnu\n"); };
@ -81,44 +80,57 @@ SuiteCond : tOR E SuiteCond;
Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1);} ; //besoin de get_address
E : tNB { printf("Nombre\n");
struct symbole_t symbole = ("", addr, INT, 1};
push(symbole, pile);
$$=addr;
addr++;
printf("AFC %d %d",addr,$1); } ;
E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
E : tNBEXP { printf("Nombre exp\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("AFC %d %d",addr,$1); };
E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
E : tID { printf("Id\n"); /*Faire un get_address sur la pile*/};
E : E tMUL E { printf("Mul\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("MUL %d %d %d",addr, $1,$2);};
E : E tDIV E { printf("Div\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("DIV %d %d %d",addr, $1,$2);};
E : E tSUB E { printf("Sub\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("SOU %d %d %d",addr, $1,$2);};
E : E tADD E { printf("Add\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("ADD %d %d %d",addr, $1,$2);}};
E : Invocation { printf("Invoc\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("AFC %d %d",addr, $1);};
E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2};
E : tSUB E { printf("Moins\n"); printf("SUB %d 0 %d",addr,$2);};
E : E tEQCOND E { printf("==\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("EQU %d %d %d",addr, $1,$3);};
E : E tGT E { printf(">\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("SUP %d %d %d",addr, $1,$3);};
E : E tLT E { printf("<\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("SUP %d %d %d",addr, $1,$3);};
E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$2); $$ = addr;};
E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$2); $$ = addr;};
E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$2); $$ = addr;};
E : E tADD E { printf("Add\n"); int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr,$1,$2); $$ = addr;};
E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, 0,addr,0); $$ = addr;};
E : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_operation(EQU, addr,$1,$3); $$ = addr;};
E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;};
E : tNOT E { printf("!\n"); };
//Créer un champ isConst dans la table des symboles
DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
Decl : DeclType SuiteDecl FinDecl ;
Decl : tCONST DeclType SuiteDeclConst FinDeclConst;
SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
FinDecl : tPV { printf("Fin Decl\n");};
FinDecl : tCOMA SuiteDecl FinDecl ;
SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
FinDeclConst : tPV;
FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
/* //Créer un champ isConst dans la table des symboles
DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
Decl : tCONST DeclType SuiteDeclConst { } ;
SuiteDeclConst : tCOMA tID SuiteDeclConst ;
SuiteDeclConst : tEQ E tPV { };
SuiteDeclConst : tPV { };
DeclType : tINT {type_courant = INT;} ;
Decl : DeclType Decl SuiteDecl { } ;
Decl : tID {push($1, 0, type_courant);};
Decl : tID tEQ E {push($1,1, type_courant);} ;
Decl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);} ;
SuiteDecl : tCOMA Decl SuiteDecl { };
SuiteDecl : tPV { };
*/
Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
/*S : E tPV
{ printf("RES: %d\n", $1); }

View file

@ -1,5 +1,4 @@
bison -d -t as.y -v
flex al.lex
gcc *.c ../Symboles/table_symboles.c -ly
cat ../Fichiers_Tests/progC | ./a.out
gcc as.tab.c lex.yy.c ../Symboles/tab_instruc.c ../Symboles/table_symboles.c -ll -o a.exe
cat ./ProgC | ./a.exe

View file

@ -483,8 +483,15 @@ char *yytext;
#line 2 "al.lex"
#include "as.tab.h"
int yywrap(void){return 1;}
#line 487 "lex.yy.c"
#line 488 "lex.yy.c"
void
yyerror (char const *s)
{
fprintf (stderr, "%s\n", s);
}
#line 494 "lex.yy.c"
#line 495 "lex.yy.c"
#define INITIAL 0
@ -701,10 +708,10 @@ YY_DECL
}
{
#line 6 "al.lex"
#line 13 "al.lex"
#line 708 "lex.yy.c"
#line 715 "lex.yy.c"
while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{
@ -763,161 +770,161 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
#line 8 "al.lex"
#line 15 "al.lex"
{ return tMAIN ;}
YY_BREAK
case 2:
YY_RULE_SETUP
#line 9 "al.lex"
#line 16 "al.lex"
{ return tOBRACKET;}
YY_BREAK
case 3:
YY_RULE_SETUP
#line 10 "al.lex"
#line 17 "al.lex"
{ return tCBRACKET; }
YY_BREAK
case 4:
YY_RULE_SETUP
#line 11 "al.lex"
#line 18 "al.lex"
{ return tOBRACE; }
YY_BREAK
case 5:
YY_RULE_SETUP
#line 12 "al.lex"
#line 19 "al.lex"
{ return tCBRACE; }
YY_BREAK
case 6:
YY_RULE_SETUP
#line 13 "al.lex"
#line 20 "al.lex"
{ return tCONST; }
YY_BREAK
case 7:
YY_RULE_SETUP
#line 14 "al.lex"
#line 21 "al.lex"
{ return tINT; }
YY_BREAK
case 8:
YY_RULE_SETUP
#line 15 "al.lex"
#line 22 "al.lex"
{ return tPRINTF; } //Degeu mais à degager
YY_BREAK
case 9:
YY_RULE_SETUP
#line 16 "al.lex"
#line 23 "al.lex"
{ return tIF; }
YY_BREAK
case 10:
YY_RULE_SETUP
#line 17 "al.lex"
#line 24 "al.lex"
{ return tWHILE; }
YY_BREAK
case 11:
YY_RULE_SETUP
#line 18 "al.lex"
#line 25 "al.lex"
{ return tLT; }
YY_BREAK
case 12:
YY_RULE_SETUP
#line 19 "al.lex"
#line 26 "al.lex"
{ return tGT; }
YY_BREAK
case 13:
YY_RULE_SETUP
#line 20 "al.lex"
#line 27 "al.lex"
{ return tEQCOND; }
YY_BREAK
case 14:
YY_RULE_SETUP
#line 21 "al.lex"
#line 28 "al.lex"
{ return tAND; }
YY_BREAK
case 15:
YY_RULE_SETUP
#line 22 "al.lex"
#line 29 "al.lex"
{ return tOR; }
YY_BREAK
case 16:
YY_RULE_SETUP
#line 23 "al.lex"
#line 30 "al.lex"
{ return tELSE;}
YY_BREAK
case 17:
YY_RULE_SETUP
#line 26 "al.lex"
#line 33 "al.lex"
{ yylval.nombre = atoi(yytext); return tNB; }
YY_BREAK
case 18:
YY_RULE_SETUP
#line 27 "al.lex"
#line 34 "al.lex"
{ yylval.nombre = -1; return tNBEXP; } //Renvoyer le token tNB et pas tNBEXP
YY_BREAK
case 19:
YY_RULE_SETUP
#line 28 "al.lex"
#line 35 "al.lex"
{ return tADD; }
YY_BREAK
case 20:
YY_RULE_SETUP
#line 29 "al.lex"
#line 36 "al.lex"
{ return tSUB; }
YY_BREAK
case 21:
YY_RULE_SETUP
#line 30 "al.lex"
#line 37 "al.lex"
{ return tMUL; }
YY_BREAK
case 22:
YY_RULE_SETUP
#line 31 "al.lex"
#line 38 "al.lex"
{ return tDIV; }
YY_BREAK
case 23:
YY_RULE_SETUP
#line 32 "al.lex"
#line 39 "al.lex"
{ return tEQ; }
YY_BREAK
case 24:
YY_RULE_SETUP
#line 33 "al.lex"
#line 40 "al.lex"
{ return tPV; }
YY_BREAK
case 25:
YY_RULE_SETUP
#line 34 "al.lex"
#line 41 "al.lex"
{} //Ne pas les retourner à Yacc
YY_BREAK
case 26:
YY_RULE_SETUP
#line 35 "al.lex"
#line 42 "al.lex"
{} //Ne pas les retourner à Yacc
YY_BREAK
case 27:
YY_RULE_SETUP
#line 36 "al.lex"
#line 43 "al.lex"
{ return tCOMA; }
YY_BREAK
case 28:
/* rule 28 can match eol */
YY_RULE_SETUP
#line 37 "al.lex"
#line 44 "al.lex"
{} //Ne pas les retourner à Yacc
YY_BREAK
case 29:
YY_RULE_SETUP
#line 38 "al.lex"
#line 45 "al.lex"
{ strcpy(yylval.id, yytext); return tID; }
YY_BREAK
case 30:
YY_RULE_SETUP
#line 39 "al.lex"
#line 46 "al.lex"
{ return tERROR; }
YY_BREAK
case 31:
YY_RULE_SETUP
#line 41 "al.lex"
#line 48 "al.lex"
ECHO;
YY_BREAK
#line 921 "lex.yy.c"
#line 928 "lex.yy.c"
case YY_STATE_EOF(INITIAL):
yyterminate();
@ -1922,7 +1929,7 @@ void yyfree (void * ptr )
#define YYTABLES_NAME "yytables"
#line 41 "al.lex"
#line 48 "al.lex"

BIN
Lex_Yacc/lex.yy.o Normal file

Binary file not shown.

0
Lex_Yacc/output.txt Normal file
View file

View file

@ -0,0 +1,68 @@
#include "tab_instruc.h"
int current_index = 0;
struct operation_t tab_op[MAXTAILLE];
void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3){
if (current_index == MAXTAILLE){
printf("Taillemax tableau operations atteinte\n");
}
else{
struct operation_t new_op = {opcode, arg1, arg2, arg3};
tab_op[current_index] = new_op;
current_index++;
}
}
char * get_asm_line_from_op(struct operation_t op){
char * buffer = malloc(sizeof(char)*200);
switch (op.opcode){
case (ADD):
sprintf(buffer,"ADD %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (MUL):
sprintf(buffer,"MUL %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (SOU):
sprintf(buffer,"SOU %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (DIV):
sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (COP):
sprintf(buffer,"COP %d %d\n",op.arg1, op.arg2);
break;
case (AFC):
sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
break;
case (JMP):
sprintf(buffer,"JMP %d\n",op.arg1);
break;
case (JMF):
sprintf(buffer,"JMF %d %d\n",op.arg1, op.arg2);
break;
case (INF):
sprintf(buffer,"INF %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (SUP):
sprintf(buffer,"SUP %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (EQU):
sprintf(buffer,"DIV %d %d %d\n",op.arg1, op.arg2, op.arg3);
break;
case (PRI):
sprintf(buffer,"PRI %d\n",op.arg1);
break;
}
return buffer;
}
void create_asm(){
FILE * output = fopen("output.txt","w");
for (int i = 0; i < current_index; i++){
char * line = get_asm_line_from_op(tab_op[i]);
fputs(line, output);
free(line);
}
}

View file

@ -1,5 +1,19 @@
enum instructions_t {ADD,SUB}
#define MAXTAILLE 1024
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void add_operation(struct instructions_t inst, int * argv, int argv);
enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,JMP,JMF,INF,SUP,EQU,PRI};
struct operation_t {
enum opcode_t opcode;
int arg1;
int arg2;
int arg3;
};
extern struct operation_t tab_op[MAXTAILLE];
void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);
void create_asm();

BIN
Symboles/tab_instruc.o Normal file

Binary file not shown.

View file

@ -30,8 +30,12 @@ Opérations possible :
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAXADDR 1024*5
int last_addr = 0;
int temp_addr = MAXADDR;
int taille_types[] = {-1, 4};
int profondeur = 0;
struct element_t {
struct symbole_t symbole;
@ -42,7 +46,6 @@ struct pile_t {
int taille;
struct element_t * first;
};
*
struct pile_t * pile;
char * type_to_string(enum type_t type) {
@ -67,16 +70,21 @@ void init (void) {
pile->taille = 0;
}
void push(char * nom, int isInit, enum type_t type) {
int push(char * nom, int isInit, enum type_t type) {
struct element_t * aux = malloc(sizeof(struct element_t));
struct symbole_t symbole = {"", last_addr, type, isInit,profondeur};
strcpy(symbole.nom,nom);
aux->symbole = symbole;
aux->suivant = pile->first;
pile->first = aux;
pile->taille++;
int addr_var = last_addr;
last_addr += taille_types[type];
return addr_var;
}
struct symbole_t pop() {
struct symbole_t retour = {"", 0, UNKNOWN, 0};
struct symbole_t retour = {"", 0, UNKNOWN, 0, 0};
struct element_t * aux;
if (pile->taille > 0) {
aux = pile->first;
@ -84,6 +92,7 @@ struct symbole_t pop() {
retour = aux->symbole;
free(aux);
pile->taille--;
last_addr -= taille_types[retour.type];
}
return retour;
}
@ -107,13 +116,13 @@ char status(char * nom) {
return retour;
}
struct symbole_t * getVariable(char * nom){
struct symbole_t * get_variable(char * nom){
struct symbole_t * retour = NULL;
struct element_t * aux = pile->first;
int i;
for (i=0; i < pile->taille; i++) {
if (!strcmp(nom, aux->symbole.nom)) {
retour = element_t;
retour = &aux->symbole;
break;
} else {
aux = aux->suivant;
@ -135,3 +144,19 @@ void print() {
aux = aux->suivant;
}
}
int get_last_addr(){
return last_addr;
}
int allocate_mem_temp_var(enum type_t type){
temp_addr -= taille_types[type];
return temp_addr;
}
void reset_temp_vars(){
temp_addr = MAXADDR;
}

View file

@ -28,9 +28,8 @@ Opérations possible :
#include <stdint.h>
enum type_t {UNKNOWN, INT};
int taille_types[] = {-1, 4};
char * tab_instructions[2] = {"ADD %d %d", "SUB %d %d"}
extern int taille_types[];
extern int profondeur;
struct symbole_t {
char nom[30];
@ -42,11 +41,14 @@ struct symbole_t {
void print_symbole(struct symbole_t symbole);
int profondeur = 0;
void init(void);
void push(struct symbole_t symbole, struct pile_t * pile);
struct symbole_t pop(struct pile_t * pile);
int push(char * nom, int isInit, enum type_t type);
struct symbole_t pop();
// renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
char status(char * nom, struct pile_t * pile);
void print(struct pile_t * pile);
char status(char * nom);
void print();
int get_last_addr();
struct symbole_t * get_variable(char * nom);
int allocate_mem_temp_var(enum type_t type);
void reset_temp_vars();

Binary file not shown.