我想製作一個程序,它爲函數創建一個分析樹。例如:「f(g(x,h(y),v,k(l(c))))」可能是一個有效的函數調用。Flex,Bison,C++ - 編譯錯誤
h1.l
%{
#include <iostream>
#include <list>
using namespace std;
#include "h1.tab.hpp"
%}
%option noyywrap
%option c++
%%
[a-z][a-zA-z0-9]* { yylval.s = yytext; return (TERM_ID); }
"(" { return (OP); }
")" { return (CP); }
";" { return (COMMA); }
%%
h1.ypp
%{
#include <list>
#include <string>
#include <iostream>
using namespace std;
extern "C" int yylex();
extern "C" int yyerror(char *p) { cerr << "Error!" << endl; }
struct ts {
string *name;
list<struct ts*> *plist; /* NULL if the sturcture represents a variable, parameter list if the structure represents a function */
};
%}
%union {
struct ts *t;
list<struct ts *> *tl;
char *s;
}
%token <s> TERM_ID
%token OP CP COMMA
%type <tl> termlist
%type <t> term
%%
term : TERM_ID OP termlist CP { $$ = new struct ts(); $$->name = new string($1); $$->plist = $3; }
| TERM_ID { $$ = new struct ts(); $$->name = new string($1); $$->plist = NULL; }
;
termlist : termlist COMMA term { $$ = $1; $$->push_back($3); }
| term { $$ = new list<struct ts*>(); $$->push_back($1); }
;
%%
int main()
{
yyparse();
return 0;
}
編譯:
$ bison -d h1.ypp
$ flex h1.l
$ g++ h1.tab.cpp lex.yy.cc
h1.tab.cpp: In function ‘int yyparse()’:
h1.tab.cpp:1382: warning: deprecated conversion from string constant to ‘char*’
h1.tab.cpp:1528: warning: deprecated conversion from string constant to ‘char*’
Undefined symbols for architecture x86_64:
"_yylex", referenced from:
yyparse() in ccmRHVKn.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
我不知道很多關於這些工具,我也從來沒有使用過CPP。
我應該改變什麼才能使這些東西有效?
問題出在鏈接器上。它無法找到'yylex'函數的定義?你正在編譯它的定義存在的源文件嗎? – Mahesh
flex工具生成yylex()函數。您不得編譯它生成的源代碼。 –