yyparse()仅在某些版本的gcc/bison/flex中未在Bison/Flex C ++项目中声明 [英] yyparse() not declared in Bison/Flex C++ project only for some version of gcc/bison/flex

查看:140
本文介绍了yyparse()仅在某些版本的gcc/bison/flex中未在Bison/Flex C ++项目中声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Bison和Flex的新手.我知道这些工具是为在C中使用而设计的,我感到所有问题都来自在C ++中使用它们.我不确定我是否做对了.

该代码在我的计算机上可以正常编译,但在我的大学服务器上则无法编译.我已经隔离了要在此处发布的问题.

在我的大学:

$ (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

在家:

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

我使用以下命令进行编译: bison -d parse.y && flex lex.l && g++ main.cpp lex.yy.c parse.tab.c -lfl

正如我已经说过的,它可以在我的计算机上正常编译(没有警告),但是我可以在服务器上得到它:

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

由于SO在方括号中存在问题,因此我还上传了一个tarball .

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

%{
#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

#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

#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();
}

主题: flex不是GNU软件.

解决方案

您可以添加

 extern "C" int yyparse (void);

在您的main.cpp文件中(也可能在parser.y中)或某些常见的#include -d头文件中.

您确实应该使用g++ -Wall -g来编译代码.

First of all I'm pretty new to Bison and 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++. 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

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.

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

%{
#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

#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

#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.

解决方案

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.

And you really should use g++ -Wall -g to compile your code.

这篇关于yyparse()仅在某些版本的gcc/bison/flex中未在Bison/Flex C ++项目中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