Search code examples
c++compiler-constructionbisonlex

SIGSEGV while creating a parser-tree


I am trying to create a parser-generator using flex/bison. This is my partial parser.y code:

func_definition : type_specifier ID LPAREN parameter_list RPAREN compound_statement
                 {
                    $$=new Symbol_info();
                    $$->code+="PROC:"+ $2->symbol+"\n";

                    if($2->symbol!="main")
                    {
                        $$->code+="PUSH AX\n";
                        $$->code+="PUSH BX\n";
                        $$->code+="PUSH CX\n";
                        $$->code+="PUSH DX\n";
                    }

                    $$->code += $6->code ;

                    if($2->symbol!="main") {
                        $$->code+="POP DX\n";
                        $$->code+="POP CX\n";
                        $$->code+="POP BX\n";
                        $$->code+="POP AX\n";
                    }

                    fprintf(parseLog, "GRAMMER RULE: func_definition -> type_specifier ID LPAREN parameter_list RPAREN compound_statement  \n"); 
                 }
                ;

And this is my partial lex.l code.

{id} {
    Symbol_info *s= new  Symbol_info(yytext, "ID");
    yylval = (YYSTYPE)s;

    return ID;
}

And this is my partial symbol_table.h code

class SymbolInfo{
    string type;
    string symbol;
public:
    string code;
    SymbolInfo *next;


    SymbolInfo(){
        symbol="";
        type="";
        code="";
    }
    SymbolInfo(string symbol, string type){
        this->symbol=symbol;
        this->type=type;
        code="";
    }
    SymbolInfo(char *symbol, char *type){
        this->symbol=string(symbol);
        this->type= string(type);
        code="";
    }

    SymbolInfo(const SymbolInfo *sym){
        symbol=sym->symbol;
        type=sym->type;
        code=sym->code;
    }

So, when I create a program, I get a SIGSEGV segmentation fault. (Address boundary error). It appears that I get that error when I try to access the yylval returned to me by the lex function.


Solution

  • I tried to run this code on an Ubuntu 64-bit instance (Ubuntu 17.10). I don't know why but the same code runs fine on a 32 bit system (Ubuntu 14.10).

    Maybe it's because of the large Integer sizes. Here is the code if you're interested.