托管文件ANTLR语法

| 网络上是否存在现有的有效主机文件语法? 我在http://www.antlr.org/grammar/list上签出了清单,但是在那儿找不到。 我还检查了Wikipedia中的hosts文件条目,它引用了RFC 952,但我认为这与/ windows / system32 / drivers / etc / hosts使用的格式相同。 任何语法格式总比没有好,但是我更喜欢ANTLR格式的语法。这是我第一次使用任何语法生成器,并且我希望保持较低的学习水平。我已经计划使用ANTLR来使用其他文件。     
已邀请:
从Microsoft页面:   HOSTS文件格式与4.3版Berkeley软件分发(BSD)UNIX / etc / hosts文件中的主机表格式相同。 并且/ etc / hosts文件在这里描述。 一个示例文件:
#
# Table of IP addresses and hostnames
#
172.16.12.2     peanut.nuts.com peanut
127.0.0.1       localhost
172.16.12.1     almond.nuts.com almond loghost
172.16.12.4     walnut.nuts.com walnut
172.16.12.3     pecan.nuts.com pecan
172.16.1.2      filbert.nuts.com filbert
172.16.6.4      salt.plant.nuts.com salt.plant salt
主机文件的格式如下: / etc / hosts中的每个表条目都包含一个IP地址,该IP地址由与该地址关联的主机名列表中的空格分隔 一个表条目可以选择以零个或多个别名结尾 评论以
#
开头 粗体字将是ANTLR语法中的规则,可能看起来像这样:
grammar Hosts;

parse
  :  tableEntry* EOF
  ;

tableEntry
  :  address hostName aliases?
     {
       System.out.println(\"\\n== Entry ==\");
       System.out.println(\"  address  : \" + $address.text);
       System.out.println(\"  hostName : \" + $hostName.text);
       System.out.println(\"  aliases  : \" + $aliases.text);
     }
  ;

address
  :  Octet \'.\' Octet \'.\' Octet \'.\' Octet
  ;

hostName
  :  Name
  ;

aliases
  :  Name+
  ;

Name
  :  Letter+ (\'.\' Letter+)*
  ;

Comment
  :  \'#\' ~(\'\\r\' | \'\\n\')* {$channel=HIDDEN;}
  ;

Space
  :  (\' \' | \'\\t\' | \'\\r\' | \'\\n\') {$channel=HIDDEN;}
  ;

Octet
  :  Digit Digit Digit
  |  Digit Digit
  |  Digit
  ;

fragment Letter
  :  \'a\'..\'z\'
  |  \'A\'..\'Z\'
  ;

fragment Digit
  :  \'0\'..\'9\'
  ;
可以使用以下类进行测试:
import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    String source = 
        \"#                                                   \\n\" +
        \"# Table of IP addresses and Hostnames               \\n\" +
        \"#                                                   \\n\" +
        \"172.16.12.2     peanut.nuts.com peanut              \\n\" +
        \"127.0.0.1       localhost                           \\n\" +
        \"172.16.12.1     almond.nuts.com almond loghost      \\n\" +
        \"172.16.12.4     walnut.nuts.com walnut              \\n\" +
        \"172.16.12.3     pecan.nuts.com pecan                \\n\" +
        \"172.16.1.2      filbert.nuts.com filbert            \\n\" +
        \"172.16.6.4      salt.plant.nuts.com salt.plant salt   \";
    ANTLRStringStream in = new ANTLRStringStream(source);
    HostsLexer lexer = new HostsLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HostsParser parser = new HostsParser(tokens);
    parser.parse();
  }
}
并将产生以下输出:
bart@hades:~/Programming/ANTLR/Demos/Hosts$ java -cp antlr-3.3.jar org.antlr.Tool Hosts.g
bart@hades:~/Programming/ANTLR/Demos/Hosts$ javac -cp antlr-3.3.jar *.java
bart@hades:~/Programming/ANTLR/Demos/Hosts$ java -cp .:antlr-3.3.jar Main

== Entry ==
  address  : 172.16.12.2
  hostName : peanut.nuts.com
  aliases  : peanut

== Entry ==
  address  : 127.0.0.1
  hostName : localhost
  aliases  : null

== Entry ==
  address  : 172.16.12.1
  hostName : almond.nuts.com
  aliases  : almond loghost

== Entry ==
      address  : 172.16.12.4
  hostName : walnut.nuts.com
  aliases  : walnut

== Entry ==
  address  : 172.16.12.3
  hostName : pecan.nuts.com
  aliases  : pecan

== Entry ==
  address  : 172.16.1.2
  hostName : filbert.nuts.com
  aliases  : filbert

== Entry ==
  address  : 172.16.6.4
  hostName : salt.plant.nuts.com
  aliases  : salt.plant salt
请注意,这只是一个快速演示:主机名可以包含除我描述的字符以外的其他字符,以列举一个缺点。     

要回复问题请先登录注册