123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- %{
- #include "stdio.h"
- #include "stdlib.h"
- %}
-
- %union {
- int nombre;
- }
-
- %token<nombre> tNB
- %token tADD
- %token tSUB
- %token tMUL
- %token tDIV
- %token tPO
- %token tPF
- %token tPV
- %token tVIRG
- %token tAO
- %token tAF
- %token tEQ
- %token tEQ2
- %token tNOTEQ
- %token tNOT
- %token tINF
- %token tINFEQ
- %token tSUP
- %token tSUPEQ
- %token tAND
- %token tOR
-
- %token tINT
- %token tCONST
- %token tMAIN
- %token tIF
- %token tELSE
- %token tELSIF
- %token tWHILE
- %token tPRINTF
- %token tID
-
- %left tADD
- %left tSUB
- %left tMUL
- %left tDIV
- // %type<nombre> E
-
- %%
-
- S : Main { printf("S\n"); } ;
-
- Main : tINT tMAIN tPO Params tPF MainBody { printf("Main\n"); } ;
-
- Params : | Param SuiteParams { printf("Params\n"); } ;
- Param : tINT tID { printf("Param\n"); } ;
-
- SuiteParams : tVIRG Param SuiteParams { printf("SuiteParam\n"); } ;
-
- MainBody : tAO Declarations Instructions tAF { printf("MainBody\n"); } ;
-
- Body : tAO Instructions tAF { printf("Body\n"); } ;
-
- Instructions : Instruction Instructions | { printf("Instructions\n"); } ;
-
- Instruction : Aff | Printf { printf("Instruction\n"); } ;
-
- Aff : tID tEQ E tPV { printf("Aff\n"); } ;
-
- E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E { printf("E\n"); } ;
-
- Declarations : | Declaration Declarations { printf("Declarations\n"); } ;
-
- Declaration : DeclarationInt | DeclarationConst { printf("Declaration\n"); } ;
-
- DeclarationInt : tINT DeclarationBody tPV { printf("DeclarationInt\n"); } ;
-
- DeclarationConst : tCONST DeclarationBody tPV { printf("DeclarationConst\n"); } ;
-
- DeclarationBody : tID tVIRG DeclarationBody | tID { printf("DeclarationBody\n"); } ;
-
- // While : tWHILE tPO Cond tPF Body { printf("While\n"); } ;
- //
- // If : IfSimple | IfElse { printf("If\n"); } ;
- //
- // IfSimple : tIF tPO Cond tPF Body { printf("IfSimple\n"); } ;
- //
- // IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ;
- //
- // Cond : tNOT Cond | Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT tPO Cond tPF { printf("Cond\n"); } ;
-
- Printf : tPRINTF tPO tID tPF tPV { printf("Printf\n"); } ;
-
- // If : tIF tPO Cond tPF Body ;
- //
- // Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | tNOT Cond ;
-
- // Invocation : tID tPO Args tPF ;
-
- // Args : .... cf params
-
- // Return : tRET E tPV ;
-
- %%
-
- #include <string.h>
-
- #define LABEL_SIZE 10
-
- // ADD @ + @ -> @
- // SUB @ - @ -> @
- // AFC nb -> @
- // CPY @ -> @
-
- // Exemple
- // y = 1 + 2;
- // AFC 1 32
- // AFC 2 33
- // ADD 32 33 32
- // CPY 32 @y
-
- /*
- table des symboles (tS) :
- sous forme de pile
- -> où sera la variable lors de l'exec
- |
- | nom | type | @ | init |
- |-------|--------|-----|--------|
- | a | | 0 | |
- | b | | 1 | |
- | c | | 2 | |
- | | | | |
- | . | | . | | -> entiers sur 1 octet
- | . | | . | |
- | . | | . | |
- | | | | |
- | y | | 26 | |
-
- Donc on remplace @y par 26 dans CPY 32 @y
-
- /!\
-
- E + E + E
- | | |
- -> -> -> vt variable temporaire (autre expression)
- -> -> -> variable -> ADD @E1 @E2 vt
- -> -> -> nombre
-
- Donc 9 possibilités (3 par E)
-
- On suppose que E est tout le temps une vt, donc on génère tout le temps le même code
-
- */
-
- int id_counter = 0;
-
- typedef struct node {
- char label[LABEL_SIZE];
- int left_child;
- int right_child;
- int id;
- } node;
-
- int create_node(char* l, int n1, int n2) {
- node n;
- strcpy(l, n.label);
- n.left_child = n1;
- n.right_child = n2;
- n.id = id_counter;
- id_counter++;
- }
-
- void yyerror(char const* err) {
- printf("%s\n", err);
- exit(1);
- }
-
- void main(void) {
- yyparse();
- }
|