简体   繁体   English

来自C ++中Lemon生成的解析器的分段错误

[英]Segmentation fault from Lemon-generated parser in C++

I am trying to figure out the Lemon parser generator, so I wrote a little test to help myself fully understand. 我试图找出Lemon解析器生成器,所以我写了一点测试以帮助自己完全理解。 The files generate without a problem and the compiler does not complain, but when I try to run it I get a Segmentation Fault. 文件生成没有问题,编译器也没有抱怨,但是当我尝试运行它时,出现了分段错误。 What am I doing wrong? 我究竟做错了什么?

lexicon.l: lexicon.l:

%{
#include "grammar.h"
%}

%option noyywrap

%%

[A-Za-z_][A-Za-z0-9]*   return IDENTIFIER;
\".*\"                  return STRING;
[0-9]+                  return NUMBER;
"="                     return EQUALS;
\n                      return NEWLINE;
%%

grammar.y: 语法:

%include {
#include <vector>
#include <iostream>
#include <cassert>
#include <sstream>
#include "AST.h"
}

%token_type {char *}
%extra_argument {std::vector<Identifier>& identifiers}

start ::= assignments. {
    std::cout << "start resolved" << std::endl;
}

assignments ::= assignment.
{
    std::cout << "assignments resolved" << std::endl;
}
assignments ::= assignments NEWLINE assignment.

assignment ::= IDENTIFIER(id) EQUALS STRING(str).
{
    std::cout << "I get here too" << std::endl;
    identifiers.push_back(Identifier(id, str));
}

assignment ::= IDENTIFIER(id) EQUALS NUMBER(num).
{
    std::stringstream ss;
    ss << num;
    identifiers.push_back(Identifier(id, ss.str()));
}

main.cpp: main.cpp中:

#include "AST.h"

using namespace std;

void* ParseAlloc(void* (*allocProc)(size_t));
void Parse(void*, int, char *, vector<Identifier>&);
void ParseFree(void*, void(*freeProc)(void*));

int yylex();
extern char * yytext;

int main() {
    vector<Identifier> identifiers;
    void* parser = ParseAlloc(malloc);
    cout << "Line 20" << endl;
    while (int lexcode = yylex()) {
        cout << "Getting in the loop: " << yytext << endl;
        Parse(parser, lexcode, yytext, identifiers);
    }
    Parse(parser, 0, NULL, identifiers);
    cout << "Line 25" << endl;
    ParseFree(parser, free);

    return 0;
}

AST.h: AST.h:

#include <string>
#include <iostream>

class Identifier {
    std::string name;
    std::string value;
public:
    Identifier(std::string _name, std::string _value)
    : name(_name),
      value(_value) {
        std::cout << "Initializing " << name << " as " << value << std::endl;
    }

    std::string getName();
    std::string getValue();
    void setValue(std::string _value);
};

AST.cpp: AST.cpp:

#include "AST.h"

std::string Identifier::getName() {
    return name;
}

std::string Identifier::getValue() {
    return value;
}

void Identifier::setValue(std::string _value) {
    value = _value;
}

And finally the test input: 最后是测试输入:

alpha = "Hello"
beta = "World"
gamma = 15

And the output: 并输出:

mikidep@mikidep-virtual:~/Scrivania/bison test$ cat text | ./parserLine 20
Getting in the loop: alpha
Segmentation Fault

Use a pointer to represent the std::vector of identifiers. 使用指针表示标识符的std::vector I am not sure how it did not occur in a compilation error, but somewhere in the code it will try to do attribution to a variable of the type std::vector<Identifier>& . 我不确定在编译错误中是怎么发生的,但是在代码中的某处它将尝试归因于std::vector<Identifier>&类型的变量。 If I recall correctly, you can not make attribution to references. 如果我没记错的话,您将无法引用参考文献。

So, changing to std::vector<Identifier>* solves your problem. 因此,更改为std::vector<Identifier>*可解决您的问题。

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

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