我有以下语法检查XML文件的有效性,文件从一个元素开始,然后是根节点。

program
    : terminal_node
      root
    ;
root
    : '<' ID attribute_list '>' node_list '<' ID '/''>'
    ;
node_list
    : node
    | node node_list
    ;
node
    : terminal_node
    : nonterminal_node
    ;   

terminal_node
    : '<' ID attribute_list '/''>'
    ;

nonterminal_node
    : '<' ID attribute_list '>' node_list '<' ID '/''>'
    ;
attribute_list
    : attribute
    | attribute attribute_list
    ;

attribute
    : ID ASSIGNOP '"' ID '"'
    | ID ASSIGNOP '"' NUM '"'
    ;
.

我得到1减少/减少冲突,我不知道如何找到它。任何帮助都会受到赞赏。

有帮助吗?

解决方案

This looks a bit strange for an XML grammar. Are you sure you don't want empty node_lists or attribute_lists?

Anyway, try this:

node_list
    : node
    | node_list node /* list first, element second, this is the LALR way */
    ;
node
    : terminal_node
    | nonterminal_node /* note a typo in your code here */
    ; 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top