3

网络上是否存在现有的有效主机文件语法?

我在http://www.antlr.org/grammar/list上查看了列表,但我没有在那里找到它。

我还检查了 Wikipedia 中的 hosts 文件条目,它引用了RFC 952,但我认为这与 /windows/system32/drivers/etc/hosts 使用的格式不同。

任何语法格式都比没有好,但我更喜欢 ANTLR 格式。这是我第一次使用任何语法生成器,我想保持低学习曲线。我已经计划使用 ANTLR 来使用其他文件。

4

1 回答 1

5

微软页面:

HOSTS 文件格式与版本 4.3 Berkeley Software Distribution (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 地址,由与该地址关联的主机名列表中空格分隔
  • 表条目可以选择以零个或多个别名结尾
  • 评论开头#

粗体字将是 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

请注意,这只是一个快速演示:主机名可以包含我所描述的字符之外的其他字符,仅举一个缺点。

于 2011-05-26T12:35:44.893 回答