Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define YYDEBUG 1
- #include "declaration.h"
- #include "commands.h"
- /* Ilość błędów */
- int errors;
- /* Struktura dla etykiet do petli IF, WHILE */
- struct lbs
- {
- int for_goto;
- int for_jmp_false;
- };
- /* Inicjalizacja nowej struktury */
- struct lbs * newlblrec()
- {
- /* malloc zwraca *void więc należy rzutować na strukturę ponownie */
- return (struct lbs *) malloc(sizeof(struct lbs));
- }
- /* Deklaracja zmiennej */
- dec(char *name)
- {
- var *ID;
- ID = decDone(name); //check if prevoiusly declared
- if (ID == 0)
- {
- ID = decNew(name);
- }
- else
- {
- errors++;
- printf("%s jest już zdeklarowany\n", name);
- }
- }
- /* Sprawdzenie zadeklarowania */
- is_declare(char *command, char *name)
- {
- var *ID;
- ID = decDone(name);
- if(ID == 0)
- {
- errors++;
- printf("%s nie jest zdeklarowany\n", name);
- }
- else
- {
- gen_code(command, ID->nr);
- }
- }
- %}
- %union semrec
- {
- int val;
- char *id;
- struct lbs *lbls;
- }
- %start program
- %token <val> NUM
- %token <id> ID
- %token <lbls> IF WHILE
- %token THEN ELSE DO END
- %token READ PRINT VAR START
- %token ASSIGN
- %left EQ NEQ LT GT LTEQ GTEQ
- %left '-' '+'
- %left '*' '/'
- %%
- program : VAR
- declarations
- START
- commands
- END { gen_code("HALT", -1); YYACCEPT; }
- ;
- declarations : /* empty */
- | id_seq ID { dec($2); }
- ;
- id_seq : /* empty */
- | id_seq ID { dec($2); }
- ;
- commands : commands command
- |
- ;
- command : ID ASSIGN exp ';' { is_declare("STORE", $1); }
- | READ ID ';' { is_declare("READ", $2); }
- | PRINT ID ';' { is_declare("PRINT", $2); }
- | PRINT NUM ';' {
- gen_code("ZERO", -1);
- gen_code("ADDC", $2);
- is_declare("STORE", "#print0");
- is_declare("PRINT", "#print0")
- }
- | IF condition {
- $1 = (struct lbs *) newlblrec();
- $1->for_jmp_false = reserve_loc();
- }
- THEN commands {
- $1->for_goto = reserve_loc(); }
- ELSE {
- back_patch($1->for_jmp_false, "JGE", gen_label()); }
- commands
- END {
- back_patch($1->for_goto, "JUMP", gen_label()); }
- | WHILE {
- $1 = (struct lbs *) newlblrec();
- $1->for_goto = gen_label();
- }
- condition { $1->for_jmp_false = reserve_loc(); }
- DO
- commands
- END {
- gen_code("JUMP", $1->for_goto );
- back_patch( $1->for_jmp_false, "JGE", gen_label());
- }
- ;
- exp : NUM { gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- }
- | ID { is_declare("LOAD", $1); }
- | NUM '+' NUM { gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- gen_code("ADDC", $3);
- }
- | ID '+' ID { is_declare("LOAD", $1);
- is_declare("ADD", $3);
- }
- | ID '+' NUM { is_declare("LOAD", $1);
- gen_code("ADDC", $3);
- }
- | NUM '+' ID { gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("ADD", $3);
- }
- | NUM '-' NUM { gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- gen_code("SUBC", $3);
- }
- | ID '-' ID { is_declare("LOAD", $1);
- is_declare("SUB", $3);
- }
- | ID '-' NUM { is_declare("LOAD", $1);
- gen_code("SUBC", $3);
- }
- | NUM '-' ID { gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("SUB", $3);
- }
- | NUM '*' NUM { gen_code("ZERO",-1);
- gen_code("ADDC", ($1*$3));}
- | NUM '*' ID {
- if($1==0)
- gen_code("ZERO",-1);
- else if ($1==1)
- is_declare("LOAD",$3);
- else
- {
- gen_code("ZERO", -1);
- is_declare("STORE", "xA");
- gen_code("ZERO",-1);
- gen_code("ADDC",$1);
- is_declare("SUB",$3);
- gen_code("JGE", gen_label()+7);
- gen_code("ZERO",-1);
- gen_code("ADDC", $1);
- is_declare("STORE", "xB");
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- gen_code("JUMP",gen_label()+6);
- gen_code("ZERO",-1);
- gen_code("ADDC", $1);
- is_declare("STORE", "xC");
- is_declare("LOAD", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("ADD", "xD");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+10);
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- gen_code("JUMP", gen_label()-10);
- is_declare("LOAD", "xB");
- is_declare("SUB", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xA");
- is_declare("STORE", "xA");
- gen_code("ZERO", -1);
- is_declare("ADD", "xB");
- gen_code("JZ", gen_label()+4);
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- gen_code("JUMP", gen_label()-25);
- is_declare("LOAD", "xA");
- }
- }
- | ID '*' NUM {
- if($3==0)
- {
- gen_code("ZERO",-1);
- }
- else if ($3==1)
- {
- is_declare("LOAD",$1);
- }
- else
- {
- gen_code("ZERO", -1);
- is_declare("STORE", "xA");
- is_declare("LOAD",$1);
- gen_code("SUBC",$3);
- gen_code("JGE",gen_label()+7);
- is_declare("LOAD", $1);
- is_declare("STORE", "xB");
- gen_code("ZERO",-1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xC");
- gen_code("JUMP", gen_label()+6);
- is_declare("LOAD", $1);
- is_declare("STORE", "xC");
- gen_code("ZERO",-1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("ADD", "xD");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+10);
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- gen_code("JUMP", gen_label()-10);
- is_declare("LOAD", "xB");
- is_declare("SUB", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xA");
- is_declare("STORE", "xA");
- gen_code("ZERO", -1);
- is_declare("ADD", "xB");
- gen_code("JZ", gen_label()+5);
- gen_code("ZERO",-1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xC");
- gen_code("JUMP", gen_label()-26);
- is_declare("LOAD", "xA");
- }
- }
- | ID '*' ID {
- gen_code("ZERO", -1);
- is_declare("STORE", "xA");
- is_declare("LOAD",$1);
- is_declare("SUB",$3);
- gen_code("JGE",gen_label()+6);
- is_declare("LOAD", $1);
- is_declare("STORE", "xB");
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- gen_code("JUMP",gen_label()+5);
- is_declare("LOAD", $1);
- is_declare("STORE", "xC");
- is_declare("LOAD", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("ADD", "xD");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+10);
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- gen_code("JUMP", gen_label()-10);
- is_declare("LOAD", "xB");
- is_declare("SUB", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xA");
- is_declare("STORE", "xA");
- gen_code("ZERO", -1);
- is_declare("ADD", "xB");
- gen_code("JZ", gen_label()+4);
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- gen_code("JUMP", gen_label()-25);
- is_declare("LOAD", "xA");
- }
- | NUM '/' NUM {
- if($3==0)
- {
- gen_code("ZERO",-1);
- }
- else if($1/$3<=0)
- gen_code("ZERO",-1);
- else
- {
- gen_code("ZERO",-1);
- gen_code("ADDC", ($1/$3));
- }
- }
- | NUM '/' ID
- {
- if($1==0)
- gen_code("ZERO",-1);
- else
- {
- gen_code("ZERO", -1);
- is_declare("ADD", $3);
- gen_code("JZ", gen_label()+33);
- is_declare("STORE", "xB");
- gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+9);
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- gen_code("JUMP", gen_label()-9); //
- is_declare("LOAD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+7);
- is_declare("LOAD", "xA");
- is_declare("SUB", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", "xB");
- is_declare("ADD", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", $3);
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+4);
- gen_code("JUMP", gen_label()-27);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "xB");
- is_declare("SUB", $3);
- }
- }
- | ID '/' NUM
- {
- if($3==0)
- gen_code("ZERO",-1);
- else if($3==1)
- is_declare("LOAD", $1);
- else
- {
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- gen_code("JZ", gen_label()+34);
- is_declare("STORE", "xB");
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+9);
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- gen_code("JUMP", gen_label()-9); //
- is_declare("LOAD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+7);
- is_declare("LOAD", "xA");
- is_declare("SUB", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", "xB");
- is_declare("ADD", "xD");
- is_declare("STORE", "xB");
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+4);
- gen_code("JUMP", gen_label()-29);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "xB");
- gen_code("SUBC", $3);
- }
- }
- | ID '/' ID {
- gen_code("ZERO", -1);
- is_declare("ADD", $3);
- gen_code("JZ", gen_label()+32);
- is_declare("STORE", "xB");
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+9);
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- gen_code("JUMP", gen_label()-9); //
- is_declare("LOAD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+7);
- is_declare("LOAD", "xA");
- is_declare("SUB", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", "xB");
- is_declare("ADD", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", $3);
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+4);
- gen_code("JUMP", gen_label()-27);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "xB");
- is_declare("SUB", $3);
- }
- | NUM '%' NUM {
- if($3==0)
- {gen_code("ZERO",-1);}
- else
- {
- gen_code("ZERO",-1);
- gen_code("ADDC",($1%$3));
- }
- }
- | NUM '%' ID {
- if($1==0)
- {
- gen_code("ZERO",-1);
- }
- else
- {
- gen_code("ZERO", -1);
- is_declare("ADD", $3);
- gen_code("JZ", gen_label()+33);
- is_declare("STORE", "xB");
- gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+9);
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- gen_code("JUMP", gen_label()-9); //
- is_declare("LOAD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+7);
- is_declare("LOAD", "xA");
- is_declare("SUB", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", "xB");
- is_declare("ADD", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", $3);
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+4);
- gen_code("JUMP", gen_label()-27);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "xA");
- }
- }
- | ID '%' NUM {
- if($3==0)
- {gen_code("ZERO",-1);}
- else
- {
- gen_code("ZERO", -1);
- is_declare("STORE", "xB");
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+9);
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- gen_code("JUMP", gen_label()-9); //
- is_declare("LOAD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+7);
- is_declare("LOAD", "xA");
- is_declare("SUB", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", "xB");
- is_declare("ADD", "xD");
- is_declare("STORE", "xB");
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+4);
- gen_code("JUMP", gen_label()-29);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "xA");
- }
- }
- | ID '%' ID {
- gen_code("ZERO", -1);
- is_declare("ADD", $3);
- gen_code("JZ", gen_label()+32);
- is_declare("STORE", "xB");
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "#jeden");
- is_declare("STORE", "xD");
- is_declare("LOAD", $3);
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+9);
- is_declare("LOAD", "xD");
- is_declare("ADD", "xD");
- is_declare("STORE", "xD");
- is_declare("LOAD", "xC");
- is_declare("ADD", "xC");
- is_declare("STORE", "xC");
- is_declare("ADD", "xC");
- gen_code("JUMP", gen_label()-9); //
- is_declare("LOAD", "xC");
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+7);
- is_declare("LOAD", "xA");
- is_declare("SUB", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", "xB");
- is_declare("ADD", "xD");
- is_declare("STORE", "xB");
- is_declare("LOAD", $3);
- is_declare("SUB", "xA");
- gen_code("JGE", gen_label()+4);
- gen_code("JUMP", gen_label()-27);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "xA");
- }
- ;
- condition : NUM EQ NUM {
- if($1 == $3)
- {
- gen_code("ZERO", -1);
- }
- else
- {
- gen_code("ZERO", -1);
- gen_code("ADDC", 1);
- }
- }
- | NUM EQ ID {
- gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("STORE", "xC");
- is_declare("SUB", $3);
- gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
- is_declare("LOAD", $3);
- is_declare("STORE", "xA");
- is_declare("LOAD", "xC");
- is_declare("STORE", "xB");
- gen_code("JUMP", gen_label()+5);
- is_declare("LOAD", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "xA");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+3);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "#jeden");
- }
- | ID EQ NUM {
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xC");
- is_declare("SUB", $1);
- gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "xC");
- is_declare("STORE", "xB");
- gen_code("JUMP", gen_label()+5);
- is_declare("LOAD", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", $1);
- is_declare("STORE", "xB");
- is_declare("LOAD", "xA");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+3);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "#jeden");
- }
- | ID EQ ID {
- is_declare("LOAD", $1);
- is_declare("SUB", $3);
- gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
- is_declare("LOAD", $3);
- is_declare("STORE", "xA");
- is_declare("LOAD", $1);
- is_declare("STORE", "xB");
- gen_code("JUMP", gen_label()+5);
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "xA");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+3);
- gen_code("ZERO", -1);
- gen_code("JUMP", gen_label()+2);
- is_declare("LOAD", "#jeden");
- /* Tutaj if */
- }
- | NUM NEQ NUM {
- if($1 == $3)
- {
- is_declare("LOAD", "#jeden");
- }
- else
- {
- gen_code("ZERO", -1);
- }
- }
- | NUM NEQ ID {
- gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("STORE", "xC");
- is_declare("SUB", $3);
- gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
- is_declare("LOAD", $3);
- is_declare("STORE", "xA");
- is_declare("LOAD", "xC");
- is_declare("STORE", "xB");
- gen_code("JUMP", gen_label()+5);
- is_declare("LOAD", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "xA");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | ID NEQ NUM {
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("STORE", "xC");
- is_declare("SUB", $1);
- gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", "xC");
- is_declare("STORE", "xB");
- gen_code("JUMP", gen_label()+5);
- is_declare("LOAD", "xC");
- is_declare("STORE", "xA");
- is_declare("LOAD", $1);
- is_declare("STORE", "xB");
- is_declare("LOAD", "xA");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | ID NEQ ID {
- is_declare("LOAD", $1);
- is_declare("SUB", $3);
- gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
- is_declare("LOAD", $3);
- is_declare("STORE", "xA");
- is_declare("LOAD", $1);
- is_declare("STORE", "xB");
- gen_code("JUMP", gen_label()+5);
- is_declare("LOAD", $1);
- is_declare("STORE", "xA");
- is_declare("LOAD", $3);
- is_declare("STORE", "xB");
- is_declare("LOAD", "xA");
- is_declare("SUB", "xB");
- gen_code("JGE", gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- /* Tutaj if */
- }
- | NUM LT NUM {
- if($1 < $3)
- gen_code("ZERO", -1);
- else
- is_declare("LOAD", "#jeden");
- }
- | NUM LT ID {;
- is_declare("LOAD", $3);
- gen_code("SUBC", $1);
- gen_code("JGE", gen_label()+3); /* Większe */
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- /* if */
- }
- | ID LT NUM {
- gen_code("ZERO", -1);
- gen_code("ADDC", $3);
- is_declare("SUB", $1);
- gen_code("JGE", gen_label()+3); /* Większe */
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | ID LT ID {
- is_declare("LOAD", $3);
- is_declare("SUB", $1);
- gen_code("JGE", gen_label()+3); /* Większe */
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | NUM GT NUM {
- if($1 > $3)
- gen_code("ZERO", -1);
- else
- is_declare("LOAD", "#jeden");
- }
- | NUM GT ID {
- gen_code("ZERO", -1);
- gen_code("ADDC", $1);
- is_declare("SUB", $3);
- gen_code("JGE", gen_label()+3); /* Większe */
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | ID GT NUM {
- is_declare("LOAD", $1);
- gen_code("SUBC", $3);
- gen_code("JGE", gen_label()+3); /* Większe */
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | ID GT ID {
- is_declare("LOAD", $1);
- is_declare("SUB", $3);
- gen_code("JGE", gen_label()+3); /* Większe */
- is_declare("LOAD", "#jeden");
- gen_code("JUMP", gen_label()+2);
- gen_code("ZERO", -1);
- }
- | NUM LTEQ NUM {
- if($1 <= $3)
- gen_code("ZERO", -1);
- else
- is_declare("LOAD", "#jeden");
- }
- | NUM LTEQ ID {
- is_declare("LOAD", $3);
- gen_code("SUBC",$1);
- gen_code("JGE",gen_label()+7);
- gen_code("ZERO",-1);
- gen_code("ADDC",$1);
- is_declare("SUB", $3);
- gen_code("JZ",gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP",gen_label()+2);
- gen_code("ZERO",-1);
- }
- | ID LTEQ NUM {
- gen_code("ZERO",-1);
- gen_code("ADDC",$3);
- is_declare("SUB",$1);
- gen_code("JGE",gen_label()+6);
- is_declare("LOAD", $1);
- gen_code("SUBC", $3);
- gen_code("JZ",gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP",gen_label()+2);
- gen_code("ZERO",-1);
- }
- | ID LTEQ ID {
- is_declare("LOAD", $3);
- is_declare("SUB",$1);
- gen_code("JGE",gen_label()+6);
- is_declare("LOAD", $1);
- is_declare("SUB", $3);
- gen_code("JZ",gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP",gen_label()+2);
- gen_code("ZERO",-1);
- }
- | NUM GTEQ NUM {
- if($1 >= $3)
- gen_code("ZERO", -1);
- else
- is_declare("LOAD", "#jeden");
- }
- | NUM GTEQ ID {
- gen_code("ZERO",-1);
- gen_code("ADDC",$1);
- is_declare("SUB",$3);
- gen_code("JGE",gen_label()+6);
- is_declare("LOAD", $3);
- gen_code("SUBC", $1);
- gen_code("JZ",gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP",gen_label()+2);
- gen_code("ZERO",-1);
- }
- | ID GTEQ NUM {
- is_declare("LOAD", $1);
- gen_code("SUBC",$3);
- gen_code("JGE",gen_label()+7);
- gen_code("ZERO",-1);
- gen_code("ADDC",$3);
- is_declare("SUB", $1);
- gen_code("JZ",gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP",gen_label()+2);
- gen_code("ZERO",-1);
- }
- | ID GTEQ ID {
- is_declare("LOAD", $1);
- is_declare("SUB",$3);
- gen_code("JGE",gen_label()+6);
- is_declare("LOAD", $3);
- is_declare("SUB", $1);
- gen_code("JZ",gen_label()+3);
- is_declare("LOAD", "#jeden");
- gen_code("JUMP",gen_label()+2);
- gen_code("ZERO",-1);
- }
- ;
- %%
- /* Odpalanie parsera */
- int main(int argc, char *argv[])
- {
- extern FILE *yyin;
- yyin = fopen(argv[1], "r");
- errors = 0;
- dec("#print0"); /* Wyświetlanie */
- dec("#jeden");
- dec("xA");
- dec("xB");
- dec("xC");
- dec("xD");
- gen_code("ADDC", 1);
- is_declare("STORE", "#jeden");
- extern int yydebug;
- yydebug = 0;
- yyparse();
- if(errors == 0)
- {
- printCom();
- }
- return 0;
- }
- yyerror(char *s)
- {
- errors++;
- printf("%s\n", s);
- }
Add Comment
Please, Sign In to add comment