fixed some rules + Makefile
This commit is contained in:
parent
dff4dfd0e3
commit
8f722d7896
22
src/Makefile
Normal file
22
src/Makefile
Normal 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
|
16
src/ram.l
16
src/ram.l
@ -9,14 +9,14 @@
|
||||
|
||||
|
||||
%%
|
||||
[rio][0-9]* { yylval = yytext; return REGISTER;}
|
||||
[rio]@[rio][0-9]* { yylval = yytext; return REGISTER_REF}
|
||||
[0-9]* { yylval = yytext; return VALUE;}
|
||||
[rio][0-9]* { yylval = yytext; return REGISTER;}
|
||||
[rio]@[rio][0-9]* { yylval = yytext; return REGISTER_REF;}
|
||||
[0-9]* { yylval = yytext; return VALUE;}
|
||||
ADD|SUB|MULT|DIV { yylval = yytext; return OP;}
|
||||
JUMP|JE|JL { yylval = yytext; return OP_CTRL;}
|
||||
, {return COMMA}
|
||||
\( {return PAR_O;}
|
||||
\) {return PAR_C;}
|
||||
\n {return SEPARATION; }
|
||||
. {printf("token inconnu\n"); yyterminate();}
|
||||
[ ]*,[ ]* {return COMMA;}
|
||||
\( {return PAR_O;}
|
||||
\) {return PAR_C;}
|
||||
\n {return SEPARATION; }
|
||||
. {printf("token inconnu\n"); yyterminate();}
|
||||
%%
|
||||
|
51
src/ram.y
51
src/ram.y
@ -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
|
||||
%left COMMA
|
||||
%right SEPARATION
|
||||
@ -21,8 +22,10 @@ int yyerror(char* s);
|
||||
%%
|
||||
program:
|
||||
input SEPARATION instruction {
|
||||
asprintf(&result, "input = [%s]\ninstructions = [ %s]\n", $1, $3);
|
||||
}
|
||||
| instruction {
|
||||
asprintf(&result, "input = []\ninstructions = [ %s]\n", $1);
|
||||
}
|
||||
;
|
||||
|
||||
@ -33,8 +36,10 @@ input:
|
||||
;
|
||||
|
||||
content:
|
||||
VALUE COMMA content {
|
||||
asprintf(&$$, "%s, %s ", $1, $3);
|
||||
content COMMA VALUE {
|
||||
char* tmp = "";
|
||||
asprintf(&tmp, "%s, %s", $1, $3);
|
||||
$$ = tmp;
|
||||
}
|
||||
| VALUE {
|
||||
$$ = $1;
|
||||
@ -43,7 +48,9 @@ content:
|
||||
|
||||
instruction:
|
||||
line SEPARATION instruction {
|
||||
asprintf(&$$, "%s%s", $1, $3);
|
||||
char* tmp = "";
|
||||
asprintf(&tmp, "%s%s", $1, $3);
|
||||
$$ = tmp;
|
||||
}
|
||||
| line SEPARATION {
|
||||
$$ = $1;
|
||||
@ -55,15 +62,21 @@ instruction:
|
||||
|
||||
line:
|
||||
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 {
|
||||
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:
|
||||
arg COMMA arg COMMA arg {
|
||||
asprintf(&$$, "%s, %s, %s", $1, $3, $5);
|
||||
char* tmp = "";
|
||||
asprintf(&tmp, "%s, %s, %s", $1, $3, $5);
|
||||
$$ = tmp;
|
||||
}
|
||||
| arg {
|
||||
$$ = $1;
|
||||
@ -72,7 +85,9 @@ args:
|
||||
|
||||
args_ctrl:
|
||||
VALUE COMMA arg COMMA arg {
|
||||
asprintf(&$$, "%c, %s, %s", $1, $3, $5);
|
||||
char* tmp = "";
|
||||
asprintf(&tmp, "%c, %s, %s", $1, $3, $5);
|
||||
$$ = tmp;
|
||||
}
|
||||
| VALUE {
|
||||
$$ = $1;
|
||||
@ -80,20 +95,26 @@ args_ctrl:
|
||||
|
||||
arg:
|
||||
REGISTER_REF {
|
||||
char* tmp = "";
|
||||
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
|
||||
// Python args: type_register, index, ref_origin, ref_target
|
||||
// r@i1 -> ('@', '1', 'i', 'r')
|
||||
free(index_str);
|
||||
$$ = tmp;
|
||||
}
|
||||
| REGISTER {
|
||||
char* tmp = "";
|
||||
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);
|
||||
$$ = tmp;
|
||||
}
|
||||
| VALUE {
|
||||
asprintf(&$$, "('value', %s), ", $1);
|
||||
char* tmp = "";
|
||||
asprintf(&tmp, "('value', %s)", $1);
|
||||
$$ = tmp;
|
||||
}
|
||||
;
|
||||
|
||||
@ -101,6 +122,14 @@ arg:
|
||||
|
||||
int main() {
|
||||
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;
|
||||
}
|
||||
|
@ -1 +1,4 @@
|
||||
(10,5,1)
|
||||
(10,5,1,2,4)
|
||||
ADD(i0 ,1 ,r0)
|
||||
SUB(i1,3,r1)
|
||||
MULT(r1,r0,o1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user