'bison parser error

I get the following errors in my lex file file I don't know why that is happening each error is about each token returned to the parser

      lexical.l: In function âyylexâ:
lexical.l:29: error: expected expression before â;â token
lexical.l:30: error: âCALLâ undeclared (first use in this function)
lexical.l:30: error: (Each undeclared identifier is reported only once
lexical.l:30: error: for each function it appears in.)
lexical.l:31: error: âIDâ undeclared (first use in this function)
lexical.l:32: error: âNUMâ undeclared (first use in this function)
lexical.l:36: error: âRELOPâ undeclared (first use in this function)
lexical.l:37: error: âADDOPâ undeclared (first use in this function)
lexical.l:38: error: âMULOPâ undeclared (first use in this function)
lexical.l:39: error: âASSIGNâ undeclared (first use in this function)
lexical.l:40: error: âANDâ undeclared (first use in this function)
lexical.l:41: error: âORâ undeclared (first use in this function)
lexical.l:42: error: âNOTâ undeclared (first use in this function)

here is my lex file which is used to send tokens to the parser

%{
#include <stdio.h>
#include"bison.tab.h"
void showToken(char*);
%}

%option yylineno
%option noyywrap
digit [0-9]
letter [a-zA-Z]
whitespace [ \t]

%%
"else"                      {return ELSE;}
"real"                      {return REAL;}
"integer"                   {return INTEGER;}
"write"                     {return XWRITE;}
"while"                     {return WHILE;}
"end"                       {return END;}
"do"                        {return DO;}
"if"                        {return IF;}
"then"                      {return THEN;}
"program"                   {return XPROGRAM;}
"function"                  {return FUNCTION;}
"return"                    {return XRETURN;}
"read"                      {return XREAD;}
"var"                       {return VAR;}
"for"                       {return FOR;}
"begin"                     {return BEGIN;}
"call"                      {return CALL;}
{letter}({letter}|{digit})*         {return ID;}
{digit}{digit}*(("."{digit}{digit}*)?)      {return NUM;}
{digit}{digit}*({letter}|{digit})*      printf("Lexical Error");
[(),:;.]                return yytext[0]; 
^[\t ]+                 ;
(==|<>|<|<=|>|>=)           {return RELOP;}
(\+|-)                  {return ADDOP;}
(\*|\/)                 {return MULOP;}
(=)                 {return ASSIGN;}
(&&)                    {return AND;}
(\|\|)                  {return OR;}
(!)                 {return NOT;}
{whitespace}+               ;
"/*".*"*/"              ;
.                   {   
                    printf("Lexical Error");
                    exit(0);
                    }

%%
void showToken(char* name){
    printf("<%s,%s>",name,yytext);
}

should the tokens returned be in capital letters or it doesn't matter ? should I include the parser file here or just the tab.c file is enough ?



Solution 1:[1]

The errors are telling you that the various tokens you're trying to return (CALL, ID, NUM, RELOP, etc) have not been defined. You're including the bison.tab.h file, so that implies that you're missing %token declarations for these tokens in the .y file.

Solution 2:[2]

I faced the similar problem- Token undeclared (first use in this function). So I tried to copy my lex and yacc file to another folder and executed the bison and flex command and compiled the files again and the issue was resolved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris Dodd
Solution 2 GARVIT AGRAWAL