0

I'm trying to implement a simple calculator using Flex and Bison. I'm running into problems in the Bison stage, wherein I can't figure out the way in which the value of the variable can be retrieved from the symbol table and assigned to $$.

The lex file:

%{
#include <iostream>
#include <string.h>
#include "calc.tab.h"
using namespace std;
void Print();

int count = 0;
%}

%%

[ \t\n]+            ;

"print"             {Print();}

"exit"              {
                exit(EXIT_SUCCESS);
                }

[0-9]+              {
                yylval.FLOAT = atof(yytext);
                return (NUMBER);
                count++;
                }

[a-z][_a-zA-Z0-9]*  {
                yylval.NAME = yytext;
                return (ID);
                }



.                   {
                return (int)yytext[0];
                }

%%

void Print()
{
    cout << "Printing ST..\n";
}

int yywrap()
{
    return 0;
}

The Bison file:

%{

#include <iostream>
#include <string.h>
#include "table.h"

extern int count;

int yylex();
int yyerror(const char *);
int UpdateSymTable(float, char *, float);

using namespace std;
%}

%union
{
    float FLOAT;
    char *NAME;
}

%token NUMBER
%token ID
%type <FLOAT> NUMBER
%type <NAME> ID
%type <FLOAT> expr
%type <FLOAT> E
%left '*'
%left '/'
%left '+'
%left '-'
%right '='

%%

E:          expr {cout << $$ << "\n";}

expr:       NUMBER {$$ = $1;}
        | expr '+' expr {$$ = $1 + $3;}
        | expr '-' expr {$$ = $1 - $3;}
        | expr '*' expr {$$ = $1 * $3;}
        | expr '/' expr {$$ = $1 / $3;}
        | ID '=' expr   {
                        int index = UpdateSymTable($$, $1, $3);
                        $$ = st[index].number = $3; //The problem is here
                        }

%%

int yyerror(const char *msg)
{
    cout << "Error: "<<msg<<"\n";
}

int UpdateSymTable(float doll_doll, char *doll_one, float doll_three)
{
    int number1 = -1;

    for(int i=0;i<count;i++)
    {
        if(!strcmp(doll_one, st[i].name) == 0)
        {

            strcpy(st[i].name, doll_one);
            st[i].number = doll_three;
            number1 = i;
        }
        else if(strcmp(doll_one, st[i].name) == 0)
        {
            number1 = i;
        }
    }

    return number1;
}

int main()
{
    yyparse();
}

The symbol table:

struct st
{
    float number;
    char name[25];
}st[25];

The output I'm getting is:

a = 20
c = a+3
20
Error: syntax error

I would really appreciate it if someone told me what is going wrong. I'm trying since a long time, and I haven't been able to resolve the error.

1 Answer 1

2
  1. The syntax error is the result of your grammar only accepting a single expr rather than a sequence of exprs. See, for example, this question.

  2. One of the problems with your symbol table lookup is that you incorrectly return the value yytext as your semantic value, instead of making a copy. See, for example, this question.

  3. However, your UpdateSymTable functions has quite a few problems, starting with the fact that the names you chose for parameters are meaningless, and furthermore the first parameter ("doll_doll") is never used. I don't know what you intended to test with !strcmp(doll_one, st[i].name) == 0 but whatever it was, there must be a simpler way of expressing it. In any case, the logic is incorrect. I'd suggest writing some simple test programs (without bison and flex) to let you debug the symbol table handling. And/or talk to your lab advisor, assuming you have one.

  4. Finally, (of what I noticed) your precedence relations are not correct. First, they are reversed: the operator which binds least tightly (assignment) should come first. Second, it is not the case that + has precedence over - , or vice versa; the two operators have the same precedence. Similarly with * and /. You could try reading the precedence chapter of the bison manual if you don't have lecture notes or other information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.