ANTLR里迭代子规则的一个注意点

这几天在休假在家,有空的时候在用 ANTLR 3.2来写 D 2.0的语法。每次用ANTLR写比较大的语言的语法时都会碰到不少问题,这次也不例外;其中有一处虽然很快就解决了,但还是值得一记。 

话说用ANTLR写语法最经常碰到的问题就是很多语言规范都是用LR语法写的,但ANTLR是LL的,要自己做LR到LL的变换,然后经常为了迁就left-factoring最终写出来的语法跟原本规范里的语法看起来会比较不同;而D语言官网文档里的语法写得乱七八糟更使得这工作进行得不顺利。 

这次要记的主要是给子规则做迭代匹配时设定额外配置项(options { greedy = false; })的注意点。 

--------------------------------------------------------------------------- 

先从正常的情况开始看。例如说我们要写一个匹配C风格的行注释的词法规则,可以这样写: 
Antlr grammar代码   收藏代码
  1. Comment  
  2.     :   '//' ~( '\n' | '\r' )* EndOfLine  
  3.     ;  
  4.   
  5. fragment  
  6. EndOfLine  
  7.     :   '\n'  
  8.     |   '\r'  
  9.     |   '\r\n'  
  10.     ;  
Comment
    :   '//' ~( '\n' | '\r' )* EndOfLine
    ;

fragment
EndOfLine
    :   '\n'
    |   '\r'
    |   '\r\n'
    ;

对应的自动机图: 
 

在ANTLR中,词法规则的名字的首字母必须是大写的。上面两条词法规则的意思是:一个Comment由以下几个元素构成: 
1、1个行注释的起始标记,“//” 
2、0或多个非'\n'或'\r'的字符(ANTLR语法中,~是“非”,“|”是或,“*”是0到多个,“+”是1到多个,“?”是0或1个,括号是分组用的) 
3、1个换行符 
其中换行符写成了一个子规则EndOfLine,又包含三种可能性。注意到EndOfLine被fragment关键字修饰,说明它只能被用作别的词法规则的子规则,而不能用于单独匹配出一个token。 

在上面的词法规则中,*之所以能正确的迭代,是因为它迭代的内容与它后面的规则的起始字符没有交集——迭代的内容只能是除了'\n'或'\r'以外的内容,而EndOfLine只能以'\n'或'\r'开始,所以迭代总是会在最近的一个换行符出现的地方停止。 

ANTLR的Java后端为上面的词法规则生成的lexer代码是(经过简单编辑/格式化,下同): 
Java代码   收藏代码
  1. // $ANTLR start "Comment"  
  2. public final void mComment() throws RecognitionException {  
  3.     int _type = Comment;  
  4.     int _channel = DEFAULT_TOKEN_CHANNEL;  
  5.     // D:\\GrammarDemo\\T.g:4:5: ( '//' (~ ( '\\n' | '\\r' ) )* EndOfLine )  
  6.     // D:\\GrammarDemo\\T.g:4:9: '//' (~ ( '\\n' | '\\r' ) )* EndOfLine  
  7.     match("//");   
  8.   
  9.     // D:\\GrammarDemo\\T.g:4:14: (~ ( '\\n' | '\\r' ) )*  
  10.     loop1:  
  11.     do {  
  12.         int alt1=2;  
  13.         int LA1_0 = input.LA(1);  
  14.   
  15.         if ( (LA1_0 >= '\u0000' && LA1_0 <= '\t')  
  16.           || (LA1_0 >= '\u000B' && LA1_0 <= '\f')  
  17.           || (LA1_0 >= '\u000E' && LA1_0 <= '\uFFFF') ) {  
  18.             alt1=1;  
  19.         }  
  20.   
  21.         switch (alt1) {  
  22.         case 1 :  
  23.             // D:\\GrammarDemo\\T.g:4:14: ~ ( '\\n' | '\\r' )  
  24.             if ( (input.LA(1) >= '\u0000' && input.LA(1) <= '\t')  
  25.               || (input.LA(1) >= '\u000B' && input.LA(1) <= '\f')  
  26.               || (input.LA(1) >= '\u000E' && input.LA(1) <= '\uFFFF') ) {  
  27.                 input.consume();  
  28.             } else {  
  29.                 MismatchedSetException mse = new MismatchedSetException(null, input);  
  30.                 recover(mse);  
  31.                 throw mse;  
  32.             }  
  33.             break;  
  34.   
  35.         default :  
  36.             break loop1;  
  37.         }  
  38.     } while (true);  
  39.   
  40.     mEndOfLine();  
  41.   
  42.     state.type = _type;  
  43.     state.channel = _channel;  
  44. }  
  45. // $ANTLR end "Comment"  
  46.   
  47. // $ANTLR start "EndOfLine"  
  48. public final void mEndOfLine() throws RecognitionException {  
  49.     // D:\\GrammarDemo\\T.g:10:5: ( '\\r' | '\\n' | '\\r\\n' )  
  50.     int alt2 = 3;  
  51.     int LA2_0 = input.LA(1);  
  52.   
  53.     if ( (LA2_0 == '\r') ) {  
  54.         int LA2_1 = input.LA(2);  
  55.   
  56.         if ( (LA2_1 == '\n') ) {  
  57.             alt2 = 3;  
  58.         } else {  
  59.             alt2 = 1;  
  60.         }  
  61.     } else if ( (LA2_0 == '\n') ) {  
  62.         alt2 = 2;  
  63.     } else {  
  64.         throw new NoViableAltException(""20, input);  
  65.     }  
  66.     switch (alt2) {  
  67.     case 1 :  
  68.         // D:\\GrammarDemo\\T.g:10:9: '\\r'  
  69.         match('\r');  
  70.         break;  
  71.     case 2 :  
  72.         // D:\\GrammarDemo\\T.g:11:9: '\\n'  
  73.         match('\n');  
  74.         break;  
  75.     case 3 :  
  76.         // D:\\GrammarDemo\\T.g:12:9: '\\r\\n'  
  77.         match("\r\n");  
  78.         break;  
  79.     }  
  80. }  
  81. // $ANTLR end "EndOfLine"  
