Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, virtually all public examples for Bison's Java generator are hidden in the testsuite. If you are adventurous, after <code>./configure &amp;&amp; make</code> do <code>make check TESTSUITEFLAGS="-d -k java"</code>. This will run all tests with the keyword (<code>-k</code>) "Java" and not remove the sandbox directories after successful tests (<code>-d</code>) so you get beneath <code>tests/testsuite.dir</code> a bunch of directories with grammars, generated Java source code and compiled classes. One example from Bison 2.5:</p> <pre><code>/* 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 &lt;Integer&gt; NUM "number" %type &lt;Integer&gt; 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 &amp;&amp; l.token == token; } public String toString () { return Integer.toString (line) + "." + Integer.toString(token); } public int lineno () { return line; } public int token () { return token; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload