在ANTLR中,如何指定特定的重复次数? [英] In ANTLR, how do you specify a specific number of repetitions?

查看:146
本文介绍了在ANTLR中,如何指定特定的重复次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ANTLR指定一种文件格式,其中包含不能超过254个字符的行(不包括行尾).我该如何在语法分析器中对此进行编码,而无需这样做:

I'm using ANTLR to specify a file format that contains lines that cannot exceed 254 characters (excluding line endings). How do I encode this in the grammer, short of doing:

line : CHAR? CHAR? CHAR? CHAR? ... (254 times)

推荐答案

这可以通过使用语义谓语.

首先以这样一种方式编写语法,即您的行多长无关紧要.一个示例如下所示:

First write your grammar in such a way that it does not matter how long your lines are. An example would look like this:

grammar Test;

parse
  :  line* EOF
  ;

line
  :  Char+ (LineBreak | EOF)
  |  LineBreak // empty line!
  ;

LineBreak : '\r'? '\n' | '\r' ;
Char      : ~('\r' | '\n') ;

,然后将谓词"添加到line规则:

and then add the "predicate" to the line rule:

grammar Test;

@parser::members {
    public static void main(String[] args) throws Exception {
        String source = "abcde\nfghij\nklm\nnopqrst";
        ANTLRStringStream in = new ANTLRStringStream(source);
        TestLexer lexer = new TestLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        TestParser parser = new TestParser(tokens);
        parser.parse();
    }  
}

parse
  :  line* EOF
  ;

line
  :  (c+=Char)+ {$c.size()<=5}? (LineBreak | EOF)
  |  LineBreak // empty line!
  ;

LineBreak : '\r'? '\n' | '\r' ;
Char      : ~('\r' | '\n') ;

c+=Char将构造一个ArrayList,其中包含该行中的所有字符.当ArrayList的大小超过5时,{$c.size()<=5}?会引发异常.

The c+=Char will construct an ArrayList containing all characters in the line. The {$c.size()<=5}? causes to throw an exception when the ArrayList's size exceeds 5.

我还在解析器中添加了一个main方法,因此您可以自己对其进行测试:

I also added a main method in the parser so you can test it yourself:

// *nix/MacOSX
java -cp antlr-3.2.jar org.antlr.Tool Test.g
javac -cp antlr-3.2.jar *.java
java -cp .:antlr-3.2.jar TestParser

// Windows
java -cp antlr-3.2.jar org.antlr.Tool Test.g
javac -cp antlr-3.2.jar *.java
java -cp .;antlr-3.2.jar TestParser

它将输出:

line 0:-1 rule line failed predicate: {$c.size()<=5}?

HTH

这篇关于在ANTLR中,如何指定特定的重复次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