1

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;
  • The code is incomplete. What is `strline`? Where are `nonterminals` and `TXLtoken` defined? Please try to create a [mcve]. – mzjn Jun 06 '18 at 07:24
  • strline, nonterminals, and TXLtoken all are defined to do some string processing to extract words like includeStatement from the input to store it in the XML. All other functionalities are working fine, except to get a node in XML and append a new sub-node to it. – Karthik Vishwambar Jun 06 '18 at 13:21
  • Sorry, but that does not help much. We cannot run the code you have shown us. How are we supposed to debug it? – mzjn Jun 06 '18 at 14:38
  • Thank you for your help. Actually, I found a fix and sorry I did not know that I had to give entire code. I had to use iter function instead of ET.Element(nonTmnl). – Karthik Vishwambar Jun 06 '18 at 17:02
  • Providing the entire code is usually not a good idea. I asked for a [mcve]. – mzjn Jun 06 '18 at 19:38

0 Answers0