8 – The Complete Syntax of Lua

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

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