// $ANTLR start "Comment"
public final void mComment() throws RecognitionException {
    int _type = Comment;
    int _channel = DEFAULT_TOKEN_CHANNEL;
    // D:\\GrammarDemo\\T.g:4:5: ( '//' (~ ( '\\n' | '\\r' ) )* EndOfLine )
    // D:\\GrammarDemo\\T.g:4:9: '//' (~ ( '\\n' | '\\r' ) )* EndOfLine
    match("//"); 

    // D:\\GrammarDemo\\T.g:4:14: (~ ( '\\n' | '\\r' ) )*
    loop1:
    do {
        int alt1=2;
        int LA1_0 = input.LA(1);

        if ( (LA1_0 >= '\u0000' && LA1_0 <= '\t')
          || (LA1_0 >= '\u000B' && LA1_0 <= '\f')
          || (LA1_0 >= '\u000E' && LA1_0 <= '\uFFFF') ) {
            alt1=1;
        }

        switch (alt1) {
        case 1 :
            // D:\\GrammarDemo\\T.g:4:14: ~ ( '\\n' | '\\r' )
            if ( (input.LA(1) >= '\u0000' && input.LA(1) <= '\t')
              || (input.LA(1) >= '\u000B' && input.LA(1) <= '\f')
              || (input.LA(1) >= '\u000E' && input.LA(1) <= '\uFFFF') ) {
                input.consume();
            } else {
                MismatchedSetException mse = new MismatchedSetException(null, input);
                recover(mse);
                throw mse;
            }
            break;

        default :
            break loop1;
        }
    } while (true);

    mEndOfLine();

    state.type = _type;
    state.channel = _channel;
}
// $ANTLR end "Comment"

// $ANTLR start "EndOfLine"
public final void mEndOfLine() throws RecognitionException {
    // D:\\GrammarDemo\\T.g:10:5: ( '\\r' | '\\n' | '\\r\\n' )
    int alt2 = 3;
    int LA2_0 = input.LA(1);

    if ( (LA2_0 == '\r') ) {
        int LA2_1 = input.LA(2);

        if ( (LA2_1 == '\n') ) {
            alt2 = 3;
        } else {
            alt2 = 1;
        }
    } else if ( (LA2_0 == '\n') ) {
        alt2 = 2;
    } else {
        throw new NoViableAltException("", 2, 0, input);
    }
    switch (alt2) {
    case 1 :
        // D:\\GrammarDemo\\T.g:10:9: '\\r'
        match('\r');
        break;
    case 2 :
        // D:\\GrammarDemo\\T.g:11:9: '\\n'
        match('\n');
        break;
    case 3 :
        // D:\\GrammarDemo\\T.g:12:9: '\\r\\n'
        match("\r\n");
        break;
    }
}
// $ANTLR end "EndOfLine"

代码中input.LA(n)是向前看第n个位置上的字符的意思。input.LA(1)是接下来要马上匹配的字符,input.LA(2)是下一个,以此类推。 
上面的代码中input.LA(n)的n最大值是2,也就是说为了匹配这条词法规则,最多只需要向前看两个字符。 

