
大概想一下要怎麼把上面的data轉換成下面的格式?先前的Lexer可以幫我們辨識文件裡的組成元件,分辨什麼是數字,分辨什麼是字母。但就原始data來看,我們還必須讓程式知道每一筆資料,到底以什麼樣的順序包含了這些小元件。換句話說,我們必需分析一下這份資料檔案的結構,是如何由這些元件組成的。要分析一份資料,以top-down的思考方式為佳。

class SimpleLexer extends Lexer;
options { k=1; filter=true; }
ALPHA : ('a'..'z'|'A'..'Z')+
{ System.out.println("Found alpha: "+ getText()); }
;
NUMERIC : ('0'..'9')+
{ System.out.println("Found numeric: "+ getText()); }
;
EXIT : '.'
{ System.exit(0); }
;
ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.