Lua4z
A binary distribution of Lua for z/OS, with batteries

Complete syntax of Lua

Here is the complete syntax of Lua represented in railroad diagrams.

These diagrams are based on the Extended Backus-Naur Form (EBNF) notation in the Lua Reference Manual.

Chunk

Statement ; LastStatement ;

referenced by:

Block

Chunk

referenced by:

Statement

VariableList = ExpressionList FunctionCall do Block end while Expression do Block end repeat Block until Expression if Expression then Block elseif else Block end for Name = Expression , Expression , Expression do Block end for NameList in ExpressionList do Block end function FunctionName FunctionBody local function Name FunctionBody local NameList = ExpressionList

referenced by:

LastStatement

return ExpressionList break

referenced by:

FunctionName

Name . : Name

referenced by:

VariableList

Variable ,

referenced by:

Variable

Name PrefixExpression [ Expression ] PrefixExpression . Name

referenced by:

NameList

Name ,

referenced by:

ExpressionList

Expression , Expression

referenced by:

Expression

nil false true Number String ... Function PrefixExpression TableConstructor Expression BinaryOperator Expression UnaryOperator Expression

referenced by:

PrefixExpression

Variable FunctionCall ( Expression )

referenced by:

FunctionCall

PrefixExpression args PrefixExpression : Name Arguments

referenced by:

Arguments

( ExpressionList ) TableConstructor String

referenced by:

Function

function FunctionBody

referenced by:

FunctionBody

( ParameterList ) Block end

referenced by:

ParameterList

NameList , ... ...

referenced by:

TableConstructor

{ FieldList }

referenced by:

FieldList

Field FieldSeparator FieldSeparator

referenced by:

Field

[ Expression ] = Expression Name = Expression Expression

referenced by:

FieldSeparator

, ;

referenced by:

BinaryOperator

+ - * / ^ % .. < <= > >= == ~= and or

referenced by:

UnaryOperator

- not #

referenced by:

About these diagrams

The diagrams were generated by the Railroad Diagram Generator.

R R

Before generating the diagrams, the following changes were made to the EBNF notation from the Lua Reference Manual:

  • The notation was converted to match the EBNF used by the W3C:

    EBNF notation differences
    Meaning Lua Reference Manual W3C
    0 or more occurrences of A {A} (A)*
    Optional A [A] (A)?
  • Some symbol names were expanded for readability.

  • Literal strings (as opposed to EBNF symbol names) were enclosed in quotation marks.

EBNF

Here is the EBNF notation that was used to generate the diagrams.

Chunk ::= (Statement (";")?)* (LastStatement (";")?)?

Block ::= Chunk

Statement ::=  VariableList "=" ExpressionList |
  FunctionCall |
  "do" Block "end" |
  "while" Expression "do" Block "end" |
  "repeat" Block "until" Expression |
  "if" Expression "then" Block
    ("elseif" Expression "then" Block)*
    ("else" Block)? "end" |
  "for" Name "=" Expression "," Expression ("," Expression)?
    "do" Block "end" |
  "for" NameList "in" ExpressionList
    "do" Block "end" |
  "function" FunctionName FunctionBody |
  "local" "function" Name FunctionBody |
  "local" NameList ("=" ExpressionList)?

LastStatement ::= "return" (ExpressionList)? | "break"

FunctionName ::= Name ("." Name)* (":" Name)?

VariableList ::= Variable ("," Variable)*

Variable ::=  Name |
  PrefixExpression "[" Expression "]" |
  PrefixExpression "." Name

NameList ::= Name ("," Name)*

ExpressionList ::= (Expression ",")* Expression

Expression ::=  "nil" | "false" | "true" |
  Number | String | "..." | Function |
  PrefixExpression | TableConstructor |
  Expression BinaryOperator Expression |
  UnaryOperator Expression

PrefixExpression ::= Variable |
  FunctionCall |
  "(" Expression ")"

FunctionCall ::= PrefixExpression args |
  PrefixExpression ":" Name Arguments

Arguments ::=  "(" (ExpressionList)? ")" |
  TableConstructor | String

Function ::= "function" FunctionBody

FunctionBody ::= "(" (ParameterList)? ")" Block "end"

ParameterList ::= NameList ("," "...")? | "..."

TableConstructor ::= "{" (FieldList)? "}"

FieldList ::= Field (FieldSeparator Field)* (FieldSeparator)?

Field ::= "[" Expression "]" "=" Expression |
  Name "=" Expression | Expression

FieldSeparator ::= "," | ";"

BinaryOperator ::= "+" | "-" | "*" | "/" |
  "^" | "%" | ".." |
  "<" | "<=" | ">" | ">=" |
  "==" | "~=" |
  "and" | "or"

UnaryOperator ::= "-" | "not" | "#"