Skip to content

Latest commit

 

History

History

readme.md

Lua Grammar

Author

Kazunori Sakamoto, Alexander Alexeev, others

In particular, efforts by Matt Hargett pushed for the refinement of the grammar.

Specification

Manual

Reference

Source

parser (mirror)

lexer (mirror)

Github project (mirror)

Summary pages

Wikipedia

Notes on mutual recursion removal

The EBNF grammar specified in the Lua Manual contains mutual left recursion. This must be removed in order for Antlr4 to accept the grammar. The following steps are the refactorings that were done to bring the grammar.

Refactoring Rule
- var ::= Name | prefixexp '[' exp ']' | prefixexp '.' Name
left factor prefixexp var ::= Name | prefixexp ( '[' exp ']' | '.' Name )
- prefixexp ::= var | functioncall | '(' exp ')'
add parentheses prefixexp ::= ( var ) | functioncall | '(' exp ')'
unfold var prefixexp ::= ( Name | prefixexp ( '[' exp ']' | '.' Name ) ) | functioncall | '(' exp ')'
ungroup prefixexp ::= Name | prefixexp ( '[' exp ']' | '.' Name ) | functioncall | '(' exp ')'
reorder alts prefixexp ::= prefixexp ( '[' exp ']' | '.' Name ) | Name | functioncall | '(' exp ')'
remove immediate left recursion prefixexp ::= ( Name | functioncall | '(' exp ')' ) ( '[' exp ']' | '.' Name )*
- functioncall ::= prefixexp args | prefixexp ':' Name args
left factor functioncall ::= prefixexp ( args | ':' Name args )
add parentheses functioncall ::= ( prefixexp ) ( args | ':' Name args )
unfold prefixexp functioncall ::= ( ( Name | functioncall | '(' exp ')' ) ( '[' exp ']' | '.' Name )* ) ( args | ':' Name args )
reorder functioncall ::= ( ( functioncall | Name | '(' exp ')' ) ( '[' exp ']' | '.' Name )* ) ( args | ':' Name args )
group functioncall ::= ( ( functioncall | ( Name | '(' exp ')' ) ) ( '[' exp ']' | '.' Name )* ) ( args | ':' Name args )
fold functioncall ::= ( ( functioncall | b ) c ) d
b ::= ( Name | '(' exp ')' )
c ::= ( '[' exp ']' | '.' Name )*
d ::= ( args | ':' Name args )
unparen functioncall ::= ( functioncall | b ) c d
distribute functioncall ::= functioncall c d | b c d
remove left recursion functioncall ::= ( b c d) ( c d )*
unfold functioncall ::= ( ( Name | '(' exp ')' ) ( '[' exp ']' | '.' Name )* ( args | ':' Name args ) ) ( ( '[' exp ']' | '.' Name )* ( args | ':' Name args ) )*

Validating test suite files

The Lua files in the examples/lua-test-suite is a collection of source code from open source projects.

Files are classified into those that pass the Lua compiler, and those that pass the Antlr4 grammar for Lua. Any files that do not pass the Lua compiler are renamed with .lua.invalid. These files are not considered in testing this grammar. Any that passed the compiler but fail to parse with this grammar are renamed with .lua.fail.

To test the sources using the compiler, this Bash code comes in handy.

for i in `find . -name '*.lua'`
do
	echo $i
	luac54.exe $i
done

Scraped rules from lparser.c

statement : ... | exprstat | ...

exprstat : suffixedexp {la = '=' || la = ','}? restassign?

suffixedexp : primaryexp ( {la = '.'}? fieldsel | {la = '['}?  '[' expr ']'