Getting started

This page installs fpeg, writes a first grammar, parses an input with it and turns the result into Python values. It takes about five minutes.

Install

fpeg needs Python 3.11 or newer, and installs from PyPI:

pip install fpeg

Then check that it imports, which prints its version:

python -c "import fpeg; print(fpeg.__version__)"

A first grammar

A grammar is a string of rules, loaded at runtime. This one reads pairs such as x = 1, separated by commas:

import fpeg

grammar = fpeg.Grammar(r"""
start: pair ** ","
pair: key=NAME "=" value=NUMBER
NAME: [a-z]+
NUMBER: [0-9]+
%ignore " "+
""")
  • Lowercase names are rules, which give the tree its nodes. Uppercase names are terminals, which match the tokens.

  • "=" is a literal, [a-z] a class of characters, and + means one or more.

  • pair ** "," matches pairs separated by commas, none or many.

  • key= and value= name the parts of a pair as fields.

  • %ignore names the text skipped between tokens, here spaces.

Parsing

parse returns the tree of an input, starting at the rule start:

tree = grammar.parse("x = 1, y = 22")
print(tree.pretty())
start
  pair
    key=x
    value=1
  pair
    key=y
    value=22

Each node is a Tree with the name of its rule in data and what it matched in children. A token is a str that knows its terminal and its place in the input:

pair = tree.children[1]
assert pair.data == "pair"
value = pair.field("value")
assert value == "22"
assert (value.type, value.line, value.column) == ("NUMBER", 1, 12)

Input that the grammar does not match raises ParseError, which says where the parse failed and what could have come there:

try:
    grammar.parse("x = 1, y")
except fpeg.ParseError as error:
    print(error)
line 1, column 9: expected "="

Values

A Transformer turns a tree into values of your own. Each method is named after a rule and returns the value of its nodes. It is called after the children of its node, so it gets their values, and its signature says how: *pairs takes the children, key and value take the fields.

class ToDict(fpeg.Transformer):
    def start(self, *pairs):
        return dict(pairs)

    def pair(self, key, value):
        return str(key), int(value)


print(ToDict().transform(tree))
{'x': 1, 'y': 22}

A grammar can also say what its nodes and tokens are worth, with value builders after =>, and a plain Transformer then needs no methods at all:

grammar = fpeg.Grammar(r"""
start: pair ** "," => dict
pair: key=NAME "=" value=NUMBER => tuple
NAME: [a-z]+ => text
NUMBER: [0-9]+ => int
%ignore " "+
""")

assert fpeg.Transformer().transform(grammar.parse("x = 1, y = 22")) == {"x": 1, "y": 22}

Where next