|
|
D [0-9]
|
D [0-9]
|
L [A-Za-z_]
|
L [A-Za-z_]
|
LD [0-9A-Za-z_]
|
LD [0-9A-Za-z_]
|
ID {L}{LD}*
|
ID {L}{LD}*
|
HX [0-9A-Fa-f]
|
HX [0-9A-Fa-f]
|
|
|
%{
|
%{
|
#include
|
|
#include
|
#include
|
|
#include
|
|
#include
|
|
|
class Opcode;
|
class Opcode;
|
class Operand;
|
class Operand;
|
class Expression;
|
class Expression;
|
|
|
#include "assembler_bison.hh"
|
#include "assembler_bison.hh"
|
|
|
void count();
|
void count();
|
int show_token(int op, const char * txt, YYSTYPE & lval);
|
int show_token(int op, const char * txt, YYSTYPE & lval);
|
#define token(x) show_token(x, yytext, yylval)
|
#define token(x) show_token(x, yytext, yylval)
|
|
|
%}
|
%}
|
|
|
%%
|
%%
|
";".* { count(); return token(EOL ); }
|
";".* { count(); return token(EOL ); }
|
" " { count(); }
|
" " { count(); }
|
"\n" { count(); return token(EOL ); }
|
"\n" { count(); return token(EOL ); }
|
|
|
".BYTE" { count(); return token(_BYTE ); }
|
".BYTE" { count(); return token(_BYTE ); }
|
".WORD" { count(); return token(_WORD ); }
|
".WORD" { count(); return token(_WORD ); }
|
".OFFSET" { count(); return token(_OFFSET); }
|
".OFFSET" { count(); return token(_OFFSET); }
|
".EXTERN" { count(); return token(_EXTERN); }
|
".EXTERN" { count(); return token(_EXTERN); }
|
".STATIC" { count(); return token(_STATIC); }
|
".STATIC" { count(); return token(_STATIC); }
|
ADD { count(); return token(ADD ); }
|
ADD { count(); return token(ADD ); }
|
AND { count(); return token(AND ); }
|
AND { count(); return token(AND ); }
|
CALL { count(); return token(CALL ); }
|
CALL { count(); return token(CALL ); }
|
CLRB { count(); return token(CLRB ); }
|
CLRB { count(); return token(CLRB ); }
|
CLRW { count(); return token(CLRW ); }
|
CLRW { count(); return token(CLRW ); }
|
DI { count(); return token(DI ); }
|
DI { count(); return token(DI ); }
|
DIV_IS { count(); return token(DIV_IS ); }
|
DIV_IS { count(); return token(DIV_IS ); }
|
DIV_IU { count(); return token(DIV_IU ); }
|
DIV_IU { count(); return token(DIV_IU ); }
|
EI { count(); return token(EI ); }
|
EI { count(); return token(EI ); }
|
HALT { count(); return token(HALT ); }
|
HALT { count(); return token(HALT ); }
|
IN { count(); return token(IN ); }
|
IN { count(); return token(IN ); }
|
JMP { count(); return token(JMP ); }
|
JMP { count(); return token(JMP ); }
|
LEA { count(); return token(LEA ); }
|
LEA { count(); return token(LEA ); }
|
LNOT { count(); return token(LNOT ); }
|
LNOT { count(); return token(LNOT ); }
|
MD_STP { count(); return token(MD_STP ); }
|
MD_STP { count(); return token(MD_STP ); }
|
MD_FIN { count(); return token(MD_FIN ); }
|
MD_FIN { count(); return token(MD_FIN ); }
|
MOD_FIN { count(); return token(MOD_FIN); }
|
MOD_FIN { count(); return token(MOD_FIN); }
|
MUL_IS { count(); return token(MUL_IS ); }
|
MUL_IS { count(); return token(MUL_IS ); }
|
MUL_IU { count(); return token(MUL_IU ); }
|
MUL_IU { count(); return token(MUL_IU ); }
|
MOVE { count(); return token(MOVE ); }
|
MOVE { count(); return token(MOVE ); }
|
NEG { count(); return token(NEG ); }
|
NEG { count(); return token(NEG ); }
|
NOT { count(); return token(NOT ); }
|
NOT { count(); return token(NOT ); }
|
NOP { count(); return token(NOP ); }
|
NOP { count(); return token(NOP ); }
|
OUT { count(); return token(OUT ); }
|
OUT { count(); return token(OUT ); }
|
OR { count(); return token(OR ); }
|
OR { count(); return token(OR ); }
|
RET { count(); return token(RET ); }
|
RET { count(); return token(RET ); }
|
RETI { count(); return token(RETI ); }
|
RETI { count(); return token(RETI ); }
|
SEQ { count(); return token(SEQ ); }
|
SEQ { count(); return token(SEQ ); }
|
SGE { count(); return token(SGE ); }
|
SGE { count(); return token(SGE ); }
|
SGT { count(); return token(SGT ); }
|
SGT { count(); return token(SGT ); }
|
SLE { count(); return token(SLE ); }
|
SLE { count(); return token(SLE ); }
|
SLT { count(); return token(SLT ); }
|
SLT { count(); return token(SLT ); }
|
SNE { count(); return token(SNE ); }
|
SNE { count(); return token(SNE ); }
|
SHS { count(); return token(SHS ); }
|
SHS { count(); return token(SHS ); }
|
SHI { count(); return token(SHI ); }
|
SHI { count(); return token(SHI ); }
|
SLS { count(); return token(SLS ); }
|
SLS { count(); return token(SLS ); }
|
SLO { count(); return token(SLO ); }
|
SLO { count(); return token(SLO ); }
|
LSL { count(); return token(LSL ); }
|
LSL { count(); return token(LSL ); }
|
LSR { count(); return token(LSR ); }
|
LSR { count(); return token(LSR ); }
|
ASR { count(); return token(ASR ); }
|
ASR { count(); return token(ASR ); }
|
SUB { count(); return token(SUB ); }
|
SUB { count(); return token(SUB ); }
|
XOR { count(); return token(XOR ); }
|
XOR { count(); return token(XOR ); }
|
|
|
RR { count(); return token(_RR ); }
|
RR { count(); return token(_RR ); }
|
R { count(); return token(_R ); }
|
R { count(); return token(_R ); }
|
RS { count(); return token(_RS ); }
|
RS { count(); return token(_RS ); }
|
RU { count(); return token(_RU ); }
|
RU { count(); return token(_RU ); }
|
RRZ { count(); return token(_RRZ ); }
|
RRZ { count(); return token(_RRZ ); }
|
RRNZ { count(); return token(_RRNZ ); }
|
RRNZ { count(); return token(_RRNZ ); }
|
|
|
LL { count(); return token(_LL ); }
|
LL { count(); return token(_LL ); }
|
L { count(); return token(_L ); }
|
L { count(); return token(_L ); }
|
LS { count(); return token(_LS ); }
|
LS { count(); return token(_LS ); }
|
LU { count(); return token(_LU ); }
|
LU { count(); return token(_LU ); }
|
|
|
SP { count(); return token(_SP ); }
|
SP { count(); return token(_SP ); }
|
|
|
[#,:+=()*/-] { count(); return token(*yytext); }
|
[#,:+=()*/-] { count(); return token(*yytext); }
|
|
|
0[xX]{HX}+ { count();
|
0[xX]{HX}+ { count();
|
sscanf(yytext + 2, "%X", &yylval._num);
|
sscanf(yytext + 2, "%X", &yylval._num);
|
return token(INT ); }
|
return token(INT ); }
|
|
|
"-"0[xX]{HX}+ { count();
|
"-"0[xX]{HX}+ { count();
|
sscanf(yytext + 3, "%X", &yylval._num);
|
sscanf(yytext + 3, "%X", &yylval._num);
|
yylval._num = -yylval._num;
|
yylval._num = -yylval._num;
|
return token(INT ); }
|
return token(INT ); }
|
|
|
{D}+ { count();
|
{D}+ { count();
|
sscanf(yytext, "%d", &yylval._num);
|
sscanf(yytext, "%d", &yylval._num);
|
return token(INT ); }
|
return token(INT ); }
|
|
|
"-"{D}+ { count();
|
"-"{D}+ { count();
|
sscanf(yytext + 1, "%d", &yylval._num);
|
sscanf(yytext + 1, "%d", &yylval._num);
|
yylval._num = -yylval._num;
|
yylval._num = -yylval._num;
|
return token(INT ); }
|
return token(INT ); }
|
|
|
{ID} { count(); return token(IDENT); }
|
{ID} { count(); return token(IDENT); }
|
|
|
[ \t\v\n\f] { count(); }
|
[ \t\v\n\f] { count(); }
|
|
|
<> { return EOFILE; }
|
<> { return EOFILE; }
|
. { return ERROR; }
|
. { return ERROR; }
|
|
|
%%
|
%%
|
|
|
int yywrap()
|
int yywrap()
|
{
|
{
|
return(1);
|
return(1);
|
}
|
}
|
|
|
int column = 0;
|
int column = 0;
|
int row = 1;
|
int row = 1;
|
|
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
void count()
|
void count()
|
{
|
{
|
for (int i = 0; yytext[i]; i++)
|
for (int i = 0; yytext[i]; i++)
|
{
|
{
|
if (yytext[i] == '\n') { column = 0; row++; }
|
if (yytext[i] == '\n') { column = 0; row++; }
|
else if (yytext[i] == '\t') column += 8 - (column % 8);
|
else if (yytext[i] == '\t') column += 8 - (column % 8);
|
else column++;
|
else column++;
|
}
|
}
|
}
|
}
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
int yyerror(const char *s)
|
int yyerror(const char *s)
|
{
|
{
|
printf("\n%s Line %d Col %d\n", s, row, column);
|
printf("\n%s Line %d Col %d\n", s, row, column);
|
fflush(stdout);
|
fflush(stdout);
|
}
|
}
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
bool did_crlf = false;
|
bool did_crlf = false;
|
bool show_it = false;
|
bool show_it = false;
|
|
|
int show_token(int op, const char * txt, YYSTYPE & lval)
|
int show_token(int op, const char * txt, YYSTYPE & lval)
|
{
|
{
|
if (op == INT)
|
if (op == INT)
|
{
|
{
|
did_crlf = false;
|
did_crlf = false;
|
if (show_it) fprintf(stderr, "Token %3d : %s\n", op, txt);
|
if (show_it) fprintf(stderr, "Token %3d : %s\n", op, txt);
|
}
|
}
|
else if (op == EOL)
|
else if (op == EOL)
|
{
|
{
|
lval._txt = 0;
|
lval._txt = 0;
|
if (!did_crlf && show_it) fprintf(stderr, "\n");
|
if (!did_crlf && show_it) fprintf(stderr, "\n");
|
did_crlf = true;
|
did_crlf = true;
|
}
|
}
|
else
|
else
|
{
|
{
|
assert(txt);
|
assert(txt);
|
did_crlf = false;
|
did_crlf = false;
|
if (show_it) fprintf(stderr, "Token %3d : %s\n", op, txt);
|
if (show_it) fprintf(stderr, "Token %3d : %s\n", op, txt);
|
char * cp = new char[strlen(txt) + 1];
|
char * cp = new char[strlen(txt) + 1];
|
strcpy(cp, txt);
|
strcpy(cp, txt);
|
lval._txt = cp;
|
lval._txt = cp;
|
}
|
}
|
return op;
|
return op;
|
}
|
}
|
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
|
|