Skip to content
Snippets Groups Projects
Commit 1b032096 authored by Taddeüs Kroes's avatar Taddeüs Kroes
Browse files

Updated token formats and added tokes REF, INT instead of ARG.

parent c14e76cf
No related branches found
No related tags found
No related merge requests found
......@@ -2,17 +2,20 @@
#include <stdio.h>
#include "y.tab.h"
%}
arg \$[a-zA-Z0-9\.]+
reg \$[a-zA-Z0-9]+
word [a-zA-Z0-9_\.]+
int [1-9][0-9]*
%%
#[^\n]* { yylval.sval = yytext; return COMMENT; } /* Comment */
[a-zA-Z0-9$\.]+: { yylval.sval = yytext; return LABEL; } /* Label */
{arg} { yylval.sval = yytext; return ARG; } /* Arg of instr */
[0-9]+(\({arg}\))? { yylval.sval = yytext; return ARG; } /* Arg of instr */
\.[^\n]* { yylval.sval = yytext; return DIRECTIVE; } /* Assembly */
[a-z\.]+ { yylval.sval = yytext; return INSTR; } /* Instruction */
[,] { return COMMA; } /* Comma */
[\n] { return NL; } /* New line */
[ \t]+ { ; } /* Ignore whitespace */
[\n] { return NL; } /* Newline */
#.* { yylval.sval = yytext; return COMMENT; } /* Comment */
\..* { yylval.sval = yytext; return DIRECTIVE; } /* Assembly directive */
{reg} { yylval.sval = yytext; return REG; } /* Registry address */
{word}: { yylval.sval = yytext; return LABEL; } /* Label */
{int} { yylval.ival = atoi(yytext); return INT; } /* Integer */
[0-9]+(\({reg}\))? { yylval.sval = yytext; return OFFSET; } /* Registry offset */
[a-z\.]+ { yylval.sval = yytext; return INSTR; } /* Instruction */
{word} { yylval.sval = yytext; return REF; } /* Label reference */
[,] { return COMMA; } /* Comma */
[ \t]+ ; /* Ignore whitespace */
......@@ -4,28 +4,27 @@
void yyerror(char*);
%}
%token COMMA NL
%union {
int ival;
char *sval;
}
%token <sval> LABEL
%token <sval> ARG
%token <sval> INSTR
%token <sval> DIRECTIVE
%token <sval> COMMENT
%token NL COMMA
%token <ival> INT
%token <sval> COMMENT DIRECTIVE REG LABEL OFFSET INSTR REF
%%
symb:
symb symb
| COMMENT {printf("Found a comment: %s\n", $1);}
| DIRECTIVE {printf("Found a directive: %s\n", $1);}
| REG {printf("Found a registry address: %s\n", $1);}
| LABEL {printf("Found a label: %s\n", $1);}
| ARG {printf("Found an argument: %s\n", $1);}
| REF {printf("Found a label reference: %s\n", $1);}
| INT {printf("Found an integer: %d\n", $1);}
| INSTR {printf("Found an instruction: %s\n", $1);}
| DIRECTIVE {printf("Found a directive: %s\n", $1);}
| COMMA {printf("Found a comma\n");}
| COMMENT {printf("Found a comment: %s\n", $1);}
| NL {printf("Found a newline\n");}
;
%%
......@@ -41,7 +40,7 @@ main(int argc, const char* argv[])
printf("No file specified");
exit(-1);
}
// open a file handle to a particular file:
FILE *myfile = fopen(argv[1], "r");
// make sure it is valid:
......@@ -51,7 +50,7 @@ main(int argc, const char* argv[])
}
// set lex to read from it instead of defaulting to STDIN:
yyin = myfile;
// parse through the input until there is no more:
do {
yyparse();
......@@ -62,4 +61,4 @@ void yyerror(char *s)
{
printf("Found error: %s\n", s);
exit(-1);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment