Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:17:44+00:00 2026-06-14T10:17:44+00:00

i’m a newbie with this flex and bison and I have a small question.

  • 0

i’m a newbie with this flex and bison and I have a small question. Once I got my lex.yy.c file and my tab.c file when i complie the lex.yy.c file i get errors:

stojk_2.l: In function ‘int yylex()’:
stojk_2.l:3: error: ‘PLUS’ was not declared in this scope
stojk_2.l:4: error: ‘MINUS’ was not declared in this scope
stojk_2.l:5: error: ‘MULT’ was not declared in this scope
stojk_2.l:6: error: ‘DIVIDE’ was not declared in this scope
stojk_2.l:8: error: ‘LPAREN’ was not declared in this scope
stojk_2.l:9: error: ‘RPAREN’ was not declared in this scope
stojk_2.l:12: error: ‘yylval’ was not declared in this scope
stojk_2.l:13: error: ‘UNSIGNEDINTEGER’ was not declared in this scope

and when i compile the tab.c file i get these errors:

stojk_3.y: In function ‘void yyerror(char*)’:
stojk_3.y:12: error: ‘print’ was not declared in this scope
stojk_3.tab.c: At global scope:
stojk_3.tab.c:1056: error: redefinition of ‘double yylval’
stojk_3.y:8: error: ‘double yylval’ previously declared here
stojk_3.tab.c: In function ‘int yyparse()’:
stojk_3.tab.c:1253: error: ‘yylex’ was not declared in this scope
stojk_3.tab.c:1401: warning: deprecated conversion from string constant to ‘char*’
stojk_3.tab.c:1547: warning: deprecated conversion from string constant to ‘char*’

it seems they cant see each other, but i put them in the same folder so i dont know what I should do…..any help will be appreciated……

Thanks Guys for the help still trying to figure it out but here is my code:

stojk_2.l

%%

"+"            {return PLUS;}
"-"            {return MINUS;}
"*"            {return MULT;}
"/"            {return DIVIDE;}

"("            {return LPAREN;}
")"            {return RPAREN;}

[0-9]+     {
            sscanf(yytext,  "%lf", &yylval);
        return UNSIGNEDINTEGER;
            }

[ \t]      {  }
[\n]       {return yytext[0];}

.          {return yytext[0];}

%%

int yywrap()
{
        return 0;
}

stojk_3.y

%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define YYSTYPE double

YYSTYPE yylval;

void yyerror(char *s)
{
        print("yyerror: %s\n", s);
}

%}

%token PLUS
%token MINUS
%token MULT
%token DIVIDE

%token LPAREN
%token RPAREN

%token UNSIGNEDINTEGER

%left PLUS MINUS
%left MULT DIVIDE

%%

lines     :         lines expr   '\n'         {printf("%g\n", $2);}
          |         lines '\n'
          |         /*empty*/
          ;

expr      :          expr PLUS  expr           {$$  =  $1  +  $3;}  
          |          expr MINUS  expr           {$$  =  $1  -  $3;} 
          |          expr MULT  expr           {$$  =  $1  *  $3;} 
          |          expr DIVIDE  expr           {$$  =  $1  /  $3;} 
          |          LPAREN  expr  RPAREN        {$$ =  $2;}
          |          UNSIGNEDINTEGER
          ;

%%

#include  "lex.yy.c"

int yylex();
int yyparse(void);

int main()
{
return yyparse();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:17:45+00:00Added an answer on June 14, 2026 at 10:17 am

    You need to generate the header from bison that defines the constants. You need to include that header in the lexical analyzer.

    bison -d stojk_3.y    # generates stojk_3.tab.c and stojk_3.tab.h
    

    You need to include stojk_3.tab.h therefore in the Flex code.

    The print() function might be a typo for printf(); it certainly isn’t a standard function.

    The multiple definition of yylval is likely because you defined it but don’t need to (Bison does that for you). You may need to declare extern int yylex(void); or equivalent in your code. The conversion complaints mean you passed a literal string to a function that takes a char * (but a string is formally non-modifiable, so the function should be declared and defined to take a const char * instead).

    Without seeing more of your code, it is difficult to be more specific.


    With the code provided

    The problem with print() is a typo for printf(), as predicted. There’s a strong argument that should be fprintf(stderr, ...) instead; errors should go to stderr and not stdout.

    You don’t need to define yylval; comment out that line.

    I declared int yylex(void); at the top of the grammar file (in the %{ ... %} section).

    I made yyerror() into a static function. I was left with two warnings:

    gcc -O3 -g -std=c99 -Wall -Wextra -Wstrict-prototypes stojk_3.tab.c -o stojk_3.tab
    In file included from stojk_3.y:50:0:
    lex.yy.c:1112:17: warning: ‘yyunput’ defined but not used [-Wunused-function]
    lex.yy.c:1153:16: warning: ‘input’ defined but not used [-Wunused-function]
    

    Fixed code

    %{
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define YYSTYPE double
    
    int yylex(void);
    
    static
    void yyerror(char *s)
    {
            printf("yyerror: %s\n", s);
    }
    
    %}
    
    %token PLUS
    %token MINUS
    %token MULT
    %token DIVIDE
    
    %token LPAREN
    %token RPAREN
    
    %token UNSIGNEDINTEGER
    
    %left PLUS MINUS
    %left MULT DIVIDE
    
    %%
    
    lines     :         lines expr   '\n'         {printf("%g\n", $2);}
              |         lines '\n'
              |         /*empty*/
              ;
    
    expr      :          expr PLUS  expr           {$$  =  $1  +  $3;}  
              |          expr MINUS  expr           {$$  =  $1  -  $3;} 
              |          expr MULT  expr           {$$  =  $1  *  $3;} 
              |          expr DIVIDE  expr           {$$  =  $1  /  $3;} 
              |          LPAREN  expr  RPAREN        {$$ =  $2;}
              |          UNSIGNEDINTEGER
              ;
    
    %%
    
    #include  "lex.yy.c"
    
    int yylex(void);
    int yyparse(void);
    
    int main(void)
    {
        return yyparse();
    }
    

    I’m not getting the warnings about non-const character pointers; I’m not sure what the problem is there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.