9 – Lua 的完整语法

原文:https://www.lua.org/manual/5.4/manual.html

9 – The Complete Syntax of Lua – 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.)

​ 以下是 Lua 在扩展 BNF 中的完整语法。与扩展 BNF 中通常一样,{A} 表示 0 个或多个 A,[A] 表示一个可选的 A。(有关运算符优先级,请参见 §3.4.8;有关 Name、Numeral 和 LiteralString 终端的说明,请参见 §3.1。)

	chunk ::= block
	block ::= {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 ‘.’ Name 
	namelist ::= Name {‘,’ Name}
	explist ::= exp {‘,’ exp}
	exp ::=  nil | false | true | Numeral | LiteralString | ‘...’ | functiondef | 
		 prefixexp | tableconstructor | exp binop exp | unop exp 
	prefixexp ::= var | functioncall | ‘(’ exp ‘)’
	functioncall ::=  prefixexp args | prefixexp ‘:’ Name args 
	args ::=  ‘(’ [explist] ‘)’ | tableconstructor | LiteralString 
	functiondef ::= function funcbody
	funcbody ::= ‘(’ [parlist] ‘)’ block end
	parlist ::= namelist [‘,’ ‘...’] | ‘...’
	tableconstructor ::= ‘{’ [fieldlist] ‘}’
	fieldlist ::= field {fieldsep field} [fieldsep]
	field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
	fieldsep ::= ‘,’ | ‘;’
	binop ::=  ‘+’ | ‘-’ | ‘*’ | ‘/’ | ‘//’ | ‘^’ | ‘%’ | 
		 ‘&’ | ‘~’ | ‘|’ | ‘>>’ | ‘<<’ | ‘..’ | 
		 ‘<’ | ‘<=’ | ‘>’ | ‘>=’ | ‘==’ | ‘~=’ | 
		 and | or
	unop ::= ‘-’ | not | ‘#’ | ‘~’

last update: tue may 2 20:09:38 utc 2023

​ 最后更新:星期二 5月 2日 20:09:38 utc 2023

最后修改 October 10, 2024: 更新 (a4b8f85)