ANTLR:空间缩进?

Ell*_*nce 7 java antlr lexer

我想用空格缩进创建一个非常简单的语法.每行由1个或多个单词组成,但缩进如python(4个空格或制表符是一个缩进),并且没有缩进的缩写,例如:

if something cool occurs
    do this
else
    otherwise do this
    loop around something
       each time doing this
       and do that
say good byte
Run Code Online (Sandbox Code Playgroud)

而不是读取每一行,计算缩进并手动构建树是否可以在ANTLR语法中完成所有这些?我的目标语言是Java.

Mar*_*ten 1

这个有可能。您所做的就是定义一个规则并让它被跳过。

干得好:

Ignore :  (' ' | '\t' | '\n' | '\r')+ {skip();}; 
Run Code Online (Sandbox Code Playgroud)

或者如果您需要识别 \n 或 \r

Ignore :  (' ' | '\t')+ {skip();}; 
Run Code Online (Sandbox Code Playgroud)

将其添加到您的语法中,所有空格和制表符都将被忽略。