9 – The Complete Syntax of Lua

Here is the complete syntax of Lua in extended BNF. (It does not describe operator precedences.)

  1. chunk ::= block
  2.  
  3. block ::= {stat} [retstat]
  4.  
  5. stat ::= ; |
  6. varlist = explist |
  7. functioncall |
  8. label |
  9. break |
  10. goto Name |
  11. do block end |
  12. while exp do block end |
  13. repeat block until exp |
  14. if exp then block {elseif exp then block} [else block] end |
  15. for Name = exp , exp [‘, exp] do block end |
  16. for namelist in explist do block end |
  17. function funcname funcbody |
  18. local function Name funcbody |
  19. local namelist [‘= explist]
  20.  
  21. retstat ::= return [explist] [‘;’]
  22.  
  23. label ::= :: Name ::
  24.  
  25. funcname ::= Name {‘. Name} [‘: Name]
  26.  
  27. varlist ::= var {‘, var}
  28.  
  29. var ::= Name | prefixexp [ exp ] | prefixexp . Name
  30.  
  31. namelist ::= Name {‘, Name}
  32.  
  33. explist ::= exp {‘, exp}
  34.  
  35. exp ::= nil | false | true | Number | String | ... | functiondef |
  36. prefixexp | tableconstructor | exp binop exp | unop exp
  37.  
  38. prefixexp ::= var | functioncall | ( exp )
  39.  
  40. functioncall ::= prefixexp args | prefixexp : Name args
  41.  
  42. args ::= ( [explist] ) | tableconstructor | String
  43.  
  44. functiondef ::= function funcbody
  45.  
  46. funcbody ::= ( [parlist] ) block end
  47.  
  48. parlist ::= namelist [‘, ...’] | ...
  49.  
  50. tableconstructor ::= { [fieldlist] }
  51.  
  52. fieldlist ::= field {fieldsep field} [fieldsep]
  53.  
  54. field ::= [ exp ] = exp | Name = exp | exp
  55.  
  56. fieldsep ::= , | ;
  57.  
  58. binop ::= + | - | * | / | ^ | % | .. |
  59. < | <= | > | >= | == | ~= |
  60. and | or
  61.  
  62. unop ::= - | not | #
  63.