如果用Ruby的正则表达式来表示上面的行注释词法规则,则是: 
Ruby代码   收藏代码
  1. %r{//[^\r\n]*(?:\r\n?|\n)}m  
%r{//[^\r\n]*(?:\r\n?|\n)}m

简单演示一下: 
Irb代码   收藏代码
  1. irb(main):001:0> s = "abc //\ndef //ghi\r\njkl"  
  2. => "abc //\ndef //ghi\r\njkl"  
  3. irb(main):002:0> r = %r{//[^\r\n]*(?:\r\n?|\n)}m  
  4. => /\/\/[^\r\n]*(?:\r\n?|\n)/m  
  5. irb(main):003:0> s.scan(r)  
  6. => ["//\n""//ghi\r\n"]  
irb(main):001:0> s = "abc //\ndef //ghi\r\njkl"
=> "abc //\ndef //ghi\r\njkl"
irb(main):002:0> r = %r{//[^\r\n]*(?:\r\n?|\n)}m
=> /\/\/[^\r\n]*(?:\r\n?|\n)/m
irb(main):003:0> s.scan(r)
=> ["//\n", "//ghi\r\n"]


--------------------------------------------------------------------------- 

如果要匹配的是C风格的块注释的话,可以把上面的词法规则变为: 
Antlr grammar代码   收藏代码
  1. Comment  
  2.     :   '/*' ( options { greedy = false; } : . )* '*/'  
  3.     ;  
Comment
    :   '/*' ( options { greedy = false; } : . )* '*/'
    ;

对应的自动机图: 
 

C风格的块注释以'/*'开始,以最近的一个'*/'结束,不能嵌套;在/*与*/之间可以是任意多个任意字符。也简单的写为: 
Antlr grammar代码   收藏代码
  1. Comment  
  2.     :   '/*' .* '*/'  
  3.     ;  
Comment
    :   '/*' .* '*/'
    ;


ANTLR的迭代运算符,“*”与“+”默认都是“贪婪的”,也就是会取最长匹配;这里我们要的是匹配“最近的一个'*/'”,要的是最短匹配,于是ANTLR默认的匹配方式就不满足需求了。为了解决这个问题,ANTLR提供了options { greedy = false; }语法来指定某个迭代子规则中使用最短匹配,同时也把“.*”与“.+”作为特例,在这两种情况下自动使用最短匹配,如果这两种情况下需要最长匹配需要显式指定options { greedy = true; }。 

ANTLR的Java后端为使用了最短匹配版本的块注释词法规则生成的lexer代码如下: 
Java代码   收藏代码
  1. // $ANTLR start "Comment"  
  2. public final void mComment() throws RecognitionException {  
  3.     int _type = Comment;  
  4.     int _channel = DEFAULT_TOKEN_CHANNEL;  
  5.     // D:\\GrammarDemo\\T.g:4:5: ( '/*' ( options {greedy=false; } : . )* '*/' )  
  6.     // D:\\GrammarDemo\\T.g:4:9: '/*' ( options {greedy=false; } : . )* '*/'  
  7.     match("/*");   
  8.   
  9.     // D:\\GrammarDemo\\T.g:4:14: ( options {greedy=false; } : . )*  
  10.     loop1:  
  11.     do {  
  12.         int alt1 = 2;  
  13.         int LA1_0 = input.LA(1);  
  14.   
  15.         if ( (LA1_0 == '*') ) {  
  16.             int LA1_1 = input.LA(2);  
  17.   
  18.             if ( (LA1_1 == '/') ) {  
  19.                 alt1 = 2;  
  20.             } else if ( (LA1_1 >= '\u0000' && LA1_1 <= '.')  
  21.                      || (LA1_1 >= '0' && LA1_1 <= '\uFFFF') ) {  
  22.                 alt1 = 1;  
  23.             }  
  24.         } else if ( (LA1_0 >= '\u0000' && LA1_0 <= ')')  
  25.                  || (LA1_0 >= '+' && LA1_0 <= '\uFFFF') ) {  
  26.             alt1 = 1;  
  27.         }  
  28.   
  29.         switch (alt1) {  
  30.         case 1 :  
  31.             // D:\\GrammarDemo\\T.g:4:46: .  
  32.             matchAny();  
  33.             break;  
  34.   
  35.         default :  
  36.             break loop1;  
  37.         }  
  38.     } while (true);  
  39.   
  40.     match("*/");  
  41.   
  42.     state.type = _type;  
  43.     state.channel = _channel;  
  44. }  
  45. // $ANTLR end "Comment"  
// $ANTLR start "Comment"
public final void mComment() throws RecognitionException {
    int _type = Comment;
    int _channel = DEFAULT_TOKEN_CHANNEL;
    // D:\\GrammarDemo\\T.g:4:5: ( '/*' ( options {greedy=false; } : . )* '*/' )
    // D:\\GrammarDemo\\T.g:4:9: '/*' ( options {greedy=false; } : . )* '*/'
    match("/*"); 

    // D:\\GrammarDemo\\T.g:4:14: ( options {greedy=false; } : . )*
    loop1:
    do {
        int alt1 = 2;
        int LA1_0 = input.LA(1);

        if ( (LA1_0 == '*') ) {
            int LA1_1 = input.LA(2);

            if ( (LA1_1 == '/') ) {
                alt1 = 2;
            } else if ( (LA1_1 >= '\u0000' && LA1_1 <= '.')
                     || (LA1_1 >= '0' && LA1_1 <= '\uFFFF') ) {
                alt1 = 1;
            }
        } else if ( (LA1_0 >= '\u0000' && LA1_0 <= ')')
                 || (LA1_0 >= '+' && LA1_0 <= '\uFFFF') ) {
            alt1 = 1;
        }

        switch (alt1) {
        case 1 :
            // D:\\GrammarDemo\\T.g:4:46: .
            matchAny();
            break;

        default :
            break loop1;
        }
    } while (true);

    match("*/");

    state.type = _type;
    state.channel = _channel;
}
// $ANTLR end "Comment"

留意到这段匹配代码在循环中一发现'*/'就会选择退出循环。 
也留意到上面的代码中input.LA(n)的n最大值是2,也就是说为了匹配这条词法规则,最多只需要向前看两个字符。 

而相应的,使用最长匹配的版本,生成的代码则是: 
Java代码   收藏代码
  1. // $ANTLR start "Comment"  
  2. public final void mComment() throws RecognitionException {  
  3.     int _type = Comment;  
  4.     int _channel = DEFAULT_TOKEN_CHANNEL;  
  5.     // D:\\GrammarDemo\\T.g:5:5: ( '/*' ( options {greedy=true; } : . )* '*/' )  
  6.     // D:\\GrammarDemo\\T.g:5:9: '/*' ( options {greedy=true; } : . )* '*/'  
  7.     match("/*");   
  8.   
  9.     // D:\\GrammarDemo\\T.g:5:14: ( options {greedy=true; } : . )*  
  10.     loop1:  
  11.     do {  
  12.         int alt1 = 2;  
  13.         int LA1_0 = input.LA(1);  
  14.   
  15.         if ( (LA1_0 == '*') ) {  
  16.             int LA1_1 = input.LA(2);  
  17.   
  18.             if ( (LA1_1 == '/') ) {  
  19.                 int LA1_3 = input.LA(3);  
  20.   
  21.                 if ( (LA1_3 >= '\u0000' && LA1_3 <= '\uFFFF') ) {  
  22.                     alt1 = 1;  
  23.                 }  
  24.             } else if ( (LA1_1 >= '\u0000' && LA1_1 <= '.')  
  25.                      || (LA1_1 >= '0' && LA1_1 <= '\uFFFF') ) {  
  26.                 alt1 = 1;  
  27.             }  
  28.         } else if ( (LA1_0 >= '\u0000' && LA1_0 <= ')')  
  29.                  || (LA1_0 >= '+' && LA1_0 <= '\uFFFF') ) {  
  30.             alt1 = 1;  
  31.         }  
  32.   
  33.         switch (alt1) {  
  34.         case 1 :  
  35.             // D:\\GrammarDemo\\T.g:5:45: .  
  36.             matchAny();  
  37.             break;  
  38.   
  39.         default :  
  40.             break loop1;  
  41.         }  
  42.     } while (true);  
  43.   
  44.     match("*/");  
  45.   
  46.     state.type = _type;  
  47.     state.channel = _channel;  
  48. }  
  49. // $ANTLR end "Comment"  
// $ANTLR start "Comment"
public final void mComment() throws RecognitionException {
    int _type = Comment;
    int _channel = DEFAULT_TOKEN_CHANNEL;
    // D:\\GrammarDemo\\T.g:5:5: ( '/*' ( options {greedy=true; } : . )* '*/' )
    // D:\\GrammarDemo\\T.g:5:9: '/*' ( options {greedy=true; } : . )* '*/'
    match("/*"); 

    // D:\\GrammarDemo\\T.g:5:14: ( options {greedy=true; } : . )*
    loop1:
    do {
        int alt1 = 2;
        int LA1_0 = input.LA(1);

        if ( (LA1_0 == '*') ) {
            int LA1_1 = input.LA(2);

            if ( (LA1_1 == '/') ) {
                int LA1_3 = input.LA(3);

                if ( (LA1_3 >= '\u0000' && LA1_3 <= '\uFFFF') ) {
                    alt1 = 1;
                }
            } else if ( (LA1_1 >= '\u0000' && LA1_1 <= '.')
                     || (LA1_1 >= '0' && LA1_1 <= '\uFFFF') ) {
                alt1 = 1;
            }
        } else if ( (LA1_0 >= '\u0000' && LA1_0 <= ')')
                 || (LA1_0 >= '+' && LA1_0 <= '\uFFFF') ) {
            alt1 = 1;
        }

        switch (alt1) {
        case 1 :
            // D:\\GrammarDemo\\T.g:5:45: .
            matchAny();
            break;

        default :
            break loop1;
        }
    } while (true);

    match("*/");

    state.type = _type;
    state.channel = _channel;
}
// $ANTLR end "Comment"

与前一版不同,这个版本的代码中input.LA(n)的n最大值是3,也就是说为了匹配这条词法规则,最多需要向前看三个字符。不同之处在于:即便循环中已经发现了'*/',如果进一步发现后面还没到输入字符流的末尾,则继续循环匹配下去,试图找到最长匹配。 

如果用Ruby的正则表达式来表示上面的最短匹配版的块注释词法规则,则是: 
Ruby代码   收藏代码
  1. %r{/\*.*?\*/}m  
%r{/\*.*?\*/}m

留意到中间匹配任意个任意字符用的是 .*? 而不是 .* ,因为Ruby的正则表达式里 * 是用最长匹配而 *? 是用最短匹配。 
简单演示一下: 
Irb代码   收藏代码
  1. irb(main):001:0> s = "abc /* def */ ghi */"  
  2. => "abc /* def */ ghi */"  
  3. irb(main):002:0> r = %r{/\*.*?\*/}m  
  4. => /\/\*.*?\*\//m  
  5. irb(main):003:0> s.scan(r)  
  6. => ["/* def */"]  
irb(main):001:0> s = "abc /* def */ ghi */"
=> "abc /* def */ ghi */"
irb(main):002:0> r = %r{/\*.*?\*/}m
=> /\/\*.*?\*\//m
irb(main):003:0> s.scan(r)
=> ["/* def */"]


--------------------------------------------------------------------------- 

好吧,前面码了那么多字其实还没进入正题,现在赶紧跳回来。 
在D语言中,除了上述两种C风格的注释外,还支持一种“嵌套注释”,以配对的'/+'和'+/'来标记其起始与终结。例如说: 
D代码   收藏代码
  1. a = /+ some comment /+ some nesting comment +/ ... +/ 1;  
a = /+ some comment /+ some nesting comment +/ ... +/ 1;

这个语句在经过解析、抛弃掉注释后,等同于: 
D代码   收藏代码
  1. a = 1;  
a = 1;


官网文档对此给出的词法规则是: 
Lr grammar代码   收藏代码
  1. NestingBlockComment:  
  2.     /+ NestingBlockCommentCharacters +/  
  3.   
  4. NestingBlockCommentCharacters:  
  5.     NestingBlockCommentCharacter  
  6.     NestingBlockCommentCharacter NestingBlockCommentCharacters  
  7.   
  8. NestingBlockCommentCharacter:  
  9.     Character  
  10.     NestingBlockComment  
NestingBlockComment:
    /+ NestingBlockCommentCharacters +/

NestingBlockCommentCharacters:
    NestingBlockCommentCharacter
    NestingBlockCommentCharacter NestingBlockCommentCharacters

NestingBlockCommentCharacter:
    Character
    NestingBlockComment

把这个LR规则简单转换为LL规则,用ANTLR的语法表示为: 
Antlr grammar代码   收藏代码
  1. Comment  
  2.     :   NestingBlockComment  
  3.     ;  
  4.   
  5. fragment  
  6. NestingBlockComment  
  7.     :   '/+' NestingBlockCommentCharacters '+/'  
  8.     ;  
  9.   
  10. fragment  
  11. NestingBlockCommentCharacters  
  12.     :   NestingBlockCommentCharacter*  
  13.     ;  
  14.   
  15. fragment  
  16. NestingBlockCommentCharacter  
  17.     :   NestingBlockComment  
  18.     |   .  
  19.     ;  
Comment
    :   NestingBlockComment
    ;

fragment
NestingBlockComment
    :   '/+' NestingBlockCommentCharacters '+/'
    ;

fragment
NestingBlockCommentCharacters
    :   NestingBlockCommentCharacter*
    ;

fragment
NestingBlockCommentCharacter
    :   NestingBlockComment
    |   .
    ;

(为方便起见,在ANTLR规则里添加了一个顶层词法规则Comment,而把原本的三条词法规则标记为fragment) 

一眼看上去貌似没啥问题了,但实际用这样生成的lexer来匹配D语言源码时会发现无法正确匹配嵌套注释。 
那么观察一下ANTLR的Java后端生成的lexer代码: 
Java代码   收藏代码
  1. // $ANTLR start "Comment"  
  2. public final void mComment() throws RecognitionException {  
  3.     int _type = Comment;  
  4.     int _channel = DEFAULT_TOKEN_CHANNEL;  
  5.     // D:\\GrammarDemo\\T.g:4:5: ( NestingBlockComment )  
  6.     // D:\\GrammarDemo\\T.g:4:9: NestingBlockComment  
  7.     mNestingBlockComment();  
  8.   
  9.     state.type = _type;  
  10.     state.channel = _channel;  
  11. }  
  12. // $ANTLR end "Comment"  
  13.   
  14. // $ANTLR start "NestingBlockComment"  
  15. public final void mNestingBlockComment() throws RecognitionException {  
  16.     // D:\\GrammarDemo\\T.g:9:5: ( '/+' NestingBlockCommentCharacters '+/' )  
  17.     // D:\\GrammarDemo\\T.g:9:9: '/+' NestingBlockCommentCharacters '+/'  
  18.     match("/+");  
  19.     mNestingBlockCommentCharacters();   
  20.     match("+/");  
  21. }  
  22. // $ANTLR end "NestingBlockComment"  
  23.   
  24. // $ANTLR start "NestingBlockCommentCharacters"  
  25. public final void mNestingBlockCommentCharacters() throws RecognitionException {  
  26.     // D:\\GrammarDemo\\T.g:14:5: ( ( NestingBlockCommentCharacter )* )  
  27.     // D:\\GrammarDemo\\T.g:14:9: ( NestingBlockCommentCharacter )*  
  28.     loop1:  
  29.     do {  
  30.         int alt1 = 2;  
  31.         int LA1_0 = input.LA(1);  
  32.   
  33.         if ( (LA1_0 >= '\u0000' && LA1_0 <= '\uFFFF') ) {  
  34.             alt1 = 1;  
  35.         }  
  36.   
  37.         switch (alt1) {  
  38.         case 1 :  
  39.             // D:\\GrammarDemo\\T.g:14:9: NestingBlockCommentCharacter  
  40.             mNestingBlockCommentCharacter();  
  41.             break;  
  42.   
  43.         default :  
  44.             break loop1;  
  45.         }  
  46.     } while (true);  
  47. }  
  48. // $ANTLR end "NestingBlockCommentCharacters"  
  49.   
  50. // $ANTLR start "NestingBlockCommentCharacter"  
  51. public final void mNestingBlockCommentCharacter() throws RecognitionException {  
  52.     // D:\\GrammarDemo\\T.g:19:5: ( NestingBlockComment | . )  
  53.     int alt2 = 2;  
  54.     int LA2_0 = input.LA(1);  
  55.   
  56.     if ( (LA2_0 == '/') ) {  
  57.         int LA2_1 = input.LA(2);  
  58.   
  59.         if ( (LA2_1 == '+') ) {  
  60.             alt2 = 1;  
  61.         } else {  
  62.             alt2 = 2;  
  63.         }  
  64.     } else if ( (LA2_0 >= '\u0000' && LA2_0 <= '.')  
  65.              || (LA2_0 >= '0' && LA2_0 <= '\uFFFF') ) {  
  66.         alt2 = 2;  
  67.     } else {  
  68.         throw new NoViableAltException(""20, input);  
  69.     }  
  70.     switch (alt2) {  
  71.     case 1 :  
  72.         // D:\\GrammarDemo\\T.g:19:9: NestingBlockComment  
  73.         mNestingBlockComment();  
  74.         break;  
  75.     case 2 :  
  76.         // D:\\GrammarDemo\\T.g:20:7: .  
  77.         matchAny();  
  78.         break;  
  79.     }  
  80. }  
  81. // $ANTLR end "NestingBlockCommentCharacter"  
// $ANTLR start "Comment"
public final void mComment() throws RecognitionException {
    int _type = Comment;
    int _channel = DEFAULT_TOKEN_CHANNEL;
    // D:\\GrammarDemo\\T.g:4:5: ( NestingBlockComment )
    // D:\\GrammarDemo\\T.g:4:9: NestingBlockComment
    mNestingBlockComment();

    state.type = _type;
    state.channel = _channel;
}
// $ANTLR end "Comment"

// $ANTLR start "NestingBlockComment"
public final void mNestingBlockComment() throws RecognitionException {
    // D:\\GrammarDemo\\T.g:9:5: ( '/+' NestingBlockCommentCharacters '+/' )
    // D:\\GrammarDemo\\T.g:9:9: '/+' NestingBlockCommentCharacters '+/'
    match("/+");
    mNestingBlockCommentCharacters(); 
    match("+/");
}
// $ANTLR end "NestingBlockComment"

// $ANTLR start "NestingBlockCommentCharacters"
public final void mNestingBlockCommentCharacters() throws RecognitionException {
    // D:\\GrammarDemo\\T.g:14:5: ( ( NestingBlockCommentCharacter )* )
    // D:\\GrammarDemo\\T.g:14:9: ( NestingBlockCommentCharacter )*
    loop1:
    do {
        int alt1 = 2;
        int LA1_0 = input.LA(1);

        if ( (LA1_0 >= '\u0000' && LA1_0 <= '\uFFFF') ) {
            alt1 = 1;
        }

        switch (alt1) {
        case 1 :
            // D:\\GrammarDemo\\T.g:14:9: NestingBlockCommentCharacter
            mNestingBlockCommentCharacter();
            break;

        default :
            break loop1;
        }
    } while (true);
}
// $ANTLR end "NestingBlockCommentCharacters"

// $ANTLR start "NestingBlockCommentCharacter"
public final void mNestingBlockCommentCharacter() throws RecognitionException {
    // D:\\GrammarDemo\\T.g:19:5: ( NestingBlockComment | . )
    int alt2 = 2;
    int LA2_0 = input.LA(1);

    if ( (LA2_0 == '/') ) {
        int LA2_1 = input.LA(2);

        if ( (LA2_1 == '+') ) {
            alt2 = 1;
        } else {
            alt2 = 2;
        }
    } else if ( (LA2_0 >= '\u0000' && LA2_0 <= '.')
             || (LA2_0 >= '0' && LA2_0 <= '\uFFFF') ) {
        alt2 = 2;
    } else {
        throw new NoViableAltException("", 2, 0, input);
    }
    switch (alt2) {
    case 1 :
        // D:\\GrammarDemo\\T.g:19:9: NestingBlockComment
        mNestingBlockComment();
        break;
    case 2 :
        // D:\\GrammarDemo\\T.g:20:7: .
        matchAny();
        break;
    }
}
// $ANTLR end "NestingBlockCommentCharacter"

可以看到 mNestingBlockCommentCharacters() 方法的循环是只要未遇到输入字符流的末尾就会继续循环匹配下去,跟前面讲解块注释的最长匹配版一样。但这里我们需要的是最短匹配。想起ANTLR提供了options { greedy = false; },这里不是正好派上用场么? 

把词法规则改为: 
Antlr grammar代码   收藏代码
  1. Comment  
  2.     :   NestingBlockComment  
  3.     ;  
  4.   
  5. fragment  
  6. NestingBlockComment  
  7.     :   '/+' NestingBlockCommentCharacters '+/'  
  8.     ;  
  9.   
  10. fragment  
  11. NestingBlockCommentCharacters  
  12.     :   ( options { greedy = false; } : NestingBlockCommentCharacter )*  
  13.     ;  
  14.   
  15. fragment  
  16. NestingBlockCommentCharacter  
  17.     :   NestingBlockComment  
  18.     |   .  
  19.     ;  
Comment
    :   NestingBlockComment
    ;

fragment
NestingBlockComment
    :   '/+' NestingBlockCommentCharacters '+/'
    ;

fragment
NestingBlockCommentCharacters
    :   ( options { greedy = false; } : NestingBlockCommentCharacter )*
    ;

fragment
NestingBlockCommentCharacter
    :   NestingBlockComment
    |   .
    ;

这会儿可好,ANTLR直接不肯生成出lexer了,抱怨词法规则有错: 
 

问题出在哪里呢?这就是这帖真正的主题了:ANTLR 3.2的options语法只能管其所在的子规则“字面上”出现的选择结构;指定用最短匹配后ANTLR不知道NestingBlockCommentCharacters能匹配什么了,也不知道最短匹配应该以什么来标志结束,直接报个错。 

解决的办法也很简单,只要把选择结构以字面量形式写到迭代中,并且把最短匹配与它后面的内容写在同一条大规则里,就好了,如下: 
Antlr grammar代码   收藏代码
  1. Comment  
  2.     :   NestingBlockComment  
  3.     ;  
  4.   
  5. fragment  
  6. NestingBlockComment  
  7.     :   '/+' ( options { greedy = false; } : ( NestingBlockComment | . ) )* '+/'  
  8.     ;  
Comment
    :   NestingBlockComment
    ;

fragment
NestingBlockComment
    :   '/+' ( options { greedy = false; } : ( NestingBlockComment | . ) )* '+/'
    ;

对应生成的代码是: 
Java代码   收藏代码
  1. // $ANTLR start "Comment"  
  2. public final void mComment() throws RecognitionException {  
  3.     int _type = Comment;  
  4.     int _channel = DEFAULT_TOKEN_CHANNEL;  
  5.     // D:\\GrammarDemo\\T.g:4:5: ( NestingBlockComment )  
  6.     // D:\\GrammarDemo\\T.g:4:9: NestingBlockComment  
  7.     mNestingBlockComment();  
  8.   
  9.     state.type = _type;  
  10.     state.channel = _channel;  
  11. }  
  12. // $ANTLR end "Comment"  
  13.   
  14. // $ANTLR start "NestingBlockComment"  
  15. public final void mNestingBlockComment() throws RecognitionException {  
  16.     // D:\\GrammarDemo\\T.g:9:5: ( '/+' ( options {greedy=false; } : ( NestingBlockComment | . ) )* '+/' )  
  17.     // D:\\GrammarDemo\\T.g:9:9: '/+' ( options {greedy=false; } : ( NestingBlockComment | . ) )* '+/'  
  18.     match("/+");   
  19.   
  20.     // D:\\GrammarDemo\\T.g:9:14: ( options {greedy=false; } : ( NestingBlockComment | . ) )*  
  21.     loop2:  
  22.     do {  
  23.         int alt2 = 2;  
  24.         int LA2_0 = input.LA(1);  
  25.   
  26.         if ( (LA2_0 == '+') ) {  
  27.             int LA2_1 = input.LA(2);  
  28.   
  29.             if ( (LA2_1 == '/') ) {  
  30.                 alt2 = 2;  
  31.             } else if ( (LA2_1 >= '\u0000' && LA2_1 <= '.')  
  32.                      || (LA2_1 >= '0' && LA2_1 <= '\uFFFF') ) {  
  33.                 alt2 = 1;  
  34.             }  
  35.         } else if ( (LA2_0 >= '\u0000' && LA2_0 <= '*')  
  36.                || (LA2_0 >= ',' && LA2_0 <= '\uFFFF') ) {  
  37.             alt2 = 1;  
  38.         }  
  39.   
  40.         switch (alt2) {  
  41.         case 1 :  
  42.             // D:\\GrammarDemo\\T.g:9:46: ( NestingBlockComment | . )  
  43.             // D:\\GrammarDemo\\T.g:9:46: ( NestingBlockComment | . )  
  44.             {  
  45.                 int alt1 = 2;  
  46.                 int LA1_0 = input.LA(1);  
  47.   
  48.                 if ( (LA1_0 == '/') ) {  
  49.                     int LA1_1 = input.LA(2);  
  50.   
  51.                     if ( (LA1_1 == '+') ) {  
  52.                         alt1 = 1;  
  53.                     }  
  54.                     else if ( (LA1_1 >= '\u0000' && LA1_1 <= '*')  
  55.                            || (LA1_1 >= ',' && LA1_1 <= '\uFFFF') ) {  
  56.                         alt1 = 2;  
  57.                     }  
  58.                     else {  
  59.                         throw new NoViableAltException(""11, input);  
  60.                     }  
  61.                 } else if ( (LA1_0 >= '\u0000' && LA1_0 <= '.')  
  62.                          || (LA1_0 >= '0' && LA1_0 <= '\uFFFF') ) {  
  63.                     alt1 = 2;  
  64.                 } else {  
  65.                     throw new NoViableAltException(""10, input);  
  66.                 }  
  67.                 switch (alt1) {  
  68.                 case 1 :  
  69.                     // D:\\GrammarDemo\\T.g:9:48: NestingBlockComment  
  70.                     mNestingBlockComment();  
  71.                     break;  
  72.                 case 2 :  
  73.                     // D:\\GrammarDemo\\T.g:9:70: .  
  74.                     matchAny();  
  75.                     break;  
  76.                 }  
  77.             }  
  78.             break;  
  79.   
  80.         default :  
  81.             break loop2;  
  82.         }  
  83.     } while (true);  
  84.   
  85.     match("+/");  
  86. }  
  87. // $ANTLR end "NestingBlockComment"  
// $ANTLR start "Comment"
public final void mComment() throws RecognitionException {
    int _type = Comment;
    int _channel = DEFAULT_TOKEN_CHANNEL;
    // D:\\GrammarDemo\\T.g:4:5: ( NestingBlockComment )
    // D:\\GrammarDemo\\T.g:4:9: NestingBlockComment
    mNestingBlockComment();

    state.type = _type;
    state.channel = _channel;
}
// $ANTLR end "Comment"

// $ANTLR start "NestingBlockComment"
public final void mNestingBlockComment() throws RecognitionException {
    // D:\\GrammarDemo\\T.g:9:5: ( '/+' ( options {greedy=false; } : ( NestingBlockComment | . ) )* '+/' )
    // D:\\GrammarDemo\\T.g:9:9: '/+' ( options {greedy=false; } : ( NestingBlockComment | . ) )* '+/'
    match("/+"); 

    // D:\\GrammarDemo\\T.g:9:14: ( options {greedy=false; } : ( NestingBlockComment | . ) )*
    loop2:
    do {
        int alt2 = 2;
        int LA2_0 = input.LA(1);

        if ( (LA2_0 == '+') ) {
            int LA2_1 = input.LA(2);

            if ( (LA2_1 == '/') ) {
                alt2 = 2;
            } else if ( (LA2_1 >= '\u0000' && LA2_1 <= '.')
                     || (LA2_1 >= '0' && LA2_1 <= '\uFFFF') ) {
                alt2 = 1;
            }
        } else if ( (LA2_0 >= '\u0000' && LA2_0 <= '*')
               || (LA2_0 >= ',' && LA2_0 <= '\uFFFF') ) {
            alt2 = 1;
        }

        switch (alt2) {
        case 1 :
            // D:\\GrammarDemo\\T.g:9:46: ( NestingBlockComment | . )
            // D:\\GrammarDemo\\T.g:9:46: ( NestingBlockComment | . )
            {
                int alt1 = 2;
                int LA1_0 = input.LA(1);

                if ( (LA1_0 == '/') ) {
                    int LA1_1 = input.LA(2);

                    if ( (LA1_1 == '+') ) {
                        alt1 = 1;
                    }
                    else if ( (LA1_1 >= '\u0000' && LA1_1 <= '*')
                           || (LA1_1 >= ',' && LA1_1 <= '\uFFFF') ) {
                        alt1 = 2;
                    }
                    else {
                        throw new NoViableAltException("", 1, 1, input);
                    }
                } else if ( (LA1_0 >= '\u0000' && LA1_0 <= '.')
                         || (LA1_0 >= '0' && LA1_0 <= '\uFFFF') ) {
                    alt1 = 2;
                } else {
                    throw new NoViableAltException("", 1, 0, input);
                }
                switch (alt1) {
                case 1 :
                    // D:\\GrammarDemo\\T.g:9:48: NestingBlockComment
                    mNestingBlockComment();
                    break;
                case 2 :
                    // D:\\GrammarDemo\\T.g:9:70: .
                    matchAny();
                    break;
                }
            }
            break;

        default :
            break loop2;
        }
    } while (true);

    match("+/");
}
// $ANTLR end "NestingBlockComment"

看,这次循环里的匹配逻辑就变回我们所需要的了:一遇到'+/'就退出循环。 

这种嵌套规则其实已经超出了正则语法及其对应的有限状态自动机的计算能力,需要使用上下文无关语法/下推式自动机(PDA)才可以处理。ANTLR生成的lexer与parser都是递归下降式的,lexer自身具有PDA级别的能力(加上断言就比PDA更强),所以可以正确处理嵌套规则。 
所以这个例子就没办法用Ruby的正则表达式来对比了。好吧Ruby的正则表达式本身已经超出DFA的范围了,不过还没达到PDA的范围   
Perl 6的rule倒是能表示出来,不过那个已经远远不是正则表达式了……这里不深入了。 

--------------------------------------------------------------------------- 

于是ANTLR想告诉我们的是:别把规则分得太细了……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值