ANTLR4停止解析文件的一部分并将其放入可访问的缓冲区(字符串)

时间:2018-04-20 10:59:32

标签: antlr antlr4

我希望语法能够严格定义它的结构,但结构的一部分不应该被我的语法解析,而是放入某种缓冲区(字符串)供以后使用。

我的语法看起来像这样:

grammar RSL;

rsl: sectionStructs? sectionProgram;

sectionProgram: 'section' 'program' '{' '}';

sectionStructs: 'section' 'structs' '{' structDef+ '}';
sectionName: ID;

structDef: 'struct' ID '{' varDef+ '}' ';';

varDef: ID ID ';';

ID: [a-zA-Z_][a-zA-Z_\-0-9]*;

WS  :  [ \t\r\n\u000C]+ -> skip
    ;

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

LINE_COMMENT
    :   '//' ~[\r\n]* -> skip
    ;

我的愿望是进行这种解析:

section structs {
    struct TestStruct {
        int var1;
        float var2;
        ...
    };

    struct Struct2 {
        int var1;
        ...
    };
}

section program {
    // Do not parse anything that would be in this section
    // just store it in a buffer for later use.
}

因此section program的所有内容都应存储在一个字符串中供以后使用,并且语法规则不应适用于程序。

解决此问题的最佳方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

一种方法是创建匹配此section program { ... }的词法分析器规则:

grammar RSL;

rsl
 : sectionStructs? SECTION_PROGRAM EOF
 ;

sectionStructs
 : 'section' 'structs' '{' structDef+ '}'
 ;

structDef
 : 'struct' ID '{' varDef+ '}' ';'
 ;

varDef
 : ID ID ';'
 ;

SECTION
 : 'section'
 ;

ID
 : [a-zA-Z_][a-zA-Z_\-0-9]*
 ;

SECTION_PROGRAM
 : 'section' S+ 'program' S* BLOCK
 ;

WS
 : S+ -> skip
 ;

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

LINE_COMMENT
 : '//' ~[\r\n]* -> skip
 ;

fragment BLOCK
 : '{' ( ~[{}] | BLOCK )* '}'
 ;

fragment S
 : [ \t\r\n]
 ;

将解析您的输入,如下所示:

enter image description here

当然,如果您的语言允许使用字符串文字等内容,则还需要在fragment BLOCK规则中考虑该内容。