ANTLR:如何跳过多行注释(ANTLR: How to skip multiline commen

2019-09-24 03:28发布

考虑下面的词法分析器:

lexer grammar CodeTableLexer;

@header {
    package ch.bsource.ice.parsers;
}

CodeTabHeader   : OBracket Code ' ' Table ' ' Version CBracket;
CodeTable       : Code ' '* Table;
EndCodeTable    : 'end' ' '* Code ' '* Table;
Code            : 'code';
Table           : 'table';
Version         : '1.0';
Row             : 'row';
Tabdef          : 'tabdef';
Override        : 'override' | 'no_override';
Obsolete        : 'obsolete';
Substitute      : 'substitute';
Status          : 'activ' | 'inactive';
Pkg             : 'include_pkg' | 'exclude_pkg';
Ddic            : 'include_ddic' | 'exclude_ddic';
Tab             : 'tab';
Naming          : 'naming';
Dfltlang        : 'dfltlang';
Language        : 'english' | 'german' | 'french' | 'italian' | 'spanish';
Null            : 'null';
Comma           : ',';
OBracket        : '[';
CBracket        : ']';

Boolean
    : 'true' 
    | 'false'
    ;

Number
    : Int* ('.' Digit*)?
    ;

Identifier
    : ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '$' | '#' | '.' | Digit)*
    ;

String
@after {
    setText(getText().substring(1, getText().length() - 1).replaceAll("\\\\(.)", "$1"));
}
    : '"' (~('"'))* '"'
    ;

Comment
    : '--' ~('\r' | '\n')* { skip(); }
    | '/*' .* '*/' { skip(); }
    ;

Space
    : (' ' | '\t') { skip(); }
    ;

NewLine
    : ('\r' | '\n' | '\u000C') { skip(); }
    ;

fragment Int
    : '1'..'9'
    | '0'
    ;

fragment Digit 
    : '0'..'9'
    ;

...及以下解析器:

parser grammar CodeTableParser;

options {
    tokenVocab = CodeTableLexer;
    backtrack = true;
    output = AST;
}

@header {
   package ch.bsource.ice.parsers;
}

parse
    : block EOF
    ;

block
    : CodeTabHeader^ codeTable endCodeTable
    ;

codeTable
    : CodeTable^ codeTableData
    ;

codeTableData
    : (Identifier^ obsolete?) (tabdef | row)*
    ;

endCodeTable
    : EndCodeTable
    ;

tabdef
    : Tabdef^ Identifier+
    ;

row
    : Row^ rowData
    ;

rowData
    : (Number^ | (Identifier^ (Comma Number)?))
        Override?
        obsolete?
        status?
        Pkg?
        Ddic?
        (tab | field)*
    ;

tab
    : Tab^ value+
    ;

field
    : (Identifier^ value) | naming
    ;

value
    : OBracket? (Identifier | String | Number | Boolean | Null) CBracket?
    ;

naming
    : Naming^ defaultNaming (l10nNaming)*
    ;

defaultNaming
    : Dfltlang^ String
    ;

l10nNaming
    : Language^ String?
    ;

obsolete
    : Obsolete^ Substitute String
    ;

status
    : Status^ Override?
    ;

......终于我班制作解析器不区分大小写:

package ch.bsource.ice.parsers;

import java.io.IOException;
import org.antlr.runtime.*;

public class ANTLRNoCaseFileStream extends ANTLRFileStream {

    public ANTLRNoCaseFileStream(String fileName) throws IOException {

        super (fileName, null);
    }

    public ANTLRNoCaseFileStream(String fileName, String encoding) throws IOException {

        super (fileName, null);
    }

    public int LA(int i) {

        if (i == 0) return 0;
        if (i < 0) i++;
        if ((p + 1 - 1) >= n) return CharStream.EOF
        return Character.toLowerCase(data[p + 1 - 1]);
    }
}

...单行注释被跳过的预期,而多行注释都没有......这里是错误消息我得到:

codetable_1.txt line 38:0 mismatched character '<EOF>' expecting '*'
codetable_1.txt line 38:0 mismatched input '<EOF>' expecting EndCodeTable
java.lang.NullPointerException
...

我缺少的东西吗? 有什么我应该知道的? 我使用ANTLR 3.4。

这里也是我试图解析示例源代码:

[code table 1.0]

/*
This is a multi-line comment
*/

code table my_table

-- this is a single-line comment
row 1
    id              "my_id_1"
    name            "my_name_1"
    descn           "my_description_1"
    naming
      dfltlang      "My description 1"
      english       "My description 1"
      german        "Meine Beschreibung 1"

-- this is another single-line comment
row 2
    id              "my_id_2"
    name            "my_name_2"
    descn           "my_description_2"
    naming
      dfltlang      "My description 2"
      english       "My description 2"
      german        "Meine Beschreibung 2"

end code table

任何帮助将非常感激 :-)

谢谢,J3D

Answer 1:

要做到这一点antlr4

BlockComment 
    : '/*' .*? '*/' -> skip
    ;


Answer 2:

巴特给了我一个惊人的支持,我想大家都非常欣赏他:-)

总之,问题是我用解析的字符流转换为小写FileStream类的错误。 这里下面是正确的Java源代码:

import java.io.IOException;
import org.antlr.runtime.*;

public class ANTLRNoCaseFileStream extends ANTLRFileStream {

    public ANTLRNoCaseFileStream(String fileName) throws IOException {

        super (fileName, null);
    }

    public ANTLRNoCaseFileStream(String fileName, String encoding) throws IOException {

        super (fileName, null);
    }

    public int LA(int i) {

        if (i == 0) return 0;
        if (i < 0) i++;
        if ((p + i - 1) >= n) return CharStream.EOF;
        return Character.toLowerCase(data[p + i - 1]);
    }
}


Answer 3:

我用2分的规则,我用跳过线(解析用于调试期间我打印出来)块注释。 他们在2拆分,以更好的可读性和块注释不支持嵌套评论。

另外,我不要跳过EOL字符( \r和/或\n )在我的语法,因为我需要他们明确了一些规则。

LineComment
    :   '//' ~('\n'|'\r')* //NEWLINE
        {System.out.println("lc > " + getText());
        skip();}
    ;

BlockComment
@init { int depthOfComments = 0;}
    :   '/*' {depthOfComments++;}
        ( options {greedy=false;}
        : ('/' '*')=> BlockComment {depthOfComments++;}
        | '/' ~('*')
        | ~('/')
        )*
        '*/' {depthOfComments--;}
        {
           if (depthOfComments == 0) {
                System.out.println("bc >" + getText());
                skip();
            }
        }
    ;


文章来源: ANTLR: How to skip multiline comments