Errors occur in simple calculator programs using bisonc++ and flexc++

40 views Asked by At

The following error occurs in the following program I have looked at the flexc++ and bisonc++ references and think I understand some of it, but I have not been able to put together working code because the actual samples are not within my ability to search. I would appreciate it if you could tell me where and how to modify the code to make it work.

flexc++ code

%%
"+"     return Parser::ADD;
"-"     return Parser::SUB;
"*"     return Parser::MUL;
"/"     return Parser::DIV;
"\n"    return Parser::NL;
"("     return Parser::BB;
")"     return Parser::BE;

([1-9][0-9]*)|0|([0-9]+\.[0-9]*) return Parser::NUM;

[ \t] //ignore;

. {
    std::cerr << "lexical error\n";
    exit(1);
}
%%

bisonc++ code

%polymorphic value: double;

%type <value> expr
%token <value> NUM
%token ADD SUB MUL DIV BB BE NL
%left '+' '-'
%left '*' '/'
%expect 16

%start program

%%
program     : statement
                | program statement
                ;

statement       : expr NL
                    {
                        std::cout << $1 << std::endl;
                    }
                ;

expr            : NUM
                | BB expr BE
                    {
                        $$ = $2;
                    }
                | expr ADD expr
                    {
                        $$ = $1 + $3;
                    }
                | expr SUB expr
                    {
                        $$ = $1 - $3;
                    }
                | expr MUL expr
                    {
                        $$ = $1 * $3;
                    }
                | expr DIV expr
                    {
                        if ($3 != 0)
                        {
                            $$ = $1 / $3;
                        }
                        else
                        {
                            std::cerr << "division by zero" << std::endl;
                            exit(1);
                        }
                    }
                ;

compile command

g++ *.cc -o main

error code

lexer: In member function ‘int Scanner::executeAction_(size_t)’:
lexer:2:31: error: ‘Parser’ has not been declared
    2 | "+"             return Parser::ADD;
      |                               ^~~~~ 
lexer:3:31: error: ‘Parser’ has not been declared
    3 | "-"             return Parser::SUB;
      |                               ^~~~~ 
lexer:4:31: error: ‘Parser’ has not been declared
    4 | "*"             return Parser::MUL;
      |                               ^~~~~ 
lexer:5:31: error: ‘Parser’ has not been declared
    5 | "/"             return Parser::DIV;
      |                               ^~~~~ 
lexer:6:23: error: ‘Parser’ has not been declared
    6 | "\n"    return Parser::NL;
      |                       ^~~~  
lexer:7:31: error: ‘Parser’ has not been declared
    7 | "("             return Parser::BB;
      |                               ^~~~  
lexer:8:31: error: ‘Parser’ has not been declared
    8 | ")"             return Parser::BE;
      |                               ^~~~  
lexer:10:20: error: ‘Parser’ has not been declared
   10 | ([1-9][0-9]*)|0|([0-9]+\.[0-9]*) return Parser::NUM;
      |                    ^~~~~~
In file included from parse.cc:6:
Parser.ih: In member function ‘int Parser::lex()’:
Parser.ih:17:12: error: ‘d_scanner’ was not declared in this scope
   17 |     return d_scanner.lex();
      |            ^~~~~~~~~

Hopefully we will have a simple quadrature machine, I tried writing it in flex or bison and including Parser.h in the C declarations section, but it didn't work with syntax errors.

0

There are 0 answers