c++ - unresolved external symbol for yyparse() -
my main.cpp looks this:
#include <assert.h> #include <fstream> #include <iostream> #include "astgen.h" #include "astexec.h" extern int yyparse(); extern file *yyin; int main() { //yydebug = 0; struct astelement *a = 0; file *fp ; fopen_s(&fp,"example.txt","r"); //std::cout<<fp; if (fp==null) { std::cout<<"error"; } yyin=fp; yyparse(); assert(a); struct execenviron* e = createenv(); execast(e, a); freeenv(e); /* todo: destroy ast */ }
when try compile this, error: main.obj : error lnk2019: unresolved external symbol "int __cdecl yyparse(void)" (?yyparse@@yahxz) referenced in function _main
i newbie in flex/bison. appreciated. if helps here parser.y file:
%error-verbose /* instruct bison generate verbose error messages*/ %{ #include "astgen.h" #define yydebug 1 /* since parser must return ast, must parameter * ast can stored. type of parameter void*. */ #define yyparse_param astdest extern int yylex(); %} %union { int val; char op; char* name; struct astelement* ast; /* new member store ast elements */ } %token token_begin token_end token_while token_do %token<name> token_id %token<val> token_number %token<op> token_operator %type<ast> program block statements statement assignment expression whilestmt call %start program %{ /* forward declarations */ void yyerror(const char* const message); %} %% program: statement';' { (*(struct astelement**)astdest) = $1; }; block: token_begin statements token_end{ $$ = $2; }; statements: {$$=0;} | statements statement ';' {$$=makestatement($1, $2);} | statements block';' {$$=makestatement($1, $2);}; statement: assignment {$$=$1;} | whilestmt {$$=$1;} | block {$$=$1;} | call {$$=$1;} assignment: token_id '=' expression {$$=makeassignment($1, $3);} expression: token_id {$$=makeexpbyname($1);} | token_number {$$=makeexpbynum($1);} | expression token_operator expression {$$=makeexp($1, $3, $2);} whilestmt: token_while expression token_do statement{$$=makewhile($2, $4);}; call: token_id '(' expression ')' {$$=makecall($1, $3);}; %% #include "astexec.h" #include <stdlib.h> void yyerror(const char* const message) { fprintf(stderr, "parse error:%s\n", message); exit(1); }
edit 1: using winflexbison in vs 2012 environment generate parser , lexer. process build rather simple. required done add <appropriate>_custom_build.targets
build customizations project in vs , build solution. reference documentation can found here @ winflexbison visual studio.
i point out followed same method build sample flex , bison file , later integrated in project. worked fine me.
what doing wrong here?
your bison input includes well-commented obsolete definition [note 1]:
/* since parser must return ast, must parameter * ast can stored. type of parameter void*. */ #define yyparse_param astdest
as comment indicates, cause prototype yyparse
be:
int yyparse(void* astdest);
however, in main
function, you've declared
extern int yyparse();
since compiling c++, yyparse
declared in main.cpp
, invoked main
not same function yyparse
declared in parser.tab.h
, defined in parser.tab.cpp
. consequently, linker error.
had put #include "parser.tab.h"
main.cpp
file instead of manually declaring yyparse
, have seen more easily-understood error message, although can't vouch error messages produced vs 2012.
notes:
yyparse_param
has been deprecated since bison version 1.875, more decade ago, , works yacc-compatible parsers. should use%parse-param
declaration, works bison-produced parsers, , allows specify actual parameter type rather having abandon type safety. see bison manual details.
Comments
Post a Comment