9 – The Complete Syntax of Lua
Here is the complete syntax of Lua in extended BNF. As usual in extended BNF, {A} means 0 or more As, and [A] means an optional A. (For operator precedences, see §3.4.8; for a description of the terminals Name, Numeral, and LiteralString, see §3.1.)
chunk ::= blockblock ::= {stat} [retstat]stat ::= ‘;’ |varlist ‘=’ explist |functioncall |label |break |goto Name |do block end |while exp do block end |repeat block until exp |if exp then block {elseif exp then block} [else block] end |for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end |for namelist in explist do block end |function funcname funcbody |local function Name funcbody |local attnamelist [‘=’ explist]attnamelist ::= Name attrib {‘,’ Name attrib}attrib ::= [‘<’ Name ‘>’]retstat ::= return [explist] [‘;’]label ::= ‘::’ Name ‘::’funcname ::= Name {‘.’ Name} [‘:’ Name]varlist ::= var {‘,’ var}var ::= Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Namenamelist ::= Name {‘,’ Name}explist ::= exp {‘,’ exp}exp ::= nil | false | true | Numeral | LiteralString | ‘...’ | functiondef |prefixexp | tableconstructor | exp binop exp | unop expprefixexp ::= var | functioncall | ‘(’ exp ‘)’functioncall ::= prefixexp args | prefixexp ‘:’ Name argsargs ::= ‘(’ [explist] ‘)’ | tableconstructor | LiteralStringfunctiondef ::= function funcbodyfuncbody ::= ‘(’ [parlist] ‘)’ block endparlist ::= namelist [‘,’ ‘...’] | ‘...’tableconstructor ::= ‘{’ [fieldlist] ‘}’fieldlist ::= field {fieldsep field} [fieldsep]field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | expfieldsep ::= ‘,’ | ‘;’binop ::= ‘+’ | ‘-’ | ‘*’ | ‘/’ | ‘//’ | ‘^’ | ‘%’ |‘&’ | ‘~’ | ‘|’ | ‘>>’ | ‘<<’ | ‘..’ |‘<’ | ‘<=’ | ‘>’ | ‘>=’ | ‘==’ | ‘~=’ |and | orunop ::= ‘-’ | not | ‘#’ | ‘~’
