| Home | Trees | Indices | Help |
|---|
|
|
Lepl is a parser library written in Python.
This is the API documentation; the module index is at the bottom of this page. There is also a manual which gives a higher level overview.
The home page for this package is the Lepl website.
A simple example of how to use Lepl:
from lepl import *
# For a simpler result these could be replaced with 'list', giving
# an AST as a set of nested lists
# (ie replace '> Term' etc with '> list' below).
class Term(List): pass
class Factor(List): pass
class Expression(List): pass
def build():
# Here we define the grammar
# A delayed value is defined later (see penultimate line in block)
expr = Delayed()
number = Digit()[1:,...] >> int
# Allow spaces between items
with DroppedSpace():
term = number | '(' & expr & ')' > Term
muldiv = Any('*/')
factor = term & (muldiv & term)[:] > Factor
addsub = Any('+-')
expr += factor & (addsub & factor)[:] > Expression
line = Trace(expr) & Eos()
return line.get_parse()
if __name__ == '__main__':
parser = build()
# parser returns a list of tokens, but line
# returns a single value, so take the first entry
print(parser('1 + 2 * (3 + 4 - 5)')[0])
Running this gives the result:
Expression
+- Factor
| `- Term
| `- 1
+- '+'
`- Factor
+- Term
| `- 2
+- '*'
`- Term
+- '('
+- Expression
| +- Factor
| | `- Term
| | `- 3
| +- '+'
| +- Factor
| | `- Term
| | `- 4
| +- '-'
| `- Factor
| `- Term
| `- 5
`- ')'
Version: 4.3.1
|
|||
| |||
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Mon Jun 21 19:34:23 2010 | http://epydoc.sourceforge.net |