Microsoftページから:
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アドレスが含まれています
- テーブルエントリは、オプションで0個以上のエイリアスで終了できます
- コメントはで始まります
#
太字の単語は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
これは簡単なデモであることに注意してください。ホスト名には、私が説明した文字以外の文字を含めることができ、欠点を1つだけ挙げることができます。