move C code out of parser.y to parser.c

This commit is contained in:
Josh Holtrop 2018-04-23 22:48:59 -04:00
parent 8db0a47fb4
commit fc5dfb7744
4 changed files with 33 additions and 28 deletions

30
src/parser/parser.c Normal file
View File

@ -0,0 +1,30 @@
#include "parser.h"
#include <stdio.h>
extern FILE * yyin;
static const char * input_fname;
void parse(const char * filename)
{
input_fname = filename;
yyin = fopen(filename, "r");
if (yyin == NULL)
{
fprintf(stderr, "Error opening \"%s\"\n", filename);
return;
}
if (yyparse() != 0)
{
fprintf(stderr, "Parsing error\n");
return;
}
}
void handle_parse_error(const char * str, const YYLTYPE * yylloc)
{
fprintf(stderr, "error: %s (line %d, column %d)\n",
str,
yylloc->first_line,
yylloc->first_column);
}

View File

@ -2,9 +2,11 @@
#define PARSER_H
#include "Node.h"
#include "parser.tab.h"
#define YYSTYPE Node *
void parse(const char * filename);
void handle_parse_error(const char * str, const YYLTYPE * yylloc);
#endif

View File

@ -4,7 +4,6 @@
%{
#include "parser.h"
#include "parser.tab.h"
#include "String.h"
#include <stdlib.h>
#include "Node.h"

View File

@ -2,13 +2,10 @@
#include <stdio.h>
#include "parser.h"
#include "parser.tab.h"
#define yyerror(msg) handle_error(msg, &yyloc)
#define yyerror(msg) handle_parse_error(msg, &yyloc)
extern FILE * yyin;
int yylex(YYSTYPE *, YYLTYPE *);
static void handle_error(const char * str, const YYLTYPE * yylloc);
%}
@ -452,26 +449,3 @@ function_definition: declarator declaration_list compound_statement
| declaration_specifiers declarator compound_statement
;
%%
static void handle_error(const char * str, const YYLTYPE * yylloc)
{
fprintf(stderr, "error: %s (line %d, column %d)\n",
str,
yylloc->first_line,
yylloc->first_column);
}
void parse(const char * filename)
{
yyin = fopen(filename, "r");
if (yyin == NULL)
{
fprintf(stderr, "Error opening \"%s\"\n", filename);
return;
}
if (yyparse() != 0)
{
fprintf(stderr, "Parsing error\n");
return;
}
}