Any help with this would be greatly appreciated! I can't figure out where this bug is coming from.
UPDATE 19 MAR:
Here's a more minimal example of the same issue:
scanner.l:
%{ #include <cstlib>
#include "parser.hpp"
%}
%option noyywrap
%%
"BEGIN|begin" { return TKBEGIN; }
"END|end" { return TKEND; }
"RETURN|return" { return TKRETURN;}
";" { return TKSEMICOLON; }
"\(" { return TKOPAREN; }
[ \n\t] {}
"[a-zA-Z][a-zA-Z0-9_]*" { return TKID; }
"(0|0x|[1-9])[0-9]+" { return TKNUMBER; }
"\'.\'" {return TKCHAR;}
"\"[^\"]*" {return TKSTRING;}
"." { fprintf(stderr, "Unexpected character %c (%d)\n", *yytext, *yytext); }
%%
parser.y:
%{
#include <iostream>
extern int yylex();
void yyerror(const char*);
%}
%define parse.error verbose
%token TKID
%token TKNUMBER
%token TKCHAR
%token TKSTRING
%token TKBEGIN
%token TKEND
%token TKRETURN
%token TKSEMICOLON
%token TKOPAREN
%%
Block: TKBEGIN StatementSequence TKEND {}
;
StatementSequence: StatementSequence TKSEMICOLON Statement {}
| Statement {}
;
Statement: ReturnStatement {}
;
ReturnStatement: TKRETURN OptExpression {}
;
OptExpression: TKOPAREN {}
;
%%
void yyerror(const char* msg){
std::cerr << msg << std::endl;
}
main.cpp:
extern int yyparse();
int main()
{
yyparse();
};
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(compiler)
find_package(BISON)
find_package(FLEX)
bison_target(Parser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp COMPILE_FLAGS -t )
flex_target(Scanner scanner.l ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp )
add_flex_bison_dependency(Scanner Parser)
set(EXTRA_COMPILE_FLAGS "-g3 -std=c++14")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_COMPILE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
set(comp_srcs
main.cpp
${BISON_Parser_OUTPUTS}
${FLEX_Scanner_OUTPUTS}
)
source_group("Comp" FILES ${comp_srcs})
add_executable(compiler ${comp_srcs})
target_link_libraries(compiler ${BISON_LIBRARIES})
target_include_directories(compiler PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
return.cpsl:
begin
return (
end
To build it, I run mkdir build; cd build/; cmake ..
and then I run it using ./compiler < ../return.cpsl
.
What it gives back is: beginreturnsyntax error, unexpected TKOPAREN, expecting TKBEGIN
.
It seems to me like there are at least two problems: 1. It doesn't like punctuation for some reason 2. It doesn't understand that it's in the middle of a Block already. Am I right there? Any ideas where this is originating?
"BEGIN|begin"
matches the string BEGIN|begin
. That's what the quotes mean: match exactly this string.
If you want to match either BEGIN
or begin
, the pattern you want is BEGIN|begin
.