web上是否有一个现有的、有效的主机文件语法?
我在http://www.antlr.org/grammar/list上查过列表,但没有找到。
我还检查了the hosts file entry in Wikipedia,它引用了RFC 952,但我不认为这与/windows/system32/drivers/etc/hosts使用的格式相同。
任何语法格式都比没有好,但我更喜欢ANTLR格式。这是我第一次使用语法生成器,我想保持我的学习曲线较低。我已经计划使用ANTLR来消费其他文件。
发布于 2011-05-26 20:35:45
从Microsoft页面执行以下操作:
主机文件格式与4.3版Berkeley Software Distribution (BSD) UNIX /
/文件中的主机表格式相同。
并且/etc/hosts文件被描述为here。
示例文件:
#
# 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 salthosts文件的格式如下:
H116表条目E117 E218可以选择以零个或多个alias
#结尾
粗体单词将是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请注意,这只是一个快速演示:主机名可以包含除我所描述的字符之外的其他字符,这只是一个缺点。
https://stackoverflow.com/questions/6135159
复制相似问题