Skip to content
Snippets Groups Projects
Commit 1573e930 authored by Jayke Meijer's avatar Jayke Meijer
Browse files

Added Yacc with makefile etc.

parent c49aaf7d
No related branches found
No related tags found
No related merge requests found
......@@ -4,3 +4,7 @@
*~
*.o
*.s
lex.yy.c
y.tab.c
y.tab.h
parser
CC=gcc
LEX=lex
YACC=yacc
CFLAGS=-ll
parser: lex yacc
$(CC) -c lex.yy.c y.tab.c
$(CC) -o $@ lex.yy.o y.tab.o $(CFLAGS)
lex: lex.l
$(LEX) lex.l
yacc: lex_out.y
$(YACC) -d lex_out.y
all: parser
......@@ -5,12 +5,12 @@
%%
[a-z0-9$._]+: { yylval = yytext; return LABEL; } /* Label */
\$[a-z0-9._] { yylval = yytext; return ARG; } /* Arg of instr */
^[a-z.]+ { yylval = yytext; return INSTR; } /* Instruction */
\.[^\n]* { yylval = yytext; return DIRECTIVE; } /* Assembly */
\, { return COMMA; } /* Comma */
#[^\n]* { yylval = yytext; return COMMENT; } /* Comment */
\n { return NL; } /* New line */
[a-z0-9$._]+: { yylval.sval = yytext; return LABEL; } /* Label */
\$[a-z0-9._] { yylval.sval = yytext; return ARG; } /* Arg of instr */
^[a-z.]+ { yylval.sval = yytext; return INSTR; } /* Instruction */
\.[^\n]* { yylval.sval = yytext; return DIRECTIVE; } /* Assembly */
[,] { return COMMA; } /* Comma */
#[^\n]* { yylval.sval = yytext; return COMMENT; } /* Comment */
[\n] { return NL; } /* New line */
[\s\t]+ { ; } /* Ignore whitespace */
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(char*);
%}
%token COMMA NL
%union {
char *sval;
}
%token <sval> LABEL
%token <sval> ARG
%token <sval> INSTR
%token <sval> DIRECTIVE
%token <sval> COMMENT
%%
symb:
LABEL {printf("Found a label: %s\n", $1);}
| ARG {printf("Found an argument: %s\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");}
;
%%
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main(int argc, const char* argv[])
{
if (argc < 2)
{
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:
if (!myfile) {
printf("Cannot open %s\n", argv[1]);
return -1;
}
// set lex to read from it instead of defaulting to STDIN:
yyin = myfile;
// parse through the input until there is no more:
do {
yyparse();
} while (!feof(yyin));
}
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