Generating a C++ reentrant parser.

This commit is contained in:
Stefano Sanfilippo 2014-11-28 19:00:51 +01:00
parent e2a635e95f
commit 649797f13a

View File

@ -1,21 +1,37 @@
%{ %{
#define YYERROR_VERBOSE
extern void yyerror(const char *);
extern int yylex();
#include "Nodes.hpp" #include "Nodes.hpp"
using namespace monicelli; using namespace monicelli;
extern Program *program;
%} %}
%skeleton "lalr1.cc"
%require "3.0"
%language "c++"
%defines
%output "Parser.cpp"
%locations
%token-table
%define parse.error verbose
%define api.namespace {monicelli}
%define parser_class_name {Parser}
%lex-param {Scanner &scanner}
%parse-param {Scanner &scanner}
%parse-param {Program &program}
%code requires { %code requires {
#include "Nodes.hpp" #include "Nodes.hpp"
namespace monicelli {
class Scanner;
}
} }
%define api.prefix {mc} %code {
static int yylex(Parser::semantic_type*, Parser::location_type*, Scanner&);
}
%token MAIN %token MAIN
%token RETURN %token RETURN
@ -107,13 +123,13 @@ static std::stack<BranchCaseList*> branchCaseStack;
program: program:
/* epsilon */ /* epsilon */
| fun_decls main fun_decls { | fun_decls main fun_decls {
program->setMain($2); program.setMain($2);
} }
; ;
fun_decls: fun_decls:
/* epsilon */ /* epsilon */
| fun_decl { | fun_decl {
program->addFunction($1); program.addFunction($1);
} }
fun_decls fun_decls
; ;
@ -362,3 +378,14 @@ simple_expression:
; ;
%% %%
#include "Scanner.hpp"
void Parser::error(const location_type& loc, const std::string &message) {
std::cerr << "Error at " << loc.begin.line << ":" << loc.begin.column;
std::cerr << ": " << message << std::endl;
}
static int yylex(Parser::semantic_type *lval, Parser::location_type *loc, Scanner &scanner) {
return scanner.yylex(lval);
}