summaryrefslogtreecommitdiff
path: root/misc-test
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2010-09-18 09:49:44 +0000
committerLudovic Pouzenc <ludovic@pouzenc.fr>2010-09-18 09:49:44 +0000
commitae72235e30e2ed31e971ae861b64c7970697d858 (patch)
treebdf4c6715290d9587c2aad512b15c0635b8c211e /misc-test
parent804cac5bcade6e128573d697c70fb23050f823f7 (diff)
download2010-netlemmings-ae72235e30e2ed31e971ae861b64c7970697d858.tar.gz
2010-netlemmings-ae72235e30e2ed31e971ae861b64c7970697d858.tar.bz2
2010-netlemmings-ae72235e30e2ed31e971ae861b64c7970697d858.zip
Retour à la version 1 du compilo, qui elle compile... ermerm
git-svn-id: file:///var/svn/2010-netlemmings/trunk@33 077b3477-7977-48bd-8428-443f22f7bfda
Diffstat (limited to 'misc-test')
-rw-r--r--misc-test/compilateur_c_en_c/compil.lex6
-rw-r--r--misc-test/compilateur_c_en_c/compil.yy152
2 files changed, 39 insertions, 119 deletions
diff --git a/misc-test/compilateur_c_en_c/compil.lex b/misc-test/compilateur_c_en_c/compil.lex
index f298704..32ddfe5 100644
--- a/misc-test/compilateur_c_en_c/compil.lex
+++ b/misc-test/compilateur_c_en_c/compil.lex
@@ -31,8 +31,6 @@ if return IF;
else return ELSE;
while return WHILE;
do return DO;
-for return FOR;
-switch return SWITCH;
main{BLANC}*\({BLANC}*\) return MAIN;
printf return PRINTF;
@@ -49,14 +47,12 @@ int return INT;
"=" return AFF;
"==" return EGALE;
"!=" return DIFFERENT;
-"++" return INCR;
-"--" return DECR;
\( return '(';
\) return ')';
{IDENTIFICATEUR} {
int lg = strlen(yytext);
if ( lg > MAX_LABEL_LEN ) {
- return LEXERROR;
+ return ERROR;
} else {
yylval.str = malloc(sizeof(char)*lg);
strcpy(yylval.str, yytext);
diff --git a/misc-test/compilateur_c_en_c/compil.yy b/misc-test/compilateur_c_en_c/compil.yy
index 2ed4ad4..3543258 100644
--- a/misc-test/compilateur_c_en_c/compil.yy
+++ b/misc-test/compilateur_c_en_c/compil.yy
@@ -26,40 +26,30 @@
void genCode(int opcode, int op1, int op2, int op3);
void genCodeJump(int opcode, int op1, int op2, int op3, char *targetLabel);
void genLabel(char *lbl, char *prefix, int id);
- struct symbolEntry * createTempo();
%}
-%token LEXERROR
+%token ERROR
%token IF
%token ELSE
%token WHILE
%token DO
-%token FOR
-%token SWITCH
%token MAIN
%token CONST
%token INT
%token PRINTF
-
+%left FOIS DIV
+%left '+' '-'
+%left '>' '<'
+%left EGALE DIFFERENT
+ /* TODO : if contractés : <cond>?<then>:<else> */
%token AFF
/* TODO : += -= *= /= */
-%nonassoc EGALE DIFFERENT
-%nonassoc '>' '<'
-%left '+' '-'
-%left '*' '/'
-
-%token INCR DECR /* TODO : priorité ? */
- /* TODO : affectations conditionnelles : <cond>?<then>:<else> */
-
%token <str> IDENTIFICATEUR
%token <nb> ENTIER
-
-%type <nb> Entier
-
%type <nb> InitVarLoc
%type <sym> DeclVarLoc1
%type <sym> DeclConst1
@@ -70,11 +60,11 @@
%type <sym> LValue
%type <sym> RValue
+ /* %type <str> BeginIfWithoutElse */
+
%type <nb> BeginIf
-%type <nb> BeginFor
%type <nb> BeginWhile
%type <nb> BeginDoWhile
-%type <nb> BeginSwitch
%union { int nb; char *str; struct symbolEntry *sym; }
@@ -82,20 +72,20 @@
%%
Prog: MAIN Block { /* printSymTable(tableSym);*/ }
-Block: '{' { contextProf++; }
+Block: '{' { contextProf++; }
Declarations
Instructions
- '}' { /* printf("Block\n");*/
- /*printSymTable(tableSym);*/
- cleanSymStack(&tableSym, --contextProf);
- }
+ '}' { /* printf("Block\n");*/
+ /*printSymTable(tableSym);*/
+ cleanSymStack(&tableSym, --contextProf);
+ }
BlockOuInstr: Block
| Instruction
Declarations: /*vide*/ { printf("Declaration vide\n"); }
- | Decl ';' Declarations
+ | Decl ';' Declarations
Decl: INT DeclVarLocList { /*printf("Decl\n");*/ }
| CONST INT DeclConstList
@@ -123,7 +113,7 @@ DeclVarLoc: DeclVarLoc1 { /*printf("Variable non initalisee : '%s'\n", $1->s
DeclVarLoc1: IDENTIFICATEUR { $$ = addSym($1, contextProf, 0); }
-InitVarLoc: AFF Entier { $$=$2; /*printf("Init à la valeur %i\n", $2);*/ }
+InitVarLoc: AFF ENTIER { $$=$2; /*printf("Init à la valeur %i\n", $2);*/ }
/************************
** Liste de constantes **
@@ -131,7 +121,7 @@ InitVarLoc: AFF Entier { $$=$2; /*printf("Init à la valeur %i\n", $2);*/ }
DeclConstList: DeclConst
| DeclConst ',' DeclConstList
-DeclConst: DeclConst1 AFF Entier {
+DeclConst: DeclConst1 AFF ENTIER {
if ( $1 == NULL ) {
yyerror("Symbol already declared");
YYERROR;
@@ -196,7 +186,7 @@ Instruction: PRINTF '(' RValue ')' ';'
writeLabel(lbl);
}
- | BeginWhile BlockOuInstr {
+ | BeginWhile BlockOuInstr {
char lbl[MAX_LABEL_LEN];
genLabel(lbl, "wh1_", $1);
genCodeJump(I_JMP, UNK, PAD, PAD, lbl);
@@ -205,46 +195,20 @@ Instruction: PRINTF '(' RValue ')' ';'
writeLabel(lbl);
}
- | BeginFor '(' Instruction {
- char lbl[MAX_LABEL_LEN];
- genLabel(lbl, "for1_",$1);
- }
- RValue {
- char lbl[MAX_LABEL_LEN];
- genLabel(lbl, "for2_",$1);
- //genCodeJump(I_JMF, $4->address, PAD, PAD, lbl); TODO comprendre pourquoi compile pas
- }
- IDENTIFICATEUR INCR ')' Block /* Bricolage pour pas avoir a différer de la génération de code :S */
- {
- //struct symbolEntry *forVar;
- /* TODO : choper le symbole de $5 puis genCode... */
- yyerror("For not yet fully implemented !");
-
- char lbl1[MAX_LABEL_LEN], lbl2[MAX_LABEL_LEN];
- genLabel(lbl1, "for1_", $1); // FIXME : $1 pointe vers IDENTIFICATEUR ???
- genLabel(lbl2, "for2_", $1);
-
- genCodeJump(I_JMP, UNK, PAD, PAD, lbl1);
-
- writeLabel(lbl2);
- }
-
| BeginDoWhile BlockOuInstr WHILE '(' RValue ')' ';'
{
char lbl1[MAX_LABEL_LEN], lbl2[MAX_LABEL_LEN];
genLabel(lbl1, "do1_", $1);
genLabel(lbl2, "do2_", $1);
- genCodeJump(I_JMF, $5->address, UNK, PAD, lbl2);
+ genCodeJump(I_JMF, $5->address, PAD, PAD, lbl2);
genCodeJump(I_JMP, UNK, PAD, PAD, lbl1);
writeLabel(lbl2);
}
-BeginFor: FOR { $$=structCount++; }
-BeginSwitch: SWITCH { $$=structCount++; }
-BeginIf: IF '(' RValue ')' {
+BeginIf: IF '(' RValue ')' {
char lbl[MAX_LABEL_LEN];
$$=structCount++;
genLabel(lbl, "if1_", $$);
@@ -285,51 +249,25 @@ LValue: Symbole {
$$=$1;
}
}
-/* Version de base, sans optimisation de calculs ni constantes composées
-RValue: Constante { $$=$1; }
- | Symbole { $$=$1; }
- | '(' RValue ')' { $$=$2; }
- | RValue '+' RValue { $$=createTempo(); genCode(I_ADD, $$->address, $1->address, $3->address); }
- | RValue '*' RValue { $$=createTempo(); genCode(I_MUL, $$->address, $1->address, $3->address); }
- | RValue '-' RValue { $$=createTempo(); genCode(I_SOU, $$->address, $1->address, $3->address); }
- | RValue '/' RValue { $$=createTempo(); genCode(I_DIV, $$->address, $1->address, $3->address); }
- | RValue '<' RValue { $$=createTempo(); genCode(I_INF, $$->address, $1->address, $3->address); }
- | RValue '>' RValue { $$=createTempo(); genCode(I_SUP, $$->address, $1->address, $3->address); }
- | RValue EGALE RValue { $$=createTempo(); genCode(I_EQU, $$->address, $1->address, $3->address); }
-
-
-Constante: ENTIER { $$=createTempo(); genCode(I_AFC, $$->address, $1, PAD); $$->initialized=1; $$->readOnly=1; }
-*/
-
-
-RValue: Constante { $$=$1; }
- | Symbole { $$=$1; }
- | '(' RValue ')' { $$=$2; }
- | AddsOrSubs { $$=$1 }
- | Muls { $$=$1 }
- | RValue '/' RValue { $$=createTempo(); genCode(I_DIV, $$->address, $1->address, $3->address); }
- | RValue '<' RValue { $$=createTempo(); genCode(I_INF, $$->address, $1->address, $3->address); }
- | RValue '>' RValue { $$=createTempo(); genCode(I_SUP, $$->address, $1->address, $3->address); }
- | RValue EGALE RValue { $$=createTempo(); genCode(I_EQU, $$->address, $1->address, $3->address); }
-
-AddOrSubs: AddOrSubs '+' RValue
-
- { $$=createTempo(); genCode(I_ADD, $$->address, $1->address, $3->address); }
-
-
-/* Vieux truc :
-// TODO : ça marche pas avec a=2-a; --> obligé d'avoir un GLR parser ?
-Entier: ENTIER { $$=$1; }
- | '(' Entier ')' { $$=$2; }
- | Entier '+' Entier %prec CST_PLUS { $$=$1+$3; }
- | Entier '*' Entier %prec CST_MUL { $$=$1*$3; }
- | Entier '-' Entier %prec CST_MOINS { $$=$1-$3; }
- | Entier '/' Entier %prec CST_DIV { $$=$1/$3; }
- | Entier '<' Entier %prec CST_INF { $$=$1<$3; }
- | Entier '>' Entier %prec CST_SUP { $$=$1>$3; }
- | Entier EGALE Entier %prec CST_EGALE { $$=$1==$3; }
-
-*/
+
+RValue: Constante { $$=$1; }
+ | Symbole {
+ //TODO : expression complete et pas symbole (qui retourne addresse ou symbole ?)
+ $$=$1;
+ }
+
+Constante: ENTIER {
+ char tempoName[MAX_LABEL_LEN];
+ sprintf(tempoName, "!tempo%d", tempCount++); // TODO : si tempo trop long, bug
+ $$ = addSym(tempoName, contextProf, 1);
+ if ( $$ == NULL ) {
+ yyerror("Error while declaring a temp variable");
+ YYERROR;
+ } else {
+ $$->initialized=1;
+ genCode(I_AFC, $$->address, $1, PAD);
+ }
+ }
%%
void yyerror(char *s)
{
@@ -412,18 +350,4 @@ void genLabel(char *lbl, char *prefix, int id)
sprintf(lbl, "!%s%d", prefix, id);
}
-struct symbolEntry * createTempo()
-{
- char tempoName[MAX_LABEL_LEN];
- struct symbolEntry *sym = NULL;
-
- sprintf(tempoName, "!tempo%d", tempCount++); // TODO : si tempo trop long, bug
- sym = addSym(tempoName, contextProf, 1);
- if ( sym == NULL ) {
- yyerror("Error while declaring a temp variable");
- /*YYERROR;*/
- }
- return sym;
-}
-
// TODO : variable globales en dehors du main ?