2012-08-30 36 views
6

我想用空格縮進創建一個非常簡單的語法。每行包含的1分或更多的話,但壓痕等蟒(4個空格或一個標籤是一個凹槽),並且沒有關閉縮進,例如:ANTLR:空間縮進?

if something cool occurs 
    do this 
else 
    otherwise do this 
    loop around something 
     each time doing this 
     and do that 
say good byte 

而不是讀的每一行,計算壓痕和手動建立一棵樹是否可以在ANTLR語法中完成所有這些?我的目標語言是Java。

+6

請參閱:http://stackoverflow.com/questions/8642154/antlr-what-is-simpliest-way-to-realize-python-like-indent-depending-grammar –

回答

1

這是可能的。你所做的就是定義一條規則並讓它跳過。

在這裏你去:

Ignore : (' ' | '\t' | '\n' | '\r')+ {skip();}; 

或者,如果你需要認識\ n或\ r

Ignore : (' ' | '\t')+ {skip();}; 

添加到您的gramar和所有空格和製表符會被忽略。