首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hosts文件ANTLR语法

Hosts文件ANTLR语法
EN

Stack Overflow用户
提问于 2011-05-26 15:30:28
回答 1查看 1.2K关注 0票数 3

web上是否有一个现有的、有效的主机文件语法?

我在http://www.antlr.org/grammar/list上查过列表,但没有找到。

我还检查了the hosts file entry in Wikipedia,它引用了RFC 952,但我不认为这与/windows/system32/drivers/etc/hosts使用的格式相同。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-26 20:35:45

Microsoft页面执行以下操作:

主机文件格式与4.3版Berkeley Software Distribution (BSD) UNIX /

/文件中的主机表格式相同。

并且/etc/hosts文件被描述为here

示例文件:

代码语言:javascript
复制
#
# 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

hosts文件的格式如下:

  • /etc/中每个主机表条目都包含一个IP地址,该地址由从与该地址主机名相关的主机名列表中分离

H116表条目E117 E218可以选择以零个或多个alias

  • comments#

结尾

粗体单词将是ANTLR语法中的规则,可能如下所示:

代码语言:javascript
复制
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'
  ;

可以使用类进行测试:

代码语言:javascript
复制
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();
  }
}

并将产生以下输出:

代码语言:javascript
复制
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

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6135159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档