Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex/Bison: segmentation fault core dump

I'm writing a simple calculator based on .gertrude esolang. What I'm trying to do is parse a text file that contains ratios (in the form of n/m) with flex, than check if the ratio is an index for an operation (+ - / *) or a number and than send the right token to Bison. I get no error when the code are compiled, but when the program is running returns a -segmentation fault core dump - for every kind of input (like 1/2 14/10 1/8 that should be 2 + 8).

Here gertrude.l

%{ 
#include <stdlib.h> 
#include <string.h>
#include <stdio.h>
#include "gertrude.tab.h" 
void yyerror(char *);    

int FrazioneToDecimale(char *str1){
    int num, den;
        unsigned tot;
        char *token;
    char *deli;
            const char del = '/';
            *deli = del;
        token = strtok (str1, deli);
        num = atoi(token);
        token = strtok (NULL, deli);
        den = atoi(token);   
        tot = 1 / (num/den);
    return tot;
}
%}

%% 

/* ratio */ 

"14/10" {
            yylval.sval = '+';
            return SOMMA;
            }

"11/7"  { 
            yylval.sval = '-';
            return SOTTRAZIONE;
            }


"6/16"  {
            yylval.sval = '*';
            return MOLTIPLICAZIONE;
            }

"5/8"   {
            yylval.sval = '/';
            return DIVISIONE;
            }

[0-9]+"/"[0-9]+    {
                            //yylval = *yytext ;
            yylval.ival = FrazioneToDecimale(yytext);
                return NUMERO;
                } 


[ \t] ;


[ \n] { return EOL; };

%% 



int yywrap(void) { 
return 0; 

} 

Here gertrude.y

%{
#include <stdio.h>
#include <string.h>
%}

%union {
int ival;
char sval;
}

%type <ival>  exp fattore termine
%token <ival> NUMERO
%token <sval> SOMMA SOTTRAZIONE MOLTIPLICAZIONE DIVISIONE 
%token EOL 

%% 

istruzione: 
    | istruzione exp EOL { printf("= %d\n", $2); }
    ;

exp: fattore
    | exp SOMMA fattore { $$ = $1 + $3; } 
    | exp SOTTRAZIONE fattore { $$ = $1 - $3; } 
    ;

fattore: termine
       | fattore MOLTIPLICAZIONE termine { $$ = $1 * $3; }
   | fattore DIVISIONE termine { $$ = $1 / $3; }
            ;

termine: NUMERO { $$ = $1; }
       ;

%%
int main(void) {
    yyparse();
}

yyerror(char *s) {
    fprintf(stderr, "error: %s\n\n", s);
}

Thanks in advance for any kind of advice!

like image 788
thebeefeater Avatar asked Oct 16 '13 10:10

thebeefeater


People also ask

Is segmentation fault a core dump?

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump.

What is segmentation fault core dumped in Python?

Segmentation fault is when our system attempts to access any page of memory that does not exist. Core dumped defines when a code part attempts to perform a write and read operation on a free or read-only location. Generally, segfaults are associated with a file named core and happen at the time of up-gradation.

What is segmentation fault stack overflow?

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).


1 Answers

Your code has a problem with pointers and strings. This is a C problem, not a Bison or Flex problem.

Look at these lines from gertrude.l:

char *deli;
const char del = '/';
*deli = del;

Your pointer variable deli is uninitialized and contains garbage, so it might point anywhere. Then you follow that pointer to where it points (anywhere!) and you put a character there. This causes the program to crash. Plus the string (wherever it is) isn't NUL-terminated.

Simply replace those three lines with this line:

 char *deli = "/";
like image 148
Thomas Padron-McCarthy Avatar answered Nov 15 '22 05:11

Thomas Padron-McCarthy