lpeg.V

Creates a non-terminal variable for a grammar

Prototype

lpeg.V (v)

Description

This operation creates a non-terminal (a variable) for a grammar. The created non-terminal refers to the rule indexed by v in the enclosing grammar.

The call lpeg.V(v) creates a pattern that represents the nonterminal (or variable) with index v in a grammar. Because the grammar still does not exist when this function is evaluated, the result is an open reference to the respective rule.

A table is *fixed* when it is converted to a pattern (either by calling lpeg.P or by using it wherein a pattern is expected). Then every open reference created by lpeg.V(v) is corrected to refer to the rule indexed by v in the table.

For example:

-- Grammar
local Exp, Term, Factor = lpeg.V"Exp", lpeg.V"Term", lpeg.V"Factor"
G = lpeg.P{ Exp,
Exp = lpeg.Ct(Factor * (FactorOp * Factor)^0);
Factor = lpeg.Ct(Term * (TermOp * Term)^0);
Term = Number + Open * Exp * Close;
}

Lua functions

Topics