C++ yyparse()未在Bison/Flex C++;仅针对某些版本的gcc/bison/flex进行项目

C++ yyparse()未在Bison/Flex C++;仅针对某些版本的gcc/bison/flex进行项目,c++,bison,flex-lexer,C++,Bison,Flex Lexer,首先,我对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

首先,我对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 由于SO在括号中有一些问题

法律

%{ #包括 #包括“dict.hpp” #包括“parse.tab.h” %} %% [0-9]+yylval.num=atoi(yytext);返回NUM; [a-z]+yylval.id=dict.id(yytext);返回ID; [:空格:]; 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” 无效错误(常量字符*e){ (e); } int-yylex(); %} %联合{ 单元id; int-num; } %令牌ID; %令牌数; %% 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_ */ #ifndef_DICT_水电站_ #定义水电站_ #包括 #包括 typedef std::对dpair; typedef无符号整数单元; 课堂口述{ std::向量选项卡; 公众: uint id(字符*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();
}
#包括
#包括
#包括
#包括“dict.hpp”
#包括“parse.tab.h”
Dict-Dict;
uint Dict::id(字符*s){
对于(uint i=0;i
非主题:。

您可以添加

 extern "C" int yyparse (void);
main.cpp
文件中(也可能在
parser.y
中)或在一些常见的
#include
-d头文件中


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

也许这些问题会有帮助:
我对yyparse()也有问题。

在main.cpp和parser.y中都添加了它,谢谢。但我仍然不明白为什么我需要声明这两个都来自C代码,因为我正在编译C++中的所有东西。有人能帮我澄清一下吗?除非你在命令行上做些额外的事情,否则gcc会将
lex.yy.c
编译成一个c文件,因为它的扩展名就是这么说的。同样适用于
parse.tab.c
。我猜如果你试图把它们作为C++来编译,这些文件就不会编译。 #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_ */
#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();
}
 extern "C" int yyparse (void);