【问题标题】:ANTLR: How to skip multiline commentsANTLR:如何跳过多行注释
【发布时间】:2012-10-15 14:42:43
【问题描述】:

给定以下词法分析器:

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]);
    }
}

...按预期跳过单行 cmets,而未按多行 cmets...这是我收到的错误消息:

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

【问题讨论】:

  • 您的测试输入会不会缺少一个结束*/

标签: antlr lexer parser-generator


【解决方案1】:

antlr4中执行此操作

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

【讨论】:

  • 但是如果您有多个评论,它不会删除介于两者之间的所有内容吗?类似的东西: /* 很长的注释 1 / some code here more code / comment 2 */ 如果我没记错的话,那个语法规则会跳过中间的代码,因为你定义了一个贪婪.*?
  • @dleal 问号使它不贪心; .* 会贪婪匹配
【解决方案2】:

Bart 给了我莫大的支持,我想我们都非常感谢他 :-)

无论如何,问题是我用来将解析的字符流转换为小写的 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]);
    }
}

【讨论】:

    【解决方案3】:

    我使用 2 条规则来跳过行和阻止 cmets(我在解析期间打印它们以用于调试目的)。它们分为 2 以提高可读性,并且块注释确实支持嵌套 cmets。

    另外,我不会在我的语法中跳过 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();
                }
            }
        ;
    

    【讨论】:

    • 看起来 OP 并没有尝试解析嵌套的块 cmets。但是你现在的做法有点冗长,IMO。您可以通过使用谓词或嵌入式代码来做到这一点,您正在做这两者。像这样的东西怎么样:BlockComment : '/*' (BlockComment | ('*' ~'/')=&gt; '*' | ~'*')* '*/' ;
    • @BartKiers ANTLR4 不支持您的建议(当时可能不存在......)。但是这可以工作: BLOCKCOMMENT: '/*' (BLOCKCOMMENT | ('' ~'/') | ~'')* '*/';
    猜你喜欢
    • 2013-08-24
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2012-12-18
    • 1970-01-01
    • 2020-01-18
    相关资源
    最近更新 更多