|
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; |
9 | yylval=yytext[0]; |
9 | yylval=yytext[0]; |
10 | return NUM; |
10 | return NUM; |
11 | }; |
11 | }; |
12 | [a-zA-Z]+? { |
12 | [a-zA-Z_][a-zA-Z0-9]* { |
13 | yylval=yytext[0]; |
13 | yylval=yytext[0]; |
14 | return IDENT; |
14 | return IDENT; |
15 | } |
15 | } |
16 | . return yytext[0]; |
16 | . return yytext[0]; |
17 | \n return 0; |
17 | \n return NL; |
18 | %% |
18 | %% |
19 | |
19 | |
20 | int yywrap() { |
20 | 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 @@ |
6 | |
6 | |
7 | void apply(char); |
7 | void apply(char); |
8 | void load_num(char); |
8 | void load_num(char); |
9 | void load_var(char); |
9 | void load_var(char *); |
10 | void store_var(char); |
10 | void store_var(char *); |
11 | |
11 | |
12 | int yyerror(char *); |
12 | int yyerror(char *); |
13 | int yylex(); |
13 | int yylex(); |
14 | int reg_count=0; |
14 | int reg_count=0; |
| |
15 | |
| |
16 | char cur_str[100]; |
| |
17 | |
15 | struct instruction { |
18 | struct instruction { |
16 | char op1; |
19 | char op1; |
17 | char op2; |
20 | char op2; |
@@ -19,21 +22,32 @@ struct instruction { |
19 | }; |
22 | }; |
20 | %} |
23 | %} |
21 | |
24 | |
22 | %token IDENT NUM |
25 | %token IDENT NUM NL |
23 | %type expr |
26 | %type expr |
24 | %left '+' |
27 | %left '+' |
25 | %left '*''/' |
28 | %left '*''/' |
26 | %left '-' |
29 | %left '-' |
27 | %% |
30 | %% |
28 | |
31 | |
29 | statement: IDENT '=' expr ';' { store_var((char)$1); } |
32 | start: statement NL start |
| |
33 | | NL |
| |
34 | |
| |
35 | statement: IDENT { |
| |
36 | strcpy(cur_str, yytext); |
| |
37 | } '=' expr { |
| |
38 | printf("ST %s, R%d\n", cur_str, reg_count-1); |
| |
39 | } |
30 | ; |
40 | ; |
31 | |
41 | |
32 | expr: expr '+' expr {apply('+');} |
42 | expr: expr '+' expr {apply('+');} |
33 | | expr '-' expr {apply('-');} |
43 | | expr '-' expr {apply('-');} |
34 | | expr '*' expr {apply('*');} |
44 | | expr '*' expr {apply('*');} |
35 | | NUM {load_num((char)$1); $$=(char)$1;} |
45 | | NUM { |
36 | | IDENT {load_var((char)$1); $$=(char)$1;} |
46 | printf("LD R%d, #%c\n", reg_count++, atoi(yytext)); |
| |
47 | } |
| |
48 | | IDENT { |
| |
49 | printf("LD R%d, %s\n", reg_count++, yytext); |
| |
50 | } |
37 | ; |
51 | ; |
38 | |
52 | |
39 | %% |
53 | %% |
@@ -55,12 +69,12 @@ void load_num(char num) { |
55 | printf("LD R%d, #%c\n", reg_count++, num); |
69 | printf("LD R%d, #%c\n", reg_count++, num); |
56 | } |
70 | } |
57 | |
71 | |
58 | void load_var(char var) { |
72 | void load_var(char *var) { |
59 | printf("LD R%d, %c\n", reg_count++, var); |
73 | printf("LD R%d, %s\n", reg_count++, var); |
60 | } |
74 | } |
61 | |
75 | |
62 | void store_var(char var) { |
76 | void store_var(char* var) { |
63 | printf("ST %c, R%d\n", var, reg_count-1); |
77 | printf("ST %s, R%d\n", var, reg_count-1); |
64 | } |
78 | } |
65 | |
79 | |
66 | int yyerror(char *s) { |
80 | int yyerror(char *s) { |
|