From fc5dfb7744d10e9ce5c0d2d9b16e3e485685cc00 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 23 Apr 2018 22:48:59 -0400 Subject: [PATCH] move C code out of parser.y to parser.c --- src/parser/parser.c | 30 ++++++++++++++++++++++++++++++ src/parser/parser.h | 2 ++ src/parser/parser.l | 1 - src/parser/parser.y | 28 +--------------------------- 4 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 src/parser/parser.c diff --git a/src/parser/parser.c b/src/parser/parser.c new file mode 100644 index 0000000..e3f71cc --- /dev/null +++ b/src/parser/parser.c @@ -0,0 +1,30 @@ +#include "parser.h" +#include + +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); +} diff --git a/src/parser/parser.h b/src/parser/parser.h index 4caebcb..00ebd86 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -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 diff --git a/src/parser/parser.l b/src/parser/parser.l index ceee885..04fda0f 100644 --- a/src/parser/parser.l +++ b/src/parser/parser.l @@ -4,7 +4,6 @@ %{ #include "parser.h" -#include "parser.tab.h" #include "String.h" #include #include "Node.h" diff --git a/src/parser/parser.y b/src/parser/parser.y index b4e6fac..0ff51e9 100644 --- a/src/parser/parser.y +++ b/src/parser/parser.y @@ -2,13 +2,10 @@ #include #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; - } -}