Anda di halaman 1dari 4

AST.

h le ZZ of pas
typedef enum {NB=0, _IDF = 1, BOOLEAN = 2, OP=3}Type_Exp de type (malloc), le n'est gr par la
virtuelle (la pile peut donc toute la
typedef enum {Int, Bool, Double} Type;
non par code et la
typedef enum {plus, moins, mult, _div} Type_Op;
typedef enum {false, true} boolean;
struct Exp ; typedef struct Exp * AST;
ce qui joue le rle de la la
typedef union {double nombre ;char *idf;boolean bool; de cette ?
struct {Type_Op top;AST expression_gauche ; Nous rutilis la table des comme solution
AST expression_droite ; } op;
de de des
} ExpValueTypeNode;
typedef struct Exp {
Type_Exp typeexp ;Type typename; QCM
ExpValueTypeNode noeud ; Control Flow Graph(Reprsentation..)
}expvalueType; bytecode J2EE one-address code
AST.C Grammaire attribue actions smantiques
Grammaire LL Analyseur descendant
AST arbre_gauche(AST a){return a->noeud.op.expression_gauche;}
Type_Op top(AST a){return a->noeud.op.top;} Acorn RISC Machine-ARM three-address code
Type type(AST a){return a->typename;} select * from * ;Erreur Syntaxique
boolean est_feuille(AST a){return(a->typeexp != OP);} bytecode (Reprsentation..)
select T1.A1 from T2 Erreur Smantique
AST creer_feuille_booleen(boolean b){AST result
Grammaire LR Analyseur Ascendant
result->typeexp=BOOLEAN;result->noeud.bool = b;
Commentaire C non ferm (/* sans */)Erreur Lexicale
result->typename = Bool;return result;}
DAG Reprsentation..
exam 2010 - 2011
bytecode J2ME one-address code
<EXPBOOL> ::= not <EXPBOOL>
| <EXPBOOL> and <EXPBOOL> Grammaire LALR Analyse Bottom-up
| <EXPBOOL> or <EXPBOOL> Terminal t T Classe dexpression rgulire de *
| <COLUMN> <OP> <COLUMN> Reprsentation intermdiaire linaire Code 2-adresses
Dsambiguser la grammaire Dsambiguser la grammaire Automate Piles Langage irrgulier
<EXPBOOL> ::= <OR> Grammaire rgulire Grammaire linaire
<OR> ::= <OR> or <NOT> | <NOT> Rcursivit gauche Bouclage du parseur LL(1)
<NOT> ::= not <NOT> | <AND>
Ambigut 2 Arbres syntaxiques
<AND> ::= <AND> and <AUX> | <AUX>
<AUX> ::= <COLUMN> <OP> <COLUMN Erreur de parenthsage non quilibr Analyse syntaxique
liminer la rcursivit gauche Analyse smantique Erreur de type
<OR> ::= <NOT> <ORAUX> LALR Parseur bottom-up
<ORAUX> ::= or <NOT><ORAUX> | LL(1)Parseur Top-down
<NOT> ::= (cette rgle ne change pas ne pas pnaliser si rpte !!) Automate dtat finis Langage rgulier
<NOTAUX> ::= (ni ce terminal, ni cette rgle n'existe !!) Dterminisme Grammaire linaire LL(1)
<AND> ::= <AUX> <ANDAUX> Reprsentation intermdiaire graphique DAG
<ANDAUX> ::= and <AUX> <ANDAUX> |
Identificateur erron Analyse lexicale
<AUX> ::= <COLUMN> <OP> <COLUMN> | ( <OR> )
lemme de ltoile Pompage
Rendre la grammaire LL(1)
<TABS> ::= idf <TABSAUX> A= < S, , , s0, F > o S F L
<TABSAUX> ::= , <TABS> | card( -fermeture( s0 ) ) > 1 (s0, )= s1, o s0 s1
typedef void * Vector ;fermeture de Kleene
Automate dtat finis Langage rgulier
struct Token lexme
Systme dquations Grammaire linaire
union // PRINT
-fermeture( s0 ) \ { s0} (s0, )= s1, o s0 s1
struct int rangvar; printnode;
Problme semi-dcidable lambigut dune grammaire
// left := right
struct Erreur : if sans endif (en csh)Analyse syntaxique
Erreur : /* sans */ ( en C)Analyse lexicale
// IF ... THEN
Optimisation en mmoire Minimiser un automate
struct int rangvar;
L1GLangage binaire
struct Java Reprsentation.Linaire
ADDOP REG1, REG2 Two-address code
} Nombre de Registres ncessaires pour
}i une expression arithmtique Attribut ncessaire la
gnration depseudo-code
// for end l Grammaire LL Analyseur Descendant
LALR Look Ahead Left-to-right with Rightmost parse
struct int rangvar ; AST AST *(null).suivant Erreur Smantique
Grammaire LR WAnalyseur Ascendant
struct Gnration de code une adresse Stack Machine code
Grammaire Ambigu Analyse floue
Non-terminal (First) Les suivants (Follow) Rcursivit Gauche Analyse sans fin
<COLUMNAUX> , from, ';' Grammaire Hrditairement-ambigu Analyse impossible
<POINTEDCOLUMN> '.' ',', ';', lower, loweroreq, Grammaire non LL Analyse descendante non optimale
greater, greateroreq, eq, neq Grammaire nullables Analogie avec les epsilon-NFA
<OR> N'EST PAS NULLABLE (*) (bonifier 2 idf, not ';' nest pas hors-contexte U deux langages hors-contexte
cas : (*) et ligne tableau vide OU ligne tableau Drivation droite et gauche Tri et tri inverse des feuilles
correcte !!
<NOTAUX> N'EXISTE PAS !! I VIDE
D
<ANDAUX> Eand or, ';'
<TABSAUX> ',' where, orderby, ';'
Exam 2012
IDF
exam 2009 2010
. Ajouter la grammaire linstruction daffichage dune chane de caractre : INUMBER ':'
INST : PRINT string ; ( avec string non terminal==["][^\n"]*["] ) ';' Y
. Ajouter la grammaire linstruction d'affectation boolenne complexe ':'
EXPB : EXPB or EXPB | EXPB and EXPB | not EXPB | ( EXPB ) | TRUEFALSE | idf ;
3. Aprs lenrichissement de la question (2)
(a) que remarquez vous, AST
(b) que proposez-vous ?
(a) La grammaire devient ambigu du fait quun IDF peut tre driv partir des i
non-terminaux EXPA et EXPB. .. (1 pt)
(b) dmarche de dsambiguisation.. .. (1 pt) * ;
4. Ajouter la grammaire la conditionnelle boolenne LIST_INST * ;
INST : if ( IDF = EXPB ) then LISTE_INST endif i
if ( IDF = EXPB ) then LISTE_INST else LISTE_INST endif
5. Aprs lenrichissement de la question (4)
(a) que remarquez vous,
(b) que proposez-vous ?
(a) La grammaire devient de nouveau ambigu du fait quune
conditionnelle if ( IDF = IDF ) peut tre driv partir des instructions :
if ( IDF = EXPA ) et : if ( IDF = EXPB ) (1 p
(b) dmarche de dsambiguisation En voici 6 erreurs smantiques nouvelles (toutes 4 parmi ces 6 sont suffisantes) :
1. Dans if ( IDF = EXPB ) la partie droite contient un identificateur non dclar
// if ... then else boo enne 2. Dans if ( IDF = EXPB ) la partie droite contient un identificateur dclar dun autre type que BOOL
struct 3. Dans if ( IDF = EXPB ) la partie droite contient un identificateur non ini ialis
int rangvar; // indice de l'idf (left exp) comparer, 4. Dans if ( IDF = EXPB ) la partie gauche est un identificateur non dclar
dans la able des symboles 5. Dans if ( IDF = EXPB ) la partie gauche est un identificateur dclar dun autre type que BOOL
ASTB right; // l'expression 6. Dans if ( IDF = EXPB ) la partie gauche est un identificateur non ini ialis
boolenne (righ exp)
comparer struct LIST_INST * typedef enum
henlins // then list of // PRINT str ng {Prin d Pr ntStr ng
ins ruc ions struct AssignAri h AssignBool
struct LIST_INST * elselinst; // else list of char * chaine ; ThenAri h ThenElseAri h
ins ruc ions } pr ntnode fThenBoo fThenE seBoo
} fnodeboo } Type_INST

