123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- %{
- #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 ;
-
- Main : tINT tMAIN tPO Params tPF MainBody ;
-
- Params : | Param SuiteParams ;
- Param : tINT tID ;
-
- SuiteParams : tVIRG Param SuiteParams ;
-
- MainBody : tAO Declarations Instructions tAF ;
-
- Body : tAO Instructions tAF ;
-
- Instructions : Instruction Instructions | ;
-
- Instruction : Aff | Printf | If ;
-
- Aff : tID tEQ E tPV ;
-
- E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E ;
-
- Declarations : | Declaration Declarations ;
-
- Declaration : DeclarationInt | DeclarationConst ;
-
- DeclarationInt : tINT DeclarationBody tPV ;
-
- DeclarationConst : tCONST DeclarationBody tPV ;
-
- DeclarationBody : tID tVIRG DeclarationBody | tID ;
-
- If : IfSimple | IfElse ;
-
- IfSimple : tIF tPO Cond tPF Body ;
-
- IfElse : IfSimple tELSE Body ;
-
- 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 Cond | tNOT tPO Cond tPF ;
-
- Printf : tPRINTF tPO tID tPF tPV
-
- // 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 ;
-
-
- /* S -> E ; S
- * S ->
- */
- // S : E tPV
- // { printf("RES: %d\n", $1); }
- // S
- // | { printf("END\n"); }
- // ;
- //
- // E : E tADD E { $$ = $1 + $3; }
- // | E tSUB E { $$ = $1 - $3; }
- // | tOB E tCB { $$ = $2; }
- // | tNB { $$ = $1; }
- // ;
-
- %%
-
- void yyerror(char const* err) {
- printf("%s\n", err);
- exit(1);
- }
-
- void main(void) {
- yyparse();
- }
|