Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 定义模式/语法并对其进行解析和实例_Java_Parsing_Schema_Grammar - Fatal编程技术网

Java 定义模式/语法并对其进行解析和实例

Java 定义模式/语法并对其进行解析和实例,java,parsing,schema,grammar,Java,Parsing,Schema,Grammar,我想知道是否有一个Java库,我可以用它来定义一个模式,然后如果我有一个该模式的实例,解析它 我的意思是,模式应该是这样的: string: [a-zA-Z]+ number: [1-9][0-9]* attribute: "attribute " name: string " " type: string :: MyParser.newAttribute(name, type) node: "node " name: string " {" :: MyParser.newNode($nam

我想知道是否有一个Java库,我可以用它来定义一个模式,然后如果我有一个该模式的实例,解析它

我的意思是,模式应该是这样的:

string: [a-zA-Z]+
number: [1-9][0-9]*

attribute: "attribute " name: string " " type: string :: MyParser.newAttribute(name, type)

node: "node " name: string " {" :: MyParser.newNode($name)
    attribute+
"}" :: MyParser.closeNode()

node+
node MyNode {
    attribute myAttribute1 Int
    attribute myAttribute2 Long
}

node SecondNode {
    attribute x String
}
然后模式的一个实例将如下所示:

string: [a-zA-Z]+
number: [1-9][0-9]*

attribute: "attribute " name: string " " type: string :: MyParser.newAttribute(name, type)

node: "node " name: string " {" :: MyParser.newNode($name)
    attribute+
"}" :: MyParser.closeNode()

node+
node MyNode {
    attribute myAttribute1 Int
    attribute myAttribute2 Long
}

node SecondNode {
    attribute x String
}
解析器将被生成,或者我不知道,但它的工作原理如下:

string: [a-zA-Z]+
number: [1-9][0-9]*

attribute: "attribute " name: string " " type: string :: MyParser.newAttribute(name, type)

node: "node " name: string " {" :: MyParser.newNode($name)
    attribute+
"}" :: MyParser.closeNode()

node+
node MyNode {
    attribute myAttribute1 Int
    attribute myAttribute2 Long
}

node SecondNode {
    attribute x String
}
  • 每当它到达一个节点行时,就会调用MyParser.newNode方法
  • 每当它到达属性行时,就会调用MyParser.newAttribute
  • 每当它到达}行时,就会调用MyParser.closeNode
当然,我必须保证在我的解析器项目中存在带有这些静态方法的MyParse类。

antlr----正是我想要的