【问题标题】:yyparse() not declared in Bison/Flex C++ project only for some version of gcc/bison/flexyyparse() 未在 Bison/Flex C++ 项目中声明,仅适用于某些版本的 gcc/bison/flex
【发布时间】:2012-09-27 22:01:44
【问题描述】:

首先,我对 Bison 和 Flex 还是很陌生。我知道这些工具是为在 C 中使用而设计的,我感觉我所有的问题都来自于在 C++ 中使用它们。我不确定我是否以正确的方式做到了。

代码在我的计算机上编译得很好,但在我的大学服务器上却没有。我已将问题隔离到此处发布。

在我的大学:

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

在家:

HOME $ (g++ --version; bison --version; flex --version)|grep '[0-9]\.' g++ (GCC) 4.7.1 20120721(预发布) 野牛 (GNU Bison) 2.6.2 弹性 2.5.37

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

正如我已经说过的,它在我的计算机上编译得很好(没有警告),但我在服务器上得到了这个:

main.cpp:在函数“int main()”中: main.cpp:28:错误:未在此范围内声明“yyparse”

由于 SO 括号有问题,I've also uploaded a tarball

lex.l

%{ #包括 #include "dict.hpp" #include "parse.tab.h" %} %% [0-9]+ yylval.num = atoi(yytext);返回数字; [a-z]+ yylval.id = dict.id(yytext);返回标识; [:空间:] ;

解析.y

%{ #包括 #include "dict.hpp" 无效 yyerror (const char* e) { 看跌期权(e); } int yylex (); %} %联盟{ 单位标识; 整数; } %令牌ID; %token NUM; %% S : 编号 S { dict.set($1, $2); } |;

dict.hpp

#ifndef _DICT_HPP_ #define _DICT_HPP_ #包括 #包括 typedef std::pair dpair; typedef 无符号 int uint; 类字典{ std::vector 选项卡; 民众: uint id(char* s); 无效集(uint i,int v); 无效打印(); }; 外部字典字典; #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 is not a GNU software.

【问题讨论】:

    标签: c++ bison flex-lexer


    【解决方案1】:

    你可以添加

     extern "C" int yyparse (void);
    

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

    你真的应该使用g++ -Wall -g 来编译你的代码。

    【讨论】:

    • 在 main.cpp 和 parser.y 中添加它使其工作,谢谢。但是我仍然不明白为什么我需要将两者都声明为来自 C 代码,因为我正在用 C++ 编译所有内容。有人可以帮我澄清一下吗?
    • 除非你在命令行上做一些额外的事情,否则 gcc 会将lex.yy.c 编译为 C 文件,因为这就是它的扩展名所说的。 parse.tab.c 也一样。我的猜测是,如果您尝试将它们作为 C++ 进行编译,这些文件将无法编译。
    【解决方案2】:

    也许这个问题会有所帮助:Difficulties during the compilation (g++, bison, flex) with yyparse(); 我也遇到了 yyparse() 的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多