Java
レクサーのsetText(...)
メソッドを呼び出します:
grammar T;
parse
: words EOF {System.out.println($words.text);}
;
words
: Word (Spaces Word)*
;
Word
: ('a'..'z'|'A'..'Z')+
;
Spaces
: (' ' | '\t' | '\r' | '\n')+ {setText(" ");}
;
クラスでテストできるもの:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
String source = "This is \n just \t\t\t\t\t\t a \n\t\t test";
ANTLRStringStream in = new ANTLRStringStream(source);
TLexer lexer = new TLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
TParser parser = new TParser(tokens);
System.out.println("------------------------------\nSource:\n" + source +
"\n------------------------------\nAfter parsing:");
parser.parse();
}
}
これにより、次の出力が生成されます。
------------------------------
Source:
This is
just a
test
------------------------------
After parsing:
This is just a test
PuneetPawaiaは次のように書いています。
どんな助けでも大歓迎です。どういうわけか、ANTLRを理解するのが難しいと感じています。そこに良いチュートリアルはありますか?
ANTLR Wikiには、少し構造化されていませんが、有益な情報がたくさんあります(しかし、それは私だけかもしれません!)。
最高のANTLRチュートリアルは本です:決定的なANTLRリファレンス:ドメイン固有言語の構築。
C#
C#ターゲットの場合は、次のことを試してください。
grammar T;
options {
language=CSharp2;
}
@parser::namespace { Demo }
@lexer::namespace { Demo }
parse
: words EOF {Console.WriteLine($words.text);}
;
words
: Word (Spaces Word)*
;
Word
: ('a'..'z'|'A'..'Z')+
;
Spaces
: (' ' | '\t' | '\r' | '\n')+ {Text = " ";}
;
テストクラスで:
using System;
using Antlr.Runtime;
namespace Demo
{
class MainClass
{
public static void Main (string[] args)
{
ANTLRStringStream Input = new ANTLRStringStream("This is \n just \t\t\t\t\t\t a \n\t\t test");
TLexer Lexer = new TLexer(Input);
CommonTokenStream Tokens = new CommonTokenStream(Lexer);
TParser Parser = new TParser(Tokens);
Parser.parse();
}
}
}
これもThis is just a test
コンソールに出力されます。SetText(...)
代わりに使用しようとしましsetText(...)
たが、それも機能しませんでした。また、C#APIドキュメントは現在オフラインであるため、試行錯誤を繰り返しました{Text = " ";}
。C#3.1.1ランタイムDLLでテストしました。
幸運を!