Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
fpeg
Light Logo Dark Logo
  • Getting started

Examples

  • A calculator
  • A configuration file
  • CSV
  • JSON to Python objects
  • A filter language

Tutorials

  • A grammar for JSON
  • From a tree to your own objects

Guides

  • Operator precedence
  • Building an AST
  • Indentation
  • Interpolated strings
  • Composing grammars
  • A language over Python
  • Errors and recovery
  • Performance

Reference

  • Grammar language
  • Semantics
  • Parse tree
  • Python API
  • Command line
  • Diagnostics
    • E101 Syntax error
    • E102 Invalid escape
    • E103 Unknown Unicode property
    • E104 Number too large
    • E201 Unknown rule
    • E202 Undeclared flag
    • E203 Unknown attribute
    • E204 Defined twice
    • E205 Invalid name
    • E206 Wrong number of arguments
    • E207 Unknown field in a builder
    • E208 Missing builder function
    • E301 Syntactic rule in a terminal
    • E302 Form that does not fit the rule
    • E303 Precedence block of the wrong form
    • E304 Field in a transparent rule
    • E305 Field over several items
    • E306 Empty literal
    • E307 Character class
    • E308 Count out of range
    • E309 Builder without a place
    • E310 Builder arguments that do not fit
    • E401 Nesting too deep
    • E402 Grammar too large
    • E501 Not supported yet
    • E601 Unknown grammar
    • E602 A grammar takes itself in
    • E603 Imported and defined
    • E604 Not in the grammar imported
    • E605 Misplaced inherited alternatives
    • E901 Internal error
    • W101 Alternative that is never tried
    • W102 Literal that an earlier literal shadows
    • W103 Alternative that an earlier one shadows
    • W201 Repetition of an expression that can match nothing
    • W202 Rule that calls itself at both ends
    • W301 Transparent alternative whose shape depends on the input
    • W302 Builder on a transparent alternative that never makes a node
    • W401 Flag that is never set or never tested
    • W402 Parameter that its rule never uses
    • W403 Recovery for a label that nothing raises
  • Known limits

Explanation

  • How fpeg works
  • Benchmarks
  • Related projects

Project

  • Planned features
Back to top
View this page

Related projects¶

What other parsing tools are for, where fpeg differs, and what moving a grammar or a transformer from Lark to fpeg takes. The benchmarks measure several of these tools beside fpeg on the same inputs.

For Python¶

  • Lark reads a grammar in EBNF at runtime and parses with an Earley parser, which takes any context-free grammar, ambiguous ones included, or with an LALR(1) parser for grammars that fit it. Both are written in Python. For a grammar that is ambiguous by nature, or where every reading of an input is wanted, Earley parsing is the tool, and a parsing expression grammar is not: it gives every input at most one tree. fpeg’s notation and its Transformer follow Lark’s, as the section below shows.

  • pe reads parsing expression grammars at runtime too, and parses with a packrat parser or a parsing machine.

  • tree-sitter generates a parser in C from a grammar written in JavaScript, ahead of time, and parses again only what an edit changed. Editors use it to highlight and fold code as it is typed, and its Python binding loads the generated parsers. fpeg loads a grammar from text at runtime instead, and does not parse incrementally yet.

  • LibCST parses Python source, in native code, into a concrete syntax tree that keeps every space and comment, so that a program can change source code and write it back. It reads Python only, where fpeg reads any language its grammar describes, Python among them.

  • Python’s own parser, through the ast module, is the reference for Python source. fpeg’s Python grammar gives the same ast, which the tests check, for languages that add to Python.

For Rust¶

  • pest generates a parser for a parsing expression grammar at build time, from a grammar file that a derive macro reads, and its crate pest_vm runs a grammar given at runtime. Its rules return pairs of tokens that the program walks.

  • serde_json is a JSON parser for Rust written by hand for JSON alone. The benchmarks show it beside fpeg’s JSON grammar as the speed that a parser built for one format reaches.

Coming from Lark¶

A grammar for Lark reads much the same in fpeg:

Lark

fpeg

?rule, _rule, -> alias, _TERMINAL

the same

"select"i

the same

[item], item?

item?

item ("," item)*

the same, or (item ++ ",")

/[a-z]+/, a regular expression

[a-z]+, classes and repetition, since terminals are written in the grammar’s own notation

%import common.WS

%import common (WS)

%ignore WS

the same

_separated{x, sep}: x (sep x)*

separated<x, sep>: x (sep x)*

an Indenter after the lexer

%indent and %same in the grammar, as the indentation guide shows

TERMINAL.2, priorities

the order of alternatives, since a choice takes the first that matches

The main difference is the choice: Lark’s | lets the parser find the alternative that fits, and fpeg’s tries the alternatives in order and takes the first that matches, so a longer alternative goes before a shorter one that starts it. fpeg check warns about an alternative that an earlier one hides.

Code written for Lark’s Transformer mostly runs: a method named after a rule gets the list of the children, and fpeg.v_args takes the options inline, meta and tree of Lark’s decorator of that name. Tree(data, children, meta), pretty() and the token attributes type, start_pos, end_pos, line, column and end_column follow Lark’s.

import fpeg
from fpeg import Transformer, v_args

grammar = fpeg.Grammar(r"""
start: pair ("," pair)*
pair: NAME "=" NUMBER
NAME: [a-z]+
NUMBER: [0-9]+
%ignore " "+
""")


class ToDict(Transformer):
    def start(self, children):
        return dict(children)

    @v_args(inline=True)
    def pair(self, name, number):
        return str(name), int(number)


print(ToDict().transform(grammar.parse("x = 1, y = 22")))
{'x': 1, 'y': 22}

Two things differ on purpose. Tokens compare as plain strings, without Lark’s comparison of their types, and an exception in a method keeps its type instead of becoming Lark’s VisitError, with a note that says which method raised it. fpeg’s grammars also name fields, which Lark’s do not, and a method that names fields as its parameters gets them by name. The Python API lists every form a method can take.

Next
Planned features
Previous
Benchmarks
Copyright © 2026 Dominik Schwimmbeck
Made with Sphinx and @pradyunsg's Furo
On this page
  • Related projects
    • For Python
    • For Rust
    • Coming from Lark