2

I am trying to figure out the Lemon parser generator, so I wrote a little test to help myself fully understand. 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:

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

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

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

#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
Michele De Pascalis
  • 932
  • 2
  • 9
  • 26

1 Answers1

0

Use a pointer to represent the std::vector of identifiers. 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>&. If I recall correctly, you can not make attribution to references.

So, changing to std::vector<Identifier>* solves your problem.

André Puel
  • 8,741
  • 9
  • 52
  • 83
  • As you can see from the debug logs the point where the identifiers vector is used isn't even reached. – Michele De Pascalis Feb 02 '15 at 21:03
  • @Glaedr it segfaults when tryting to do `yypParser->identifiers = identifiers`, the left side is of type `std::vector&`, this reference is unitialized so it is pointing to NULL. It will end in `std::vector >::operator= (this=0x0, __x=std::vector of length 0, capacity 0)`, look at the `this=0x0`. I used gdb to make the answer, I did not just guess. – André Puel Feb 02 '15 at 21:57
  • So the weird behavior is due to the different approach pointers and references take when assigning, the latter being subject to operator overloading? – Michele De Pascalis Feb 02 '15 at 23:13