Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bison java examples

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!

like image 922
TheHube Avatar asked Feb 13 '12 21:02

TheHube


People also ask

What is bison programming?

Bison is a general-purpose parser generator that converts an annotated context-free grammar into a deterministic LR or generalized LR (GLR) parser employing LALR (1) parser tables. As an experimental feature, Bison can also generate IELR (1) or canonical LR(1) parser tables.

How does GNU bison work?

Bison reads a specification in the BNF notation (a context-free language), warns about any parsing ambiguities, and generates a parser that reads sequences of tokens and decides whether the sequence conforms to the syntax specified by the grammar.


2 Answers

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;
  }
}
like image 169
Tim Landscheidt Avatar answered Oct 01 '22 22:10

Tim Landscheidt


Since release 3.6 Bison ships with examples in Java. If Bison 3.6+ is installed on your machine, see /usr/local/share/doc/bison/examples/java (where /usr/local depends on your configuration).

You can browse the Java examples online on Savannah or on GitHub.

like image 22
akim Avatar answered Oct 01 '22 21:10

akim