[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Problem with LPEG- grammar
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 21 Oct 2008 16:18:12 -0200
> require"lpeg"
>
> unaryOp = "!" --unary Operator
> binaryOp = "&&" --binary Operator
> expression = lpeg.P{
> "Exp"; --Rule 1
> Exp = lpeg.V"uExp" + lpeg.V"bExp",
> uExp = lpeg.V"Exp" * unaryOp,
> bExp = lpeg.V"Exp" * binaryOp * lpeg.V"Exp",
> }
>
> This (simplified beyond sensefulness) piece of code fails: "rule
> 'bExp' is left recursive" says the interpreter (but uExp works fine).
> But wheres the problem? If it wasnt recursive, i wouldn't use a
> grammar table... Am I doing something wrong? Or is this the reason
> for the "0.9" version number of the LPEG- lib?
This has nothing to do with 0.9. Like any other top-down parser, LPEG
cannot handle left recursion. You still can (and must) use grammars
for recursion, only left recursion is problematic. In your case,
a bExp may start with an Exp that may start with a bExp... Your grammar
is also ambiguous, so practically any parser tool will not accept it.
The reference manual for LPEG has an example that is exactly a grammar
for expressions. You may check it for some ideas.
-- Roberto