我想制作一个程序,为函数创建解析树。例如:“f(g(x,h(y),v,k(l(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。
我应该改变什么才能使这些东西起作用?