Arpeggio 0.10

Arpeggio - PEG parser

Arpeggio is a part of the research project whose main goal is building environment for DSL development.
The main domain of application is IDE for DSL development but it can be used for all
sort of general purpose parsing.

Installation

Arpeggio is written in Python programming language and distributed with setuptools support.
Install it with from pypi with the following command:

pip install Arpeggio

Or from source with:

python setup.py install

after installation you should be able to import arpeggio python module with:

import arpeggio

Quick start

Write a grammar. There are several ways to do that:

The canonical grammar format uses Python statements and expressions.
Each rule is specified as Python function which should return a data
structure that defines the rule. For example a grammar for simple
calculator can be written as:

The second and third options are implemented using canonical first form.
Feel free to implement your own grammar syntax if you don’t like these
(see modules arpeggio.peg and arpeggio.cleanpeg).

Instantiate a parser. Parser works as grammar interpreter. There is no code generation.

fromarpeggioimportParserPythonparser=ParserPython(calc)# calc is the root rule of your grammar# Use param debug=True for verbose debugging# messages and grammar and parse tree visualization# using graphviz and dot

Parse your inputs.

parse_tree=parser.parse("-(4-1)*5+(2+4.67)+5.89/(.2+7)")

Analyze parse tree directly or write a visitor class to transform it to a more
usable form. See examples how it is done.

For textual PEG syntaxes instead of ParserPyton instantiate ParserPEG
from arpeggio.peg or arpeggio.cleanpeg modules. See examples how it is done.

To debug your grammar set debug parameter to True. A verbose debug
messages will be printed and a dot files will be generated for parser model (grammar)
and parse tree visualization.

Here is an image rendered using graphviz of parser model for ‘calc’ grammar.

And here is an image rendered for parse tree for the above parsed calc expression.

If you are building a domain-specific language then I suggest you to take a look at textX.

Make a pull request. I will look at the changes and if everything is ok I will pull it in.

Note: For code contributions please try to adhere to the PEP-8 guidelines. Although I am not strict in that regard it is useful to have a common ground for coding style. To make things easier use tools for code checking (PyLint, PyFlakes, pep8 etc.).

Why is it called arpeggio?

In music, arpeggio is playing the chord notes one by one in sequence. I came up with the name by thinking that parsing is very similar to arpeggios in music. You take tokens one by one from an input and make sense out of it – make a chord!

Well, if you don’t buy this maybe it is time to tell you the truth. I searched the dictionary for the words that contain PEG acronym and the word arpeggio was at the top of the list ;)