void interpreter_pseudo_code_list_inst(pseudocode pc)


if (pc != NULL) { 8. Nous voudrions pouvoir exprimer des comparaisons riches et les utiliser
// interpretation de la prem re nstruct on dans es affectations et les cond t onne es
nterpreter_pseudo_code_ nst( pc->first ) ; On ajoutera un non-terminal COMP drivant les comparaisons arithmtiques
et boolennes en plus des rgles suivantes la grammaire
// appel rcursif sur la suite de pseudocode COMP : EXPA <= EXPA | EXPA => EXPA | EXPA = EXPA | EXPB = EXPB | EXPB
nterpreter_pseudo_code_ st_ nst( pc-> next );} INST : idf ":=" COMP
} if ( COMP ) then LISTE_INST end f
void if ( COMP ) then
nterpreter_pseudo_code_ nst(pseudocodeins ruc io LISTE_INST e se
n pci) LISTE_INST end f
// SQUELETTE DU PROGRAMME A RAFF NER:
swi ch(pci codop)
case ADD
op1 = VM_STACK.depiler() op2 = VM_STACK.depiler() // idf := COMP
VM_STACK empiler(op1 + op2) ; break struct
case DIV int rangvar; // indice de l'idf (left exp), o il faut affecter, dans la table des symboles
op1 = VM_STACK.depiler() op2 = ASTCOMP right; // l'expression boolenne complexe droi e (right exp) a ec er
VM_STACK.depiler() } boo ass gnnode
VM_STACK empiler(op1 / op2) ; break
case DUPL
VM_STACK empiler(VM_STACK e epile()); break // if ... then else arithmtique et boo en
case JMP struct
nterpreter_pseudo_code_ nst( ASTCOMP comparison;
rechercher_ins ruc ion_au_label(pci pci.param.label_name)) struct LIST_INST * thenlinst;
break struct LIST_INST * elselinst;
case LOAD } fnode
op1 = VM_STACK.empiler(@pci.param.var)
break Complter linterprteur de reprsentations intermdiaire pour prendre en
case SWAP compte linstruction for
op1 = VM_STACK depiler() op2 = void nterpreter_ nst(ins valueType
VM_STACK depiler() ins a ribu e)
VM_STACK empiler(op switch(ins a ribu e ypeins )
2) VM_STACK empiler(op1) ; break case forLoop
set_va ue(ins a ribu e node ornode rangvar instattribute.node.fornode.min)
// oubien TS[instattribute.node.fornode.rangvar] = ins a ribu e node ornode min

