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!
Unfortunately, virtually all public examples for Bison’s Java generator are hidden in the testsuite. If you are adventurous, after
./configure && makedomake 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 beneathtests/testsuite.dira bunch of directories with grammars, generated Java source code and compiled classes. One example from Bison 2.5: