FSK B

>b's weblog

News. Journal. Whatever.

Wes Geistes Kind die sogenannte Kinderhilfe istDiese Zeitungen muss man nicht mehr lesen

How to quickly parse a language to XML with pyPEG

Because this is a common task, maybe this little sample can be helpful. The following backend function writes out a pyAST to XML:

from xml.sax.saxutils import escape

def pyAST2XML(pyAST):
    if isinstance(pyAST, str):
        return escape(pyAST)
    if isinstance(pyAST, tuple):
        result = "<" + pyAST[0].replace("_", "-") + ">"
        for e in pyAST[1:]:
            result += pyAST2XML(e)
        result += "</" + pyAST[0].replace("_", "-") + ">"
    else:
        result = ""
        for e in pyAST:
            result += pyAST2XML(e)
    return result

This sample program uses the implementation above to convert a simple language to XML:

from pyPEG import parseLine
from xmlast import pyAST2XML
import re

def call():  return symbol, "(", parms, ")", ";"
def symbol(): return re.compile(r"\w+")
def parms(): return symbol, -1, (",", symbol)
def program():  return call, -1, call

text = "f(x, y);"
result, rest = parseLine(text, program)
print pyAST2XML(result)

A test run:

vb@bayhorse:~/pyPEG % python sample3.py | xmlstarlet fo
<?xml version="1.0"?>
<program>
  <call>
    <symbol>f</symbol>
    <parms>
      <symbol>x</symbol>
      <symbol>y</symbol>
    </parms>
  </call>
</program>
vb@bayhorse:~/pyPEG % 
publiziert Sun, 17 May 2009 14:58:56 +0200

Zurück zum Blogindex