以下是将BNF格式语法转换为XML的要求。
输入:
define program
[repeat statement]
end define
define statement
[includeStatement]
| [keysStatement]
| [compoundsStatement]
| [commentsStatement]
| [tokensStatement]
| [defineStatement]
| [redefineStatement]
| [ruleStatement]
| [functionStatement]
| [externalStatement]
| [comment] [NL]
end define
预期产出:
<Feature>
<program>
<statement>
<includeStatement />
<keysStatement />
<compoundsStatement />
<commentsStatement />
<tokensStatement />
<defineStatement />
<redefineStatement />
<ruleStatement />
<functionStatement />
<externalStatement />
<comment />
<NL />
</statement>
</program>
</Feature>
实际输出:
<Feature>
<program>
<statement />
</program>
</Feature>
下面是我的代码中的一个函数,ET.SubElement(parent,)正在为一个部分工作但在另一个部分不工作原因可能因为ET.Element(nonTmnl)返回一个值而不是返回一个引用。我已根据我的发现评论了代码。感谢有关如何访问XML节点的任何建议,以便我可以插入子节点。
import xml.etree.cElementTree as ET
def getNonTerminal (strline):
wordList=''
global parent
if re.match('define \w',strline):
nonTmnl = strline.replace('define ','')
nonTmnl = nonTmnl.replace('\n','')
nonTmnl = nonTmnl.replace(' ','')
if nonTmnl not in nonterminals:
child = ET.SubElement(parent, nonTmnl) #This line is working Problem line 2 not working and has a dependency on problem line 1 parent = child
nonterminals.append(nonTmnl)
else:
parent = ET.Element(nonTmnl) #Problem line1: Here I am searching for a node under which I want to insert a new sub-node
return;
if re.match('.*\[.*\].*',strline):
strline = strline.replace('\'[','')
while (re.match('.*\[.*\].*',strline)):
wordList = extractWords(strline)
strList = wordList.split(' ')
for item in strList:
if item not in TXLtoken and item not in TXLunparseElement and item not in TXLmodifier and item not in TXLother and item not in nonterminals :
if not item.startswith('\''):
item = item.replace(' ','')
while(item[-1] in TXLmodifier):
item = item[:-1]
nonterminals.append(item)
child = ET.SubElement(parent, item) #Problem line2: Here I am adding the subnode. While debugging I see it adds to the parent node(variable), but it never reflects in final XML.
strline = strline.replace('['+wordList+']','',1)
return;