Skip to the content.

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

Parsers made with Citron are:

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 additionally 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.

  4. Handle errors

    The easy way to handle errors is to catch the thrown errors, as described in The parsing interface.

    However, to be able to recover from errors, produce partial parse trees and generate contextual error messages, we should use Error capturing.

Examples

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

See also