# BNF to LPEG

5 messages
Open this post in threaded view
|

## BNF to LPEG

 Is there a module that can convert BNF to LPEG? The test case being the Lua grammar in §9 of in the manual.
Open this post in threaded view
|

## Re: BNF to LPEG

 > Is there a module that can convert BNF to LPEG? > The test case being the Lua grammar in §9 of in the manual. Not that I know. Such a conversion is tricky for at least two reasons: - Usually, BNF assumes a separate scanner, while LPeg works directly on the character level. - Although BNF and LPeg may look similar, they have different meanings. (If the BNF is LL(n), then a corresponding LPeg would have equal meaning, but the scanner problem is still there. In particular, the Lua grammar in the manual is not LL(n).) -- Roberto
Open this post in threaded view
|

## Re: BNF to LPEG

 2016-04-07 15:22 GMT+02:00 Roberto Ierusalimschy <[hidden email]>: >> Is there a module that can convert BNF to LPEG? >> The test case being the Lua grammar in §9 of in the manual. > > Not that I know. Such a conversion is tricky for at least two reasons: > > - Usually, BNF assumes a separate scanner, while LPeg works directly > on the character level. > > - Although BNF and LPeg may look similar, they have different meanings. > (If the BNF is LL(n), then a corresponding LPeg would have equal > meaning, but the scanner problem is still there. In particular, the > Lua grammar in the manual is not LL(n).) I can understand that to get from BNF (or EBNF) to LPeg with equivalent functionality is highly nontrivial. When doing it manually, though, a very large part of the work is tedious but straightforward: precisely the sort of thing that computers do perfectly but humans don't. For example, just mechanically substituting notation. {something} becomes (something)^0 [something] becomes (something)^-1 | becomes + juxtaposition becomes multiplication Etc. After converting about twenty lines of a particular EBNF (not Lua's) in this way, and making the occasional mistake, I thought "This job will take less total time if I write a litlle Lua program". After writing about twenty lines of that program, I thought "someone must have done this before". Oh well. :-(