How to properly implement the BNF grammar described in the Lua manual?

142 Views Asked by At

This is a portion of the BNF grammar for Lua described in the Lua manual section 8:

    var ::=  Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name 

    exp ::=  nil | false | true | Number | String | `...´ | function | 
         prefixexp | tableconstructor | exp binop exp | unop exp 

    prefixexp ::= var | functioncall | `(´ exp `)´

As you can see, it contains one recursive reference between var and prefixexp, which ends in an infinite loop. I think this is a case of left recursion, although indirect, because if you inline the definition of var, prefixexp ends like this:

    prefixexp ::= Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name  | functioncall | `(´ exp `)´

I'm not sure if this grammar is supposed to be directly implementable (despite the lack of operator precedence) or if it will require some transformation first.

0

There are 0 best solutions below