lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sun, Dec 14, 2014 at 6:14 PM, Sean Conner <> wrote:

  The rules I use for this type of thing look like:

re = require "re" -- it's still part of LPeg.
                  -- and I find it easier to follow the grammar

parser = re.compile [[
        expr    <- term   (termop   term)*
        term    <- factor (factorop factor)*
        factor  <- number / open expr close

        space           <- %s*
        number          <- space {'-'^-1 [0-9]^+1} space
        termop          <- space {[+-]} space
        factorop        <- space {[*/]} space
        open            <- space '('    space
        close           <- space ')'    space


I'm glad you posted this! Someone, somewhere, is going to search for 'left recursion lpeg' and solve their problem. 

I've been merrily rewriting my grammar and am pleased to find it's working. My issue is that lpeg won't pass a valid grammar because empty-return propagation doesn't account for the predicate in #patt. I think this should change, the additional complexity to the library is at compile-time. 

The grammar, as written, will not loop, and the compiler can reason through the call chain, just like we do, to confirm that.