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 7805287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:10:38+00:00 2026-06-02T02:10:38+00:00

I am writing a parser with flex and bison and so far have these

  • 0

I am writing a parser with flex and bison and so far have these tokens for flex:

[ \t\n] ;
(x[0-9]+)   {
    yylval.var = strdup(yytext);
    return VARIABLE;
}
~(x[0-9]+)  {
    yylval.var = strdup(yytext);
    return NEG_VARIABLE;
}
[a-zA-Z0-9]+ {
                yylval.name = strdup(yytext);
                return NAME;
            }
~[a-zA-Z0-9]+ {
                    yylval.name = strdup(yytext);
                    return NEG_NAME;
                }
[\{\},\(\)] { return yytext[0];}
. ;

and these parse rules for bison are:

fol:
        clauses {cout << "Done with file"<<endl;}
        ;
clauses:
        clauses clause
        | clause
        ;
clause:
        startc terms endc
        ;
startc:
        '{' {cout << "Bison found start of clause" << endl;}
        ;
endc:
        '}' {cout << "Bison found end of clause" << endl;}
        ;
function:
        NAME startfun endfun {cout << "Bison found a function " << $1 << endl;}
        |NEG_NAME startfun endfun {cout << "Bison found a negative function " << $1 << endl;}
        ;
startfun:
        '(' {cout << "Bison found start of params" << endl;}
        ;
endfun:
        terms ')' {cout << "Bison found a function end" << endl;}
        ;
terms:
        terms ',' term
        | term
        ;
term:
        VARIABLE {cout << "Bison found a variable "<< $1 << endl;}
        | NEG_VARIABLE {cout << "Bison found a negative variable " << $1 << endl;}
        | NAME {cout << "Bison found a constant " << $1 << endl;}
        |function
        ;

Now it all works great except that when it parses a function it first parses the parameters
and parens and then gives me the function name at the end. I can work around this, but it makes my life harder since I am storing functions as disjoint sets and I would need to keep a list of the parameters until I can get the name of the function to create the root and then union them in instead of creating it directly.

Can anyone show me how to get Bison to give me the function name before the parameters? I have been trying for over an hour with no luck.

  • 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-02T02:10:40+00:00Added an answer on June 2, 2026 at 2:10 am

    What do you mean by “give me the function name before the parameters”? Currently you don’t print the function name until after you see the endfun and reduce the function rule, which is after the various parameter rules. The usual technique is to have the terms rules produce a list of things that can then be used in the function rule:

    terms:  terms ',' term { $$ = append_list($1, $3); }
         |  term { $$ = create_singleton_list($1); }
         ;
    
    term:   VARIABLE { $$ = new VariableTerm($1); }
    

    …

    Alternately, if you just want to print things out, you can have a rule that is reduced as soon as you see a function name:

    function: funcname startfun endfun {cout << "Bison ending a function " << $1 << endl;}
            ;
    
    funcname: NAME { cout << "Starting a function " << ($$ = $1) << endl; }
            | NEGNAME { cout << "Starting a negative function " << ($$ = $1) << endl; }
            ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Python parser to learn Flex and Bison, and I'm trying to
I am writing a parser and throughout my code in various methods I have
I'm writing a parser in Bison for a basic compiler (and then expand it
I am kind new to writing custom languages. Before diving to Flex,Bison based Lexers,Parsers,Compilers
I am writing a parser to parse incoming text files. I have it to
I'm writing up a small parser which parses constraints, using Flex and Lemon. Lemon
I'm writing a parser using Bison, and am using a map for semantic evaluation.
I am writing a vcf parser and I have the file open but now
I am writing a parser. I have taken care of all the encoding conversion
I'm writing a simple recursive descent parser in OCaml. Typically (as far as 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.