fixed some rules + Makefile

This commit is contained in:
Sam Hadow 2024-03-27 19:37:17 +01:00
parent dff4dfd0e3
commit 8f722d7896
4 changed files with 74 additions and 20 deletions

22
src/Makefile Normal file
View File

@ -0,0 +1,22 @@
LDFLAGS="-L/usr/local/opt/flex/lib"
machine.py: ram
./ram < test.1
cat machine.py
ram: ram.yy.c ram.tab.c
gcc -o $@ $^ -ly -lfl
ram.tab.c: ram.y
bison -d --report=all $^
ram.yy.c: ram.l
flex -o $@ $^
clean:
rm -f machine.py
rm -f ram
rm -f *.o
rm -f *.output
rm -f *.tab.c *.tab.h
rm -f *.yy.c

View File

@ -9,14 +9,14 @@
%% %%
[rio][0-9]* { yylval = yytext; return REGISTER;} [rio][0-9]* { yylval = yytext; return REGISTER;}
[rio]@[rio][0-9]* { yylval = yytext; return REGISTER_REF} [rio]@[rio][0-9]* { yylval = yytext; return REGISTER_REF;}
[0-9]* { yylval = yytext; return VALUE;} [0-9]* { yylval = yytext; return VALUE;}
ADD|SUB|MULT|DIV { yylval = yytext; return OP;} ADD|SUB|MULT|DIV { yylval = yytext; return OP;}
JUMP|JE|JL { yylval = yytext; return OP_CTRL;} JUMP|JE|JL { yylval = yytext; return OP_CTRL;}
, {return COMMA} [ ]*,[ ]* {return COMMA;}
\( {return PAR_O;} \( {return PAR_O;}
\) {return PAR_C;} \) {return PAR_C;}
\n {return SEPARATION; } \n {return SEPARATION; }
. {printf("token inconnu\n"); yyterminate();} . {printf("token inconnu\n"); yyterminate();}
%% %%

View File

@ -13,7 +13,8 @@ int yyerror(char* s);
%} %}
%token REGISTER REGISTER_REF OP OP_CTRL VALUE %token VALUE
%token REGISTER REGISTER_REF OP OP_CTRL
%token PAR_O PAR_C %token PAR_O PAR_C
%left COMMA %left COMMA
%right SEPARATION %right SEPARATION
@ -21,8 +22,10 @@ int yyerror(char* s);
%% %%
program: program:
input SEPARATION instruction { input SEPARATION instruction {
asprintf(&result, "input = [%s]\ninstructions = [ %s]\n", $1, $3);
} }
| instruction { | instruction {
asprintf(&result, "input = []\ninstructions = [ %s]\n", $1);
} }
; ;
@ -33,8 +36,10 @@ input:
; ;
content: content:
VALUE COMMA content { content COMMA VALUE {
asprintf(&$$, "%s, %s ", $1, $3); char* tmp = "";
asprintf(&tmp, "%s, %s", $1, $3);
$$ = tmp;
} }
| VALUE { | VALUE {
$$ = $1; $$ = $1;
@ -43,7 +48,9 @@ content:
instruction: instruction:
line SEPARATION instruction { line SEPARATION instruction {
asprintf(&$$, "%s%s", $1, $3); char* tmp = "";
asprintf(&tmp, "%s%s", $1, $3);
$$ = tmp;
} }
| line SEPARATION { | line SEPARATION {
$$ = $1; $$ = $1;
@ -55,15 +62,21 @@ instruction:
line: line:
OP_CTRL PAR_O args_ctrl PAR_C { OP_CTRL PAR_O args_ctrl PAR_C {
asprintf(&$$, "{\"op\": Ram.op, \"args\": (%s, %s)}, ", $1, $2); char* tmp = "";
asprintf(&tmp, "{\"op\": Ram.op_ctrl, \"args\": ('%s', (%s))},\n", $1, $3);
$$ = tmp;
} }
| OP PAR_O args PAR_C { | OP PAR_O args PAR_C {
asprintf(&$$, "{\"op\": Ram.op_ctrl, \"args\": (%s, %s)}, ", $1, $2); char* tmp = "";
asprintf(&tmp, "{\"op\": Ram.op, \"args\": ('%s', (%s))},\n", $1, $3);
$$ = tmp;
} }
; ;
args: args:
arg COMMA arg COMMA arg { arg COMMA arg COMMA arg {
asprintf(&$$, "%s, %s, %s", $1, $3, $5); char* tmp = "";
asprintf(&tmp, "%s, %s, %s", $1, $3, $5);
$$ = tmp;
} }
| arg { | arg {
$$ = $1; $$ = $1;
@ -72,7 +85,9 @@ args:
args_ctrl: args_ctrl:
VALUE COMMA arg COMMA arg { VALUE COMMA arg COMMA arg {
asprintf(&$$, "%c, %s, %s", $1, $3, $5); char* tmp = "";
asprintf(&tmp, "%c, %s, %s", $1, $3, $5);
$$ = tmp;
} }
| VALUE { | VALUE {
$$ = $1; $$ = $1;
@ -80,20 +95,26 @@ args_ctrl:
arg: arg:
REGISTER_REF { REGISTER_REF {
char* tmp = "";
char *index_str = strdup($1 + 3); // copy from 4th char char *index_str = strdup($1 + 3); // copy from 4th char
asprintf(&$$, "('%c', '%c', '%c', %s), ", $1[1], index_str, $1[2], $1[0]); asprintf(&tmp, "('%c', '%c', '%c', %s)", $1[1], index_str, $1[2], $1[0]);
// r@i1 -> r register, index in i1 // r@i1 -> r register, index in i1
// Python args: type_register, index, ref_origin, ref_target // Python args: type_register, index, ref_origin, ref_target
// r@i1 -> ('@', '1', 'i', 'r') // r@i1 -> ('@', '1', 'i', 'r')
free(index_str); free(index_str);
$$ = tmp;
} }
| REGISTER { | REGISTER {
char* tmp = "";
char *index_str = strdup($1 + 1); // copy from 2nd char char *index_str = strdup($1 + 1); // copy from 2nd char
asprintf(&$$, "('%c', %s), ", $1[0], index_str); asprintf(&tmp, "('%c', %s)", $1[0], index_str);
free(index_str); free(index_str);
$$ = tmp;
} }
| VALUE { | VALUE {
asprintf(&$$, "('value', %s), ", $1); char* tmp = "";
asprintf(&tmp, "('value', %s)", $1);
$$ = tmp;
} }
; ;
@ -101,6 +122,14 @@ arg:
int main() { int main() {
yyparse(); yyparse();
FILE *file = fopen("machine.py", "w");
if (file == NULL) {
fprintf(stderr, "Error creating file machine.py\n");
return 1;
} else {
fprintf(file, "%s", result);
fclose(file);
}
return 0; return 0;
} }

View File

@ -1 +1,4 @@
(10,5,1) (10,5,1,2,4)
ADD(i0 ,1 ,r0)
SUB(i1,3,r1)
MULT(r1,r0,o1)