简体   繁体   English

yyparse()仅在某些版本的gcc / bison / flex中未在Bison / Flex C ++项目中声明

[英]yyparse() not declared in Bison/Flex C++ project only for some version of gcc/bison/flex

First of all I'm pretty new to Bison and Flex. 首先,我对Bison和Flex都很陌生。 I know those tools were designed to be used in C and I have a feeling all of my problem comes from using them in C++. 我知道这些工具是为C语言设计的,我觉得我的所有问题都来自于在C ++中使用它们。 I'm not sure I've done it the right way. 我不确定我是以正确的方式做到的。

The code compiles fine on my computer but doesn't on my university server. 代码在我的计算机上编译良好,但不在我的大学服务器上。 I've isolated the issue to post here. 我已将此问题隔离到此处。

At my university: 在我的大学:

$ (g++ --version; bison --version; flex --version)|grep '[0-9]\.'
g++ (Debian 4.4.5-8) 4.4.5
bison (GNU Bison) 2.4.1
flex 2.5.35

At home: 在家:

HOME $ (g++ --version; bison --version; flex --version)|grep '[0-9]\.'
g++ (GCC) 4.7.1 20120721 (prerelease)
bison (GNU Bison) 2.6.2
flex 2.5.37

I use the following command to compile: bison -d parse.y && flex lex.l && g++ main.cpp lex.yy.c parse.tab.c -lfl 我使用以下命令编译: bison -d parse.y && flex lex.l && g++ main.cpp lex.yy.c parse.tab.c -lfl

As I already said, it compiles fine (no warnings) on my computer but I get this on the server: 正如我已经说过的,它在我的计算机上编译很好(没有警告),但我在服务器上得到了这个:

main.cpp: In function 'int main()':
main.cpp:28: error: 'yyparse' was not declared in this scope

Since SO has some problem with brackets, I've also uploaded a tarball . 由于SO在括号方面存在一些问题, 我还上传了一个tarball

lex.l lex.l

%{
#include 
#include "dict.hpp"
#include "parse.tab.h"
%}

%%

[0-9]+  yylval.num = atoi(yytext); return NUM;
[a-z]+  yylval.id = dict.id(yytext); return ID;
[:space:]   ;

parse.y parse.y

%{
#include 
#include "dict.hpp"
void yyerror (const char* e) {
    puts(e);
}
int yylex ();
%}

%union{
    uint id;
    int num;    
}

%token ID;
%token NUM;

%%

S : ID NUM S {
        dict.set($1, $2);
    }
|;

dict.hpp dict.hpp

#ifndef _DICT_HPP_
#define _DICT_HPP_
#include 
#include 

typedef std::pair dpair;
typedef unsigned int uint;

class Dict {
    std::vector tab;
public:
    uint id(char* s);
    void set(uint i, int v);
    void print();
};

extern Dict dict;

#endif /* _DICT_HPP_ */

main.cpp main.cpp中

#include <vector>
#include <string>
#include <cstdio>
#include "dict.hpp"
#include "parse.tab.h"

Dict dict;

uint Dict::id (char* s) {
    for(uint i = 0; i < tab.size(); i++)
        if(tab[i].first == s)
            return i;
    tab.push_back(dpair(std::string(s), tab.size()));
    return tab.size()-1;
}

void Dict::set (uint i, int v) {
    tab[i].second = v;
}

void Dict::print () {
    for(uint i = 0; i < tab.size(); i++)
        printf("%20s = %d\n", tab[i].first.c_str(), tab[i].second);
}

int main ()
{
    yyparse();
    dict.print();
}

OFFTOPIC: flex is not a GNU software . OFFTOPIC: flex不是GNU软件

You could add 你可以添加

 extern "C" int yyparse (void);

in your main.cpp file (and perhaps also in parser.y ) or in some common #include -d header file. 在你的main.cpp文件中(也可能在parser.y )或一些常见的#include -d头文件中。

And you really should use g++ -Wall -g to compile your code. 你真的应该使用g++ -Wall -g来编译你的代码。

Maybe this questions will be helpful: Difficulties during the compilation (g++, bison, flex) with yyparse(); 也许这个问题会有所帮助: 使用yyparse()进行编译时遇到的困难(g ++,bison,flex); I had also a problem with yyparse(). 我也遇到了yyparse()的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM