Source

ppparse /

Filename Size Date modified Message
demo
src/ppparse
74 B
1.0 KB
1.6 KB
540 B

ppparse -- a simple parser generator

The ppparse package generates a lexical scanner and a syntax parser for the given grammar. Use it as in the following example:

from ppparse import make_scan, make_parse

scan = make_scan("""
    SPACE   : [\s]+
            !skip
    NUMBER  : [\d]+
    SYMBOL  : [()*/+-]
            !symbol
""")

parse = make_parse("""
    expr    : term
            | expr `+` term
            | expr `-` term
    term    : factor
            | term `*` factor
            | term `/` factor
    factor  : atom
            | `+` factor
            | `-` factor
    atom    : NUMBER
            | `(` expr `)`
""")

text = "(-4+15)*4 - 2"
tokens = scan(text)
syntax = parse(tokens)

The function scan() generated by make_scan() takes a string or an open file as input and produces a list of Token objects. The function parse() generated by make_parse() takes a list of Token objects and produces a syntax tree composed of Syntax and Token objects.

A Token instance has the following attributes:

code: unicode
The token type; usually coincides with the name of the production rule which matched the token.
text: unicode
The token value.

Each Syntax instance has the following attributes:

code: unicode
The type of the syntax node; coincides with the name of the rule which produced the node.
arms: list of Syntax and Token objects
Nodes which matched the production rule.