2012-02-13 88 views
6

有谁知道,如果有一些教程和/或超过净使用GNU野牛与Java的例子。我通过网络搜索。但我没有设法找到任何东西。我试图实现一个例子,但我无法编译它(因为我还需要一个词法分析器)。这是我的例子:野牛Java示例

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

%% 

任何帮助会感激!

+0

不是你的直接问题,但我觉得有义务建议ANTLR! http://www.antlr.org/ – 2012-02-13 21:54:30

回答

10

不幸的是,几乎所有的野牛的Java发生器公共场合的例子都隐藏在测试套件。如果你冒险,./configure && makemake 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

非常感谢,我将深入野牛测试套件以了解更多信息! – TheHube 2012-02-14 10:54:18

+0

每次我尝试运行由此生成的Calc.java文件时,都会收到java.lang.ClassNotFoundException。你知道这是为什么吗?错误日志中没有任何行号。 – rgbrgb 2012-03-10 20:50:32

+0

您可以将您的报告发布到[email protected]吗?在评论部分很难诊断错误。 – 2012-03-10 23:01:42