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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:23:54+00:00 2026-05-29T19:23:54+00:00

Does anyone knows if there are some tutorials and/or examples of using GNU Bison

  • 0

Does anyone knows if there are some tutorials and/or examples of using GNU Bison with Java over the net. I’ve searched through the net. But i didn’t manage to find anything. I have tried to implement an example but I could not compile it (since I need a lexer also). Here is my example:

%{
  static void main(String[] args) {
    yyparse();
  }
%}

%union {
  int     number;
  char    operator;
}

%language "Java"

%token<number>   NUMBER 
%token<operator> OPERATOR  

%type <number> exp

%left OPERATOR
%%

input
    : /* Empty string */
    | exp { System.out.print("Result >> " + $1); }
    ;

exp
    : NUMBER
    | exp OPERATOR exp { 
        switch($2) {
            case '+': $$ = $1 + $3; break;
            case '-': $$ = $1 - $3; break;
            case '*': $$ = $1 * $3; break;
            case '/': $$ = $1 / $3; break;
        }
    }

%%

Any help would be appreciate!

  • 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-05-29T19:23:55+00:00Added an answer on May 29, 2026 at 7:23 pm

    Unfortunately, virtually all public examples for Bison’s Java generator are hidden in the testsuite. If you are adventurous, after ./configure && make do make check TESTSUITEFLAGS="-d -k java". This will run all tests with the keyword (-k) “Java” and not remove the sandbox directories after successful tests (-d) so you get beneath tests/testsuite.dir a bunch of directories with grammars, generated Java source code and compiled classes. One example from Bison 2.5:

    /* Infix notation calculator--calc */
    %language "Java"
    %name-prefix "Calc"
    %define parser_class_name "Calc"
    %define public
    
    
    %code {
    
      public static void main (String args[]) throws IOException
      {
        CalcLexer l = new CalcLexer (System.in);
        Calc p = new Calc (l);
        p.parse ();
      }
    
    }
    
    %code imports {
      import java.io.StreamTokenizer;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.Reader;
      import java.io.IOException;
    }
    
    /* Bison Declarations */
    %token <Integer> NUM "number"
    %type  <Integer> exp
    
    %nonassoc '=' /* comparison            */
    %left '-' '+'
    %left '*' '/'
    %left NEG     /* negation--unary minus */
    %right '^'    /* exponentiation        */
    
    /* Grammar follows */
    %%
    input:
      line
    | input line
    ;
    
    line:
      '\n'
    | exp '\n'
    | error '\n'
    ;
    
    exp:
      NUM                { $$ = $1;                                             }
    | exp '=' exp
      {
        if ($1.intValue () != $3.intValue ())
          yyerror ( "calc: error: " + $1 + " != " + $3);
      }
    | exp '+' exp        { $$ = new Integer ($1.intValue () + $3.intValue ());  }
    | exp '-' exp        { $$ = new Integer ($1.intValue () - $3.intValue ());  }
    | exp '*' exp        { $$ = new Integer ($1.intValue () * $3.intValue ());  }
    | exp '/' exp        { $$ = new Integer ($1.intValue () / $3.intValue ());  }
    | '-' exp  %prec NEG { $$ = new Integer (-$2.intValue ());                  }
    | exp '^' exp        { $$ = new Integer ((int)
                                             Math.pow ($1.intValue (),
                                                       $3.intValue ()));        }
    | '(' exp ')'        { $$ = $2;                                             }
    | '(' error ')'      { $$ = new Integer (1111);                             }
    | '!'                { $$ = new Integer (0); return YYERROR;                }
    | '-' error          { $$ = new Integer (0); return YYERROR;                }
    ;
    
    
    %%
    class CalcLexer implements Calc.Lexer {
    
      StreamTokenizer st;
    
      public CalcLexer (InputStream is)
      {
        st = new StreamTokenizer (new InputStreamReader (is));
        st.resetSyntax ();
        st.eolIsSignificant (true);
        st.whitespaceChars (9, 9);
        st.whitespaceChars (32, 32);
        st.wordChars (48, 57);
      }
    
    
      public void yyerror (String s)
      {
        System.err.println (s);
      }
    
    
      Integer yylval;
    
      public Object getLVal() {
        return yylval;
      }
    
      public int yylex () throws IOException {
        int ttype = st.nextToken ();
    
        if (ttype == st.TT_EOF)
          return Calc.EOF;
    
        else if (ttype == st.TT_EOL)
          {
    
            return (int) '\n';
          }
    
        else if (ttype == st.TT_WORD)
          {
            yylval = new Integer (st.sval);
            return Calc.NUM;
          }
    
        else
          return st.ttype;
      }
    
    
    
    }
    
    
    class Position {
      public int line;
      public int token;
    
      public Position ()
      {
        line = 0;
        token = 0;
      }
    
      public Position (int l, int t)
      {
        line = l;
        token = t;
      }
    
      public boolean equals (Position l)
      {
        return l.line == line && l.token == token;
      }
    
      public String toString ()
      {
        return Integer.toString (line) + "." + Integer.toString(token);
      }
    
      public int lineno ()
      {
        return line;
      }
    
      public int token ()
      {
        return token;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if there is some type of tool, preferably a Firefox add
Does anyone know there have any other way that (by not using json_encode and
Does anyone know if there is a good equivalent to Java's Set collection in
Does anyone know how to read a x.properties file in Maven. I know there
Does anyone know whether there's a way to mock Entity Data Provider so Unit
Does anyone know if there is a c# Console app, similar to the Python
Does anyone know why there is no respond_to block for generated edit actions? Every
Does anyone know if there's an add-in that does autocomplete for queries on SQL
Does anyone know if there is an API to get the current monitor state
Does anyone know if there is an implementation of javax.jms.QueueConnectionFactory for WebSphere MQ and

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.