Below is the requirement to convert a BNF-form grammar into XML.
input:
define program
[repeat statement]
end define
define statement
[includeStatement]
| [keysStatement]
| [compoundsStatement]
| [commentsStatement]
| [tokensStatement]
| [defineStatement]
| [redefineStatement]
| [ruleStatement]
| [functionStatement]
| [externalStatement]
| [comment] [NL]
end define
expected output:
<Feature>
<program>
<statement>
<includeStatement />
<keysStatement />
<compoundsStatement />
<commentsStatement />
<tokensStatement />
<defineStatement />
<redefineStatement />
<ruleStatement />
<functionStatement />
<externalStatement />
<comment />
<NL />
</statement>
</program>
</Feature>
actual output:
<Feature>
<program>
<statement />
</program>
</Feature>
Below is a function in my code, ET.SubElement(parent, ) is working for one section but not working in another part the reason could because of ET.Element(nonTmnl) returns a value rather than returning a reference. I have commented the code on my finding. Appreciate any suggestion on how I can get access to a node in XML so I can insert a child node to it.
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;