Lua Grammar |
|
"Section 8 - The Complete Syntax of Lua" of the Lua Reference Manual[1] provides an extended BNF grammar (without operator precedences) of Lua 5.1. For precedence, see [Section 2.5.6 - Precedence].
This is essentially the same as Lemon[2] format, except that it permits quoted strings for literal terminals
%fallback OPEN '(' .
chunk ::= block .
semi ::= ';' .
semi ::= .
block ::= scope statlist .
block ::= scope statlist laststat semi .
ublock ::= block 'until' exp .
scope ::= .
scope ::= scope statlist binding semi.
statlist ::= .
statlist ::= statlist stat semi .
stat ::= 'do' block 'end' .
stat ::= 'while' exp 'do' block 'end' .
stat ::= repetition 'do' block 'end' .
stat ::= 'repeat' ublock .
stat ::= 'if' conds 'end' .
stat ::= 'function' funcname funcbody .
stat ::= setlist '=' explist1 .
stat ::= functioncall .
repetition ::= 'for' NAME '=' explist23 .
repetition ::= 'for' namelist 'in' explist1 .
conds ::= condlist .
conds ::= condlist 'else' block .
condlist ::= cond .
condlist ::= condlist 'elseif' cond .
cond ::= exp 'then' block .
laststat ::= 'break' .
laststat ::= 'return' .
laststat ::= 'return' explist1 .
binding ::= 'local' namelist .
binding ::= 'local' namelist '=' explist1 .
binding ::= 'local' 'function' NAME funcbody .
funcname ::= dottedname .
funcname ::= dottedname ':' NAME .
dottedname ::= NAME .
dottedname ::= dottedname '.' NAME .
namelist ::= NAME .
namelist ::= namelist ',' NAME .
explist1 ::= exp .
explist1 ::= explist1 ',' exp .
explist23 ::= exp ',' exp .
explist23 ::= exp ',' exp ',' exp .
%left 'or' .
%left 'and' .
%left '<' '<=' '>' '>=' '==' '~=' .
%right '..' .
%left '+' '-' .
%left '*' '/' '%' .
%right 'not' '#' .
%right '^' .
exp ::= 'nil'|'true'|'false'|NUMBER|STRING|'...' .
exp ::= function .
exp ::= prefixexp .
exp ::= tableconstructor .
exp ::= 'not'|'#'|'-' exp . ['not']
exp ::= exp 'or' exp .
exp ::= exp 'and' exp .
exp ::= exp '<'|'<='|'>'|'>='|'=='|'~=' exp .
exp ::= exp '..' exp .
exp ::= exp '+'|'-' exp .
exp ::= exp '*'|'/'|'%' exp .
exp ::= exp '^' exp .
setlist ::= var .
setlist ::= setlist ',' var .
var ::= NAME .
var ::= prefixexp '[' exp ']' .
var ::= prefixexp '.' NAME .
prefixexp ::= var .
prefixexp ::= functioncall .
prefixexp ::= OPEN exp ')' .
functioncall ::= prefixexp args .
functioncall ::= prefixexp ':' NAME args .
args ::= '(' ')' .
args ::= '(' explist1 ')' .
args ::= tableconstructor .
args ::= STRING .
function ::= 'function' funcbody .
funcbody ::= params block 'end' .
params ::= '(' parlist ')' .
parlist ::= .
parlist ::= namelist .
parlist ::= '...' .
parlist ::= namelist ',' '...' .
tableconstructor ::= '{' '}' .
tableconstructor ::= '{' fieldlist '}' .
tableconstructor ::= '{' fieldlist ','|';' '}' .
fieldlist ::= field .
fieldlist ::= fieldlist ','|';' field .
field ::= exp .
field ::= NAME '=' exp .
field ::= '[' exp ']' '=' exp .
--RiciLake
The original C parser of Lua, of course, exists in the source code of the Lua distribution, but it is hand-written in C and not easy to reuse.
LpegRecipes lists a Lua 5.1 lexer and parser in Lua LPeg.
Cheese[3] is a parser generator that uses Parsing Expression Grammars (PEG) as its description formalism and Lua itself as a description language for the parsers. It includes a parser that understands the complete syntax of Lua 5.1. (The PEG appears to be an effort that predates LPeg.)
[Yueliang] implements a full Lua compiler in Lua. It is a fairly direct port of the Lua C code to Lua.
Parrot [4] contains a compiler, written in Perl5, that compiles Lua 5.1 down to Parrot VM bytecodes (though it's a work in progress). There is also a web interface to the subversion repository [5].
See also related efforts:
Those reimplementing Lua may be interested in the test suite for 5.1 LuaList:2006-03/msg00716.html and 5.0 LuaList:2005-12/msg00312.html.
ParseIn August 2004, Rici Lake announced a new Lua 5.0 parser on the lua mailing list, implemented in Lua: [LuaParse].
[Lua2js] contains a complete grammar for Lua 5.0 done with the Gentle[6] compiler construction kit.
The lua-xgettext tool includes a parser in Haskell/Parsec: LuaList:2006-10/msg00609.html
A mostly complete C++ parser of Lua 5.0, which is using Spirit from Boost: http://article.gmane.org/gmane.comp.parsers.spirit.general/4846
This seems like a nice start for reusing in your own things that need a Lua interpreter in C++. The author has abandoned the project, and the above is the latest version as of February 2004. In February 2005, I looked at this grammar closer to try to use it. There are a number of problems with it:
The last point is a show-stopper for me. Thus I have failed to make Spirit parse Lua correctly.
-- Asger Ottar Alstrup (aalstrup at laerdal.dk)
Moved to LuaFourOneGrammar
An incomplete interpreter of Lua 4 implemented in Java: http://www.hetland.org/jlua/ (broken link)
[Lua-ml] is an implementation of Lua 2.5 in OCaml. The parser is constructed with some OCaml parser generator, wrapped up in some literate programming system.