Citron Parser Generator

Citron is an LALR parser generator for Swift. For a given input grammar, Citron creates a reentrant type-safe parser in Swift.

Features

Origins

Citron is adapted from the Lemon parser generator by Richard Hipp, the creator of SQLite. Lemon is used to generate the parser that parses SQL statements in SQLite.

Requirements

Using Citron requires Swift 4. The parser has no dependancies other than the Swift Standard Library. The lexer is dependant on Foundation for the use of regular expressions.

Using Citron

To make use of Citron, we should:

  1. Create a grammar file

    The grammar file contains the input grammar for which we’d like Citron to create a parser. It contains grammar rules, code blocks associated with the rules and Citron directives.

    See The Citron Grammar File for information on how to write a grammar file.

  2. Generate the parser

    To generate a parser, we should compile Citron and then run Citron on the grammar file.

    See Generating the parser for the commands that can accomplish this.

  3. Use the parser

    We can then use the parser class in our code, and provide it with inputs. We can optionally use Citron’s lexer to generate the inputs for the parser.

    See The Parsing Interface for information on how we can use the parser in our code.

Examples

A few examples of how Citron is used for parsing can be found in the “examples” folder in the project repository.

See also