草庐IT

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

coder 2024-02-20 原文

首先,我对 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

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

题外话:flex is not a GNU software .

最佳答案

你可以添加

 extern "C" int yyparse (void);

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

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

关于c++ - yyparse() 未在 Bison/Flex C++ 项目中仅针对某些版本的 gcc/bison/flex 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768411/

有关c++ - yyparse() 未在 Bison/Flex C++ 项目中仅针对某些版本的 gcc/bison/flex 声明的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  3. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  4. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  5. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  6. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  7. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  8. += 的 Ruby 方法 - 2

    有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=

  9. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

  10. ruby - 如何使用 method_missing 动态声明方法? - 2

    我有一个ruby​​程序,我想接受用户创建的方法,并使用该名称创建一个新方法。我试过这个:defmethod_missing(meth,*args,&block)name=meth.to_sclass我收到以下错误:`define_method':interningemptystring(ArgumentError)in'method_missing'有什么想法吗?谢谢。编辑:我以不同的方式让它工作,但我仍然很好奇如何以这种方式做到这一点。这是我的代码:defmethod_missing(meth,*args,&block)Adder.class_evaldodefine_method

随机推荐