PEG matching/parsing lib in progress
spir
denis.spir at gmail.com
Mon Nov 8 04:35:57 PST 2010
On Mon, 8 Nov 2010 11:48:20 +0100
spir <denis.spir at gmail.com> wrote:
[...]
A third point to solve is the one of recursive patterns (self-, mutual, circular, recursion). The issue is indeed that patterns must be defined in order, which is not possible in case of recursion. A side issue is that patterns are identified by ref (pointer): we must keep this ref unchanged, a point that eliminates some possible solutions.
What I usually do is to define a kind of fake Recursion pattern type, that delegates matching to the actual one, recorded as attribute. Recursion is used to predefine (one of) the recursive pattern(s); then the rest of the grammar can be defined normally; finally, the recursive one gets its actual pattern. Example:
operation = new Recursion(); // placeholder
group = new Tuple(L_PAREN, operation, R_PAREN); // ( operation )
operand = new Choice(group, number, variable); // group|number|variable
operation.define(new Tuple(operand, operator, operand)); // operand operator operand
(We can use operator overloading to replace of the call to define().)
What do you think of this solution? (In addition to matching indirection, it raises a few side-issues in the implementation of some features, but I know how to solve them.)
Can you find other solutions?
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
More information about the Digitalmars-d
mailing list