Rule

public struct Rule<Param>

Represents a rule of the grammar.

A rule is similar to a rule in a context-free grammar, and has the form

L => R1 ... Rn

where L is the left-hand side of the rule, and R1 ... Rn is the right-hand side of the rule.

Unlike the rules of a context-free grammar though, each of the symbols L, R1, …, Rn carries an input and an output parameter with them. The property eval (in tandem with the property initialEnv) is responsible for computing these parameters along the stages of the parsing process.

Furthermore, the symbol L does not have to be a nonterminal, but can also be a terminal symbol. In this case, the invocation of this rule during parsing spawns a separate parsing process with a grammar identical with the current grammar, except that L is now treated as a nonterminal symbol. This enables a form of scannerless parsing.

Seealso

Grammar

Seealso

EvalFunc
  • lhs

    The left-hand side L of a rule of the form L => R1 ... Rn.

    Declaration

    Swift

    public let lhs: Symbol
  • rhs

    The right-hand side [R1 ... Rn] of a rule of the form L => R1 ... Rn

    Declaration

    Swift

    public let rhs: [Symbol]
  • The initial environment (actually a copy of it) is passed to eval at stage 0.

    Seealso

    EvalFunc

    Declaration

    Swift

    public let initialEnv: EvalEnv
  • The evaluation function responsible for this rule.

    Declaration

    Swift

    public let eval: EvalFunc<Param>
  • Creates a rule.

    Declaration

    Swift

    public init(lhs: Symbol, rhs: [Symbol], initialEnv: EvalEnv, eval: @escaping EvalFunc<Param>)

    Parameters

    lhs

    The left-hand side L of the rule.

    rhs

    The right-hand side R1 ... Rn of the rule.

    initialEnv

    The initial environment of the rule.

    eval

    The evaluation function of the rule.