2012-02-13 16 views
6

क्या कोई जानता है कि नेट पर जावा के साथ जीएनयू बाइसन का उपयोग करने के कुछ ट्यूटोरियल और/या उदाहरण हैं। मैंने नेट के माध्यम से खोज की है। लेकिन मैं कुछ भी खोजने का प्रबंधन नहीं किया था। मैंने एक उदाहरण लागू करने की कोशिश की है लेकिन मैं इसे संकलित नहीं कर सका (क्योंकि मुझे एक लेक्सर भी चाहिए)।बाइसन जावा उदाहरण

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

आपका सीधा सवाल नहीं है, लेकिन मुझे एएनटीएलआर का सुझाव देने के लिए बाध्य लगता है! http://www.antlr.org/ –

उत्तर

10

दुर्भाग्यवश, बायिसन जावा जनरेटर के लिए लगभग सभी सार्वजनिक उदाहरण परीक्षण में छिपे हुए हैं। यदि आप साहसी हैं, ./configure && make के बाद make check TESTSUITEFLAGS="-d -k java" करें। यह कीवर्ड (-k) "जावा" के साथ सभी परीक्षण चलाएगा और सफल परीक्षणों (-d) के बाद सैंडबॉक्स निर्देशिका को हटाएगा ताकि आप tests/testsuite.dir नीचे व्याकरण, जेनरेट किए गए जावा स्रोत कोड और संकलित कक्षाओं के साथ निर्देशिकाओं का एक समूह प्राप्त कर सकें। बाइसन 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

+0

मुझे हर बार java.lang.ClassNotFoundException मिलता है जब मैं इस द्वारा उत्पन्न Calc.java फ़ाइल को चलाने का प्रयास करता हूं। क्या आप जानते हैं कि ऐसा क्यों होगा? त्रुटि लॉग में कुछ भी लाइन संख्या नहीं है। – rgbrgb

+0

क्या आप अपनी रिपोर्ट को [email protected] पर पोस्ट कर सकते हैं, कृपया? एक टिप्पणी खंड में त्रुटियों का निदान करना मुश्किल है। –

संबंधित मुद्दे