下面是lua2.1的文法,来自lua源代码中的lua.stx文件
终结符使用黑体表示
非终结符使用斜体表示
functionlist -> null
| functionlist stat sc
| functionlist function
| functionlist setdebug
function -> function name body
method -> function name : name body
body -> ( parlist ) block end
statlist -> null
| statlist stat sc
sc -> null | ;
stat -> stat1
cond -> expr1
stat1 -> if expr1 then PrepJump block Prejump elsepart end
| while expr1 do PrepJump block PreJump end
| repeat block until cond PrepJump
| varlist = exprlist1
| functioncall
| local localdeclist decinit
elsepart -> null
| else block
| elseif cond then PrepJump block PrepJump elsepart
block -> statlist ret
ret -> null
| return exprlist sc
PrepJump -> null
expr1 -> expr
expr -> ( expr )
| expr1 == expr1
| expr1 < expr1
| expr1 > expr1
| expr1 ~= expr1
| expr1 <= expr1
| expr1 >= expr1
| expr1 + expr1
| expr1 - expr1
| expr1 * expr1
| expr1 / expr1
| expr1 ^ expr1
| expr1 .. expr1
| 有个表达式暂时没想明白,暂时不写上去
| table
| varexp
| number
| string
| nil
| functioncall
| not expr1
| expr1 and PreJump expr1
| expr1 or PrepJump expr1
table -> { filedlist }
functioncall -> funcvalue funcParams
funcvalue -> varexp
| varexp : name
funcParams -> ( exprlist )
| table
exprlist -> null | exprlist1
exprlist1 -> expr | exprlist1 , expr
parlist -> parlist1
parlist1 -> name
| parlist1 , name
fieldlist -> null
| lfieldlist1 lastcomma
| ffieldlist1 lastcomma
| lfieldlist1 ; ffieldlist1 lastcomma
lastcomma -> null | ,
ffieldlist1 -> ffield
| ffieldlist1 , ffield
ffield -> name = expr1
lfieldlist1 -> expr1
| lfieldlist1 , expr1
varlist1 -> var
| varlist1 , var
var -> singlevar
| varexp [ expr1 ]
| varexp . name
singlevar -> name
varexp -> var
localdeclist -> name
| localdeclist , name
decinit -> null
| = exprlist1
setdebug -> $debug