Bison java示例

问题描述 投票:7回答:2

没有人知道是否有一些教程和/或示例通过网络在Java上使用GNU Bison。我已经在网上搜寻了。但是我什么也没找到。我尝试实现一个示例,但无法编译它(因为我也需要一个词法分析器)。这是我的示例:

%{
  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;
        }
    }

%%

任何帮助将不胜感激!

java grammar bison yacc parser-generator
2个回答
13
投票

[不幸的是,几乎所有Bison Java生成器的公开示例都隐藏在测试套件中。如果您喜欢冒险,请在./configure && make之后执行make check TESTSUITEFLAGS="-d -k java"。这将使用关键字(-k)“ Java”运行所有测试,并且在成功测试(-d)之后不会删除沙箱目录,因此您可以在tests/testsuite.dir下找到一堆包含语法,生成的Java源代码并进行编译的目录类。 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
投票

因为release 3.6 Bison附带了Java示例。如果您的计算机上安装了Bison 3.6+,请参阅/usr/local/share/doc/bison/examples/java(其中/usr/local取决于您的配置)。

您可以在SavannahGitHub上在线浏览Java示例。

© www.soinside.com 2019 - 2024. All rights reserved.