lua 5.3语法分析的BNF

看lua解释器源代码的时候发现和lua官网上给的有点出入,自己记录一下
官网的文档:https://www.lua.org/manual/5.3/manual.html,直接在页面搜索BNF就行了
以下是从源码的注释里扣出来的,源码注释有些地方写的有点问题,可能是作者版本迭代的时候忘记改注释了,帮作者改了一点

block 		-> statlist
statlist 	-> { stat [';'] }
fieldsel 	-> ['.' | ':'] NAME
index 		-> '[' expr ']'
constructor -> '{' [ field { sep field } [sep] ] '}'
sep 		-> ',' | ';'
field 		-> listfield | recfield
recfield 	-> (NAME | '['exp1']') = exp1
listfield 	-> exp
body 		->  '(' parlist ')' block END
parlist 	-> [ param { ',' param } ]
param 		-> NAME | '...'
explist 	-> expr { ',' expr }
funcargs 	-> '(' [ explist ] ')' | constructor | STRING
primaryexp 	-> NAME | '(' expr ')'
suffixedexp -> primaryexp { '.' fieldsel | '[' exp ']' | ':' NAME funcargs | funcargs }
simpleexp 	-> FLT | INT | STRING | NIL | TRUE | FALSE | ... | constructor | FUNCTION body | suffixedexp
subexpr 	-> (simpleexp | unop subexpr) { binop subexpr }
assignment 	-> ',' suffixedexp assignment | '=' explist
cond 		-> exp
label 		-> '::' NAME '::'
whilestat 	-> WHILE cond DO block END
repeatstat 	-> REPEAT block UNTIL cond
forstat 	-> FOR (fornum | forlist) END
fornum 		-> NAME = exp1,exp1[,exp1] forbody 
forlist 	-> NAME {,NAME} IN explist forbody
forbody 	-> DO block
stat 		-> LOCAL NAME {',' NAME} ['=' explist]  | RETURN [explist]
			| ';' (empty statement) | fistat | whilestat | DO block END | forstat | repeatstat 
			| funstat | localstat | label | retstat | breakstat | 'goto' NAME  | suffixedexp | assignment
ifstat 		-> IF cond THEN block { test_then_block } [ELSE block] END
test_then_block -> [IF | ELSEIF] cond THEN block
funcname 	-> NAME {fieldsel} [':' NAME]
funcstat 	-> FUNCTION funcname body

你可能感兴趣的:(lua)