From 015cbbfb9dd7a08355c25b3a3ea93621b2a64c57 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 18 Jun 2021 10:00:04 +0530 Subject: yay it works --- 15/main.l | 4 ++-- 15/main.y | 34 ++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 12 deletions(-) (limited to '15') diff --git a/15/main.l b/15/main.l index ffcb97e..1011cdb 100644 --- a/15/main.l +++ b/15/main.l @@ -9,12 +9,12 @@ extern int yylval; yylval=yytext[0]; return NUM; }; -[a-zA-Z]+? { +[a-zA-Z_][a-zA-Z0-9]* { yylval=yytext[0]; return IDENT; } . return yytext[0]; -\n return 0; +\n return NL; %% int yywrap() { diff --git a/15/main.y b/15/main.y index 46075c2..40b7ba2 100644 --- a/15/main.y +++ b/15/main.y @@ -6,12 +6,15 @@ void apply(char); void load_num(char); -void load_var(char); -void store_var(char); +void load_var(char *); +void store_var(char *); int yyerror(char *); int yylex(); int reg_count=0; + +char cur_str[100]; + struct instruction { char op1; char op2; @@ -19,21 +22,32 @@ struct instruction { }; %} -%token IDENT NUM +%token IDENT NUM NL %type expr %left '+' %left '*''/' %left '-' %% -statement: IDENT '=' expr ';' { store_var((char)$1); } +start: statement NL start + | NL + +statement: IDENT { + strcpy(cur_str, yytext); + } '=' expr { + printf("ST %s, R%d\n", cur_str, reg_count-1); + } ; expr: expr '+' expr {apply('+');} | expr '-' expr {apply('-');} | expr '*' expr {apply('*');} - | NUM {load_num((char)$1); $$=(char)$1;} - | IDENT {load_var((char)$1); $$=(char)$1;} + | NUM { + printf("LD R%d, #%c\n", reg_count++, atoi(yytext)); + } + | IDENT { + printf("LD R%d, %s\n", reg_count++, yytext); + } ; %% @@ -55,12 +69,12 @@ void load_num(char num) { printf("LD R%d, #%c\n", reg_count++, num); } -void load_var(char var) { - printf("LD R%d, %c\n", reg_count++, var); +void load_var(char *var) { + printf("LD R%d, %s\n", reg_count++, var); } -void store_var(char var) { - printf("ST %c, R%d\n", var, reg_count-1); +void store_var(char* var) { + printf("ST %s, R%d\n", var, reg_count-1); } int yyerror(char *s) { -- cgit v1.2.3