if (get_value(instattribute.node.fornode.rangvar) <= instattribute.node.fornode.max)


// oubien i (TS ins a ribu e node ornode rangvar <= ins a ribu e node ornode max)
interpreter_list_inst( forbodylinst )

break
// structure des - DATA // nouvel structure pour
struct char double ; typedef union
char ;
// nouvel structure pour struct ;
struct
char // nom
struct
;

// structure r typedef struct


r
struct r r ;
struct ;
;

1) la d'un
// pc <> NULL
void pc){
struct *));

(pc NULL){
);
== NULL) // n y a pas de
else ); // un en O(1) si // JG

2) la d'une
void pi, struct Element op1, op2 ;
= NULL;

JNE: op1 = depiler(VM_STACK); op2 =


(op1 != op2) ;

JG: op1 = depiler(VM_STACK); op2 =


(op1 > op2) ;

JMP: ( ; break ;
// des (hors
Pile * VM_STACK;
void inintialiser_machine_abstraite(){VM_STACK = creer_pile();}
void interpreter_pseudo_instruction(struct pseudoinstruction pi, char ** next_label_name){
Element op1, op2, resultat; int* rangvar ; *next_label_name = NULL;
switch(pi.codop){
case DATA: varvalueType nv; strcpy(nv.name, pi.param.nv.name);nv.valinit = pi.param.nv.value;
ajouter_nouvelle_variable_a_TS(nv);break;
case ADD: op1 = depiler(VM_STACK); op2 = depiler(VM_STACK);resultat = op1 + op2;
empiler(VM_STACK, resultat)break;
case _DIV: op1 = depiler(VM_STACK);op2 = depiler(VM_STACK);resultat = op1 / op2;
empiler(VM_STACK, resultat);break;
case _MULT: op1 = depiler(VM_STACK); op2 = depiler(VM_STACK);resultat = op1 * op2;
empiler(VM_STACK, resultat);break;
case SUB:op1 = depiler(VM_STACK); op2 = depiler(VM_STACK);
resultat = op1 - op2; empiler(VM_STACK, resultat);break;

case LOAD:
if (inTS(pi.param.var, rangvar) != true) if (debug) printf("%s n'est pas :\n", pi.param.var);
else if (debug) printf("%s est l'indice %d :\n",pi.param.var, *rangvar);
if (debug) printf("LOAD = %s %lf\n", pi.param.var,valinit(*rangvar));
empiler(VM_STACK, valinit(*rangvar));break;
case STORE: op1 = depiler(VM_STACK); inTS(pi.param.var, rangvar);set_valinit(*rangvar, op1); break;
case DUPL: op1 = depiler(VM_STACK); empiler(VM_STACK, op1);empiler(VM_STACK, op1); break;
case PUSH: empiler(VM_STACK, pi.param._const); break;
case SWAP:op1 = depiler(VM_STACK); op2 = depiler(VM_STACK);empiler(VM_STACK, op1); empiler(VM_STACK, op2); break;
case JNE: op1 = depiler(VM_STACK); op2 = depiler(VM_STACK);
if (op1 != op2) strcpy(*next_label_name,pi.param.label_name)
else {;} break;
case JMP: strcpy(*next_label_name, pi.param.label_name);
case PRNT:op1 = depiler(VM_STACK); printf("%lf", op1); break;
case LABEL: break;

void interpreter_pseudo_code(pseudocode pc){char ** next_label_name = (char **) malloc(sizeof (char*));


if (pc != NULL){interpreter_pseudo_instruction(pc->first, next_label_name);
if (*next_label_name == NULL) interpreter_pseudo_code(pc->next); // Il n y a pas de branchement !!
else{ // JNE ou JMP ==> effectuer un branchement (O(n)
struct pseudocodenode * compteur_ordinal = pc->next;
while ( (compteur_ordinal->first.codop != LABEL) ||
(strcmp(compteur_ordinal->first.param.label_name, *next_label_name) != 0) ) {
compteur_ordinal = compteur_ordinal->next;}
interpreter_pseudo_code(compteur_ordinal)
typedef enum {DATA, ADD, _DIV, DUPL, JMP, JNE, LABEL, LOAD, _MULT, POP, PRNT, PUSH, SUB, STORE, SWAP} CODOP;
typedef struct pseudocodenode * pseudocode;
struct namevalue {char * name;double value;};
typedef union {char * var;double _const;char * label_name;
struct namevalue nv;}Param;
struct pseudoinstruction{CODOP codop;Param param;};
struct pseudocodenode{struct pseudoinstruction first;
struct pseudocodenode * next};
void afficher_pseudo_code(pseudocode void inserer_code_en_queue(pseudocode pc1, pseudocode pc2){
pc){if (pc != NULL){ if (debug){afficher_pseudo_code(pc1); afficher_pseudo_code(pc2);}
afficher_pseudo_instruction(pc->first); if (pc1->next == NULL) { pc1->next = pc2;}
else{pseudocode pc = pc1;while(pc->next != NULL) {pc = pc->next;}
afficher_pseudo_code(pc->next);}} pc->next = pc2;}
if (debug) { afficher_pseudo_code(pc1); printf("\n");}}

typedef enum {BeginExpected} SyntacticErrorType; smerror * creer_sm_erreur(SemanticErrorType et, int line,


typedef enum {NonDeclaredVar,AlreadyDeclared, smerror * e = (smerror*) malloc (sizeof (smerror) );
BadlyInitialised,IncompatibleAssignType, e->name = (char *) malloc (strlen(name));
IncompatibleCompType,IncompatibleOperationType strcpy(e->name, name); e->line = line;e->errort = et;
} SemanticErrorType; return e;}
typedef struct {char *name;int line;
SemanticErrorType errort; void creer_sx_erreur(SyntacticErrorType et, int line){
} smerror; sxerror * e = (sxerror*) malloc (sizeof (sxerror) );
typedef struct {int line;SyntacticErrorType errort; e->line = line;e->errort = et; ERSX[NBERRSX++]= e; }
} sxerror;

TYPE
typedef enum {PrintIdf, PrintString, AssignArith, AssignBool, IfThenArith, IfThenElseArith} Type_INST ;

typedef struct {char *name; typedef struct {inline;}tokenvalueType;


int nbdecl;Type typevar;boolean
initialisation; typedef struct {Type typename; } typevalueType;
double valinit;int line;
} varvalueType; struct INST; // pr dclaration de la structure
typedef struct {Type typename; double valinit; }constvalueType;
de stockage d'une instruction
struct LIST_INST;// pr dclaration dela structure
typedef struct INST {Type_INST typeinst;
union {// PRINT idftoprint de stockage d'une liste d'instruction
struct {int rangvar; } printnode; instvalueType* creer_instruction_print(int rangvar){
// left := right instvalueType * printinstattribute = (instvalueType *) malloc
struct {int rangvar;AST right;} assignnode; (sizeof(instvalueType));
// IF ... THEN printinstattribute->typeinst = PrintIdf;
struct {int rangvar;AST right; printinstattribute->node.printnode.rangvar = rangvar;
struct LIST_INST * thenlinst; return printinstattribute;}
struct LIST_INST * elselinst;} ifnode;
} node; instvalueType* creer_instruction_affectation(int rangvar, AST *
} instvalueType; past){instvalueType * pinstattribute = (instvalueType *) malloc
typedef struct LIST_INST {struct INST first; (sizeof(instvalueType));
struct LIST_INST * next;} listinstvalueType; pinstattribute->typeinst =
typedef union { (type(*past)==Bool)?AssignBool:AssignArith;
varvalueType varattribute; pinstattribute->node.assignnode.rangvar = rangvar;
constvalueType constattribute; pinstattribute->node.assignnode.right = * past;
Type typename; return pinstattribute;
instvalueType instattribute; }
listinstvalueType listinstattribute;} valueType;

instvalueType* creer_instruction_if(int rangvar,AST* past,listinstvalueType *plistthen,listinstvalueType * plistelse){


instvalueType * pinstattribute = (instvalueType *) malloc (sizeof(instvalueType));
pinstattribute->typeinst = ((plistelse != NULL)?IfThenElseArith:IfThenArith);
pinstattribute->node.ifnode.rangvar = rangvar;
pinstattribute->node.ifnode.right = * past;
pinstattribute->node.ifnode.thenlinst = plistthen;
pinstattribute->node.ifnode.elselinst = plistelse;
return pinstattribute;}

Anda mungkin juga menyukai