Commit 553b4774 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Started some test work on grammar.

parent 98f930b8
...@@ -2,20 +2,18 @@ ...@@ -2,20 +2,18 @@
#include <stdio.h> #include <stdio.h>
#include "y.tab.h" #include "y.tab.h"
%} %}
reg \$[a-zA-Z0-9]+ word [a-zA-Z0-9$_]+
label [a-zA-Z0-9_\.]+
int [0-9]+ int [0-9]+
%% %%
[\n] { return NL; } /* Newline */ [\n] { return NL; } /* Newline */
#.* { yylval.sval = yytext; return COMMENT; } /* Comment */ #.* { yylval.sval = yytext; return COMMENT; } /* Comment */
\..* { yylval.sval = yytext; return DIRECTIVE; } /* Assembly directive */ \..* { yylval.sval = yytext; return DIRECTIVE; } /* Assembly directive */
{label}: { yylval.sval = yytext; return LABEL; } /* Label */ {word}: { yylval.sval = yytext; return LABEL; } /* Label */
{reg} { yylval.sval = yytext; return REG; } /* Registry address */
{int} { yylval.ival = atoi(yytext); return INT; } /* Integer */ {int} { yylval.ival = atoi(yytext); return INT; } /* Integer */
{int}(\({reg}\))? { yylval.sval = yytext; return OFFSET; } /* Registry offset */ {word} { yylval.sval = yytext; return WORD; } /* Label reference / registry address */
[a-z0-9\.]+ { yylval.sval = yytext; return INSTR; } /* Instruction */ {int}(\({word}\))? { yylval.sval = yytext; return OFFSET; } /* Registry offset address */
{label} { yylval.sval = yytext; return REF; } /* Label reference */ [a-z0-9\.]+ { yylval.sval = yytext; return CMD; } /* Command */
[,] { return COMMA; } /* Comma */ [,] { return COMMA; } /* Comma */
[ \t]+ ; /* Ignore whitespace */ [ \t]+ ; /* Ignore whitespace */
%{ %{
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// Instruction types
#define TYPE_COMMENT 0
#define TYPE_DIRECTIVE 1
#define TYPE_LABEL 2
#define TYPE_CMD 3
// Argument types
#define ARG_REG 0
#define ARG_OFFSET 1
typedef struct line {
int type;
const char *name;
int argc;
char **argv;
int *argt;
struct line *prev;
struct line *next;
} line;
void yyerror(char*); void yyerror(char*);
void add_line(int type, const char *name, int argc, char **argv, int *argt);
line *first_line, *last_line;
%} %}
%union { %union {
...@@ -11,22 +37,38 @@ void yyerror(char*); ...@@ -11,22 +37,38 @@ void yyerror(char*);
%token NL COMMA %token NL COMMA
%token <ival> INT %token <ival> INT
%token <sval> COMMENT DIRECTIVE REG LABEL OFFSET INSTR REF %token <sval> COMMENT DIRECTIVE WORD LABEL OFFSET CMD
%start symbol
%type <sval> reg3
%% %%
symb: reg3: "add" | "sub"
symb symb
| COMMENT {printf("Found a comment: %s\n", $1);} command:
| DIRECTIVE {printf("Found a directive: %s\n", $1);} reg3 WORD WORD WORD {
| REG {printf("Found a registry address: %s\n", $1);} char *argv[] = (char **)malloc(3 * sizeof(char *));
| LABEL {printf("Found a label: %s\n", $1);} char *argt[] = (int *)malloc(3 * sizeof(int));
| REF {printf("Found a label reference: %s\n", $1);} argv[0] = $2;
| INT {printf("Found an integer: %d\n", $1);} argv[1] = $3;
| OFFSET {printf("Found an offset registry address: %s\n", $1);} argv[2] = $4;
| INSTR {printf("Found an instruction: %s\n", $1);} argt[2] = argt[1] = argt[0] = ARG_REG;
| COMMA {printf("Found a comma\n");} add_line(TYPE_CMD, (char *)$1, 3, argv, argt);
| NL {printf("Found a newline\n");} }
;
symbol:
symbol symbol
| COMMENT { printf("Found comment: %s\n", $1); }
| DIRECTIVE { printf("Found directive: %s\n", $1); }
| WORD { printf("Found word: %s\n", $1); }
| LABEL { printf("Found label: %s\n", $1); }
| INT { printf("Found integer: %d\n", $1); }
| OFFSET { printf("Found offset registry address: %s\n", $1); }
| CMD { printf("Found command: %s\n", $1); }
| COMMA { printf("Found comma\n"); }
| NL { printf("Found newline\n"); }
; ;
%% %%
...@@ -34,10 +76,9 @@ extern int yylex(); ...@@ -34,10 +76,9 @@ extern int yylex();
extern int yyparse(); extern int yyparse();
extern FILE *yyin; extern FILE *yyin;
main(int argc, const char* argv[]) main(int argc, const char *argv[])
{ {
if (argc < 2) if( argc < 2 ) {
{
printf("No file specified"); printf("No file specified");
exit(-1); exit(-1);
} }
...@@ -45,7 +86,7 @@ main(int argc, const char* argv[]) ...@@ -45,7 +86,7 @@ main(int argc, const char* argv[])
// open a file handle to a particular file: // open a file handle to a particular file:
FILE *myfile = fopen(argv[1], "r"); FILE *myfile = fopen(argv[1], "r");
// make sure it is valid: // make sure it is valid:
if (!myfile) { if( !myfile ) {
printf("Cannot open %s\n", argv[1]); printf("Cannot open %s\n", argv[1]);
return -1; return -1;
} }
...@@ -55,11 +96,28 @@ main(int argc, const char* argv[]) ...@@ -55,11 +96,28 @@ main(int argc, const char* argv[])
// parse through the input until there is no more: // parse through the input until there is no more:
do { do {
yyparse(); yyparse();
} while (!feof(yyin)); } while( !feof(yyin) );
} }
void yyerror(char *s) void yyerror(char *s)
{ {
printf("Found error: %s\n", s); printf("Error: %s\n", s);
exit(-1); exit(1);
}
void add_line(int type, const char *name, int argc, char **argv, int *argt) {
line *l = (line*)malloc(sizeof(line));
l->argc = argc;
l->argv = argv;
l->argt = argt;
if( last_line == NULL ) {
// First line
first_line = last_line = l;
} else {
// Add line to linked list
l->prev = last_line;
last_line = last_line->next = l;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment