2

私は次のようなものを翻訳する簡単な翻訳者を作成しようとしています:

aaa | bbb | ccc

1 : aaa
2 : bbb
c : ccc

文法test01.gは次のとおりです。

grammar test01;

options {
    output=AST; 
}

@members{
  int N;
}

test 
@init{
  N = 0;
}:
  id ('|' id)* -> id (BR id)*;

id   : {N++;} ID  -> {new CommonTree(new CommonToken(ID, Integer.toString(N) + " : "  + $ID.text))};
ID   : ('a'..'z')+;
BR   : '\n';
WS   : ' '{$channel=HIDDEN;};

翻訳者ソースFooTest.java:

import org.antlr.runtime.*;

class FooTest {
  public static void main(String[] args) throws Exception {    
    String text = "aaa | bbb | ccc";        
    System.out.println("parsing: "+text);        
    ANTLRStringStream in = new ANTLRStringStream(text);
    test01Lexer lexer = new test01Lexer(in);
    CommonTokenStream tokens = new TokenRewriteStream(lexer);
    test01Parser parser = new test01Parser(tokens);
    parser.test();
    System.out.println("Result: "+tokens.toString());    
  }
}

私がそれを実行するとき、私は次のようなものを手に入れることを期待しています:

parsing: aaa | bbb | ccc
Result:
 1 : aaa
 2 : bbb 
 3 : ccc

しかし、私は得る:

  parsing: aaa | bbb | ccc
  Result:  aaa | bbb | ccc

テキストは変更されていないようです。

変更されたソースを取得する方法は?

4

1 に答える 1

1

次のようにして、トークンのフラットリストを印刷するだけです。

CommonTokenStream tokens = new TokenRewriteStream(lexer);
// ...
System.out.println("Result: "+tokens.toString());  

FooTestクラスを次のように調整する場合:

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;

class FooTest {
  public static void main(String[] args) throws Exception {    
    String text = "aaa | bbb | ccc";        
    System.out.println("parsing: "+text);        
    ANTLRStringStream in = new ANTLRStringStream(text);
    test01Lexer lexer = new test01Lexer(in);
    CommonTokenStream tokens = new TokenRewriteStream(lexer);
    test01Parser parser = new test01Parser(tokens);
    CommonTree root = (CommonTree)parser.test().getTree();
    for(int i = 0; i < root.getChildCount(); i++) {
      CommonTree child = (CommonTree)root.getChild(i);
      System.out.println("root.children[" + i + "] = " + child);
    }
  }
}

以下がコンソールに出力されます。

parsing: aaa | bbb | ccc
root.children[0] = 1 : aaa
root.children[1] = BR
root.children[2] = 2 : bbb
root.children[3] = BR
root.children[4] = 3 : ccc

また、パーサークラスにグローバル変数を配置する必要がないことに注意してください。ルールは変数も処理します(それらにローカル)。これが推奨されます:

grammar test01;

options {
    output=AST; 
}

test:
  id ('|' id)* -> id (BR id)*;

id
@init{
  int N = 0;
}
  : {N++;} ID  -> {new CommonTree(new CommonToken(ID, Integer.toString(N) + " : "  + $ID.text))}
  ;

// other rules
于 2011-12-21T09:36:36.267 に答える