2012-02-13 2 views
6

그물을 통해 Java와 함께 GNU Bison을 사용하는 자습서 및/또는 예제가 있는지 아는 사람이 있습니까? 나는 그물을 수색했다. 그러나 나는 아무것도 찾을 수 없었다. 예제를 구현하려고 시도했지만 컴파일 할 수 없습니다 (필자도 렉서가 필요하기 때문에).Bison java examples

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

답변

10

불행히도, Bison의 Java 생성기에 대한 모든 공개 예제는 테스트 스위트에 숨겨져 있습니다. 당신이 모험이라면, ./configure && make 후에 make check TESTSUITEFLAGS="-d -k java"을해라. 이 키워드 (-k) "자바"모든 테스트를 실행하고 성공적인 테스트 (-d) 후 샌드 박스 디렉토리를 제거하지 그래서 당신은 tests/testsuite.dir 아래 문법과 디렉토리의 무리를 얻을 것이다, 자바 소스 코드와 컴파일 된 클래스를 생성합니다. 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

+0

이것으로 생성 된 Calc.java 파일을 실행할 때마다 java.lang.ClassNotFoundException이 발생합니다. 왜 그렇게 될지 아십니까? 오류 로그에는 아무 것도없는 행 번호가 없습니다. – rgbrgb

+0

[email protected]로 보고서를 보내 주시겠습니까? 댓글 섹션에서 오류를 진단하는 것은 어렵습니다. –