%{ /* * UPEM / Compilation / Projet * Pacien TRAN-GIRARD, Adam NAILI * * TODO : * ------ * - Gestion des tableaux * - Fonctions avec paramètres et retour de fonctions (void fonctions déjà fonctionnelles) * */ int nb_globals = 0; #include "symbol_table.h" #include #include #include "generator.h" extern int lineno; int yylex(); void yyerror(char *); static Scope scope = GLOBAL; static int bss_done = 0; static int num_label = 0; static int num_if = 0; %} %union { char caractere; int num; char ident[64]; int type; char comp[3]; char addsub; char divstar; } %token CARACTERE %token NUM %token IDENT %token ORDER EQ %token ADDSUB %token DIVSTAR %token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE %token TYPE %type Exp EB TB FB M E T F Parametres %type LValue %left ',' %precedence ')' %precedence ELSE %% Prog: { gen_prologue(); } DeclConsts DeclVars DeclFoncts { gen_const_declaration(); } ; DeclConsts: DeclConsts CONST ListConst ';' | ; ListConst: ListConst ',' IDENT '=' Litteral | IDENT '=' Litteral ; Litteral: NombreSigne | CARACTERE ; NombreSigne: NUM | ADDSUB NUM ; DeclVars: DeclVars TYPE Declarateurs ';' | ; Declarateurs: Declarateurs ',' Declarateur { gen_declaration($3, $0, scope); } | Declarateur { gen_declaration($1, $0, scope); } ; Declarateur: IDENT | IDENT '[' NUM ']' ; DeclFoncts: DeclFoncts DeclFonct | DeclFonct ; DeclFonct: EnTeteFonct { scope = LOCAL; } Corps { gen_function_end_declaration(); scope = GLOBAL; } ; EnTeteFonct: TYPE IDENT PrologueCont '(' Parametres ')' {gen_function_declaration($2, $1, $5);} | VOID IDENT PrologueCont '(' Parametres ')' {gen_function_declaration($2, 2, $5);} ; PrologueCont: {gen_prologue_continue(&bss_done);}; Parametres: VOID {$$ = 0;} | ListTypVar { $$ = $1;} ; ListTypVar: ListTypVar ',' TYPE IDENT { gen_declaration($4, $3, scope); $$ = $1+1; } | TYPE IDENT { gen_declaration($2, $1, scope); $$ = 1; } ; Corps: '{' DeclConsts DeclVars SuiteInstr '}' ; SuiteInstr: SuiteInstr Instr | ; Instr: Exp ';' | ';' | RETURN Exp ';' | RETURN ';' | READE '(' IDENT ')' ';' { gen_reade($3); } | READC '(' IDENT ')' ';' { gen_readc($3); } | PRINT '(' Exp ')' ';' { gen_print($3);} | IF '(' Exp IfHandling')' Instr { gen_if_label($4); } | IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling | WHILE '(' Exp ')' Instr | '{' SuiteInstr '}' ; IfHandling: { gen_if_start($$ = num_if++); }; IfEndHandling: { gen_if_end($-3); }; IfElseEndHandling: { gen_ifelse_end($-5); }; Exp: LValue '=' Exp { $$ = gen_assign($1, scope); } | EB ; EB: EB OR TB { gen_or($1, $3, num_label++); } | TB ; TB: TB AND FB { gen_and($1, $3, num_label++); } | FB ; FB: FB EQ M { gen_eq($2, $1, $3, num_label++); } | M ; M: M ORDER E { gen_order($2, $1, $3, num_label++); } | E ; E: E ADDSUB T { gen_addsub($2, $1, $3); } | T ; T: T DIVSTAR F { gen_divstar($2, $1, $3); } | F ; F: ADDSUB F { $$ = gen_signed_expr($1, $2); } | '!' F { $$ = gen_negate_expr($2); } | '(' Exp ')' { $$ = $2; } | LValue { $$ = gen_value($1, scope); } | NUM { $$ = gen_num($1, scope); } | CARACTERE { $$ = gen_char($1, scope); } | IDENT '(' Arguments ')' { $$ = gen_function_call($1,$3);} ; LValue: IDENT { gen_check($1, scope); } | IDENT '[' Exp ']' { gen_check($1, scope); } ; Arguments: ListExp | {$$ = 0;} ; ListExp: ListExp ',' Exp {$$ = $1 + 1;} | Exp {$$ = 1;} ; %% void yyerror(char *msg){ fprintf(stderr, "%s at line %d\n", msg, lineno); } int main(int argc, char **argv) { char opt; output = stdout; while ((opt = getopt(argc, argv, "o:")) != -1) switch (opt) { case 'o': output = fopen(optarg, "w+"); if (output == NULL) { perror("Error opening output file: "); return -1; } break; default: fputs("Exiting due to invalid option.\n", stderr); return -1; } return yyparse(); }