Unclear grammar for global in 5.5

59 views
Skip to first unread message

Sergey Bronnikov

unread,
Jul 16, 2026, 7:16:34 AM (8 days ago) Jul 16
to lua-l
Hello,

Lua 5.5 BNF grammar contains rules for "local" and "global" names:

stat ::=  ‘;’ |
<snipped>
 local attnamelist [‘=’ explist] |
 global attnamelist |
<snipped>

I'm confused by the absence of the "[‘=’ explist]" in the rule for "global".
It seems that the code below is syntactically incorrect:

global a, b = 1, 2
or 
global a = 1
or
global <const> a = 1

However, the chunks above are successfully accepted by Lua:

$ ./lua -e "global a, b = 1, 2"; echo $?
0
$ ./lua -e "global a = 1"; echo $?
0
$ ./lua -e "global <const> a = 1"; echo $?
0

Where is a truth?

Also, it is suspicious that chunk below does not work:

$ ./lua -e "global a; print()"
./lua: (command line):1: variable 'print' not declared

The same with "local" works fine:

$ ./lua -e "local a; print()"; echo $?
0

Version: PUC Rio Lua 5.5, 84938a7d2b680d2d28ec99606e84fe712efd9a69

Luiz Henrique de Figueiredo

unread,
Jul 16, 2026, 7:49:45 AM (8 days ago) Jul 16
to lu...@googlegroups.com
> I'm confused by the absence of the "[‘=’ explist]" in the rule for "global".

This has been fixed in Lua 5.5.1. See
https://www.lua.org/work/doc/manual.html#9

Sergey Bronnikov

unread,
Jul 16, 2026, 8:08:20 AM (8 days ago) Jul 16
to lua-l
Thanks!

Is it a bug?

$ ./lua -e "global a; print()"
./lua: (command line):1: variable 'print' not declared

Sergey
 

Roberto Ierusalimschy

unread,
Jul 16, 2026, 8:47:35 AM (8 days ago) Jul 16
to lu...@googlegroups.com
> $ ./lua -e "global a; print()"
> ./lua: (command line):1: variable 'print' not declared

Please read the manual:

"outside any global declaration, Lua works as global-by-default.
Inside any global declaration, Lua works without a default: All
variables must be declared."

-- Roberto
Reply all
Reply to author
Forward
0 new messages