lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

Klaas-Jan Stol wrote:
> for sake of completeness, I just updated the Lua wiki with a link to a 
> Lua 5 grammar for Bison (or Yacc).

Oh, nice! But you forgot the URL: http://lua-users.org/wiki/LuaGrammar

> I'm using this grammar in my attempt 
> to write a Lua compiler targeting Parrot, the new virtual machine for 
> Perl6.

Since you are restarting the Lua/Parrot project at this time,
maybe it would be a good idea to use the Lua 5.1 grammar?

It's not that different from Lua 5.0. We gained two new operators
('*x' to get the object size and 'x%y' for modulo) and the
ellipsis token ('...') is now a valid multi-result expression
representing the variable arguments inside a varargs function.


BTW: Here are some semantic challenges for your compiler:

local x,y,z = 1,...   f(1,...)   f(1,g())   f{1,...}   f{1,g()}
local x,y,z = 1,(...) f(1,(...)) f(1,(g())) f{1,(...)} f{1,(g())}
local x,y,z = 1,...,3 f(1,...,3) f(1,g(),3) f{1,...,3} f{1,g(),3}
local x,y,z = 1,2,3,...
local x,y,z = 1,2,3,g()
local x,y,z = 1,f(2,{3,...},g({4,h()},...))

And here's a tough one to check that you got the semantics for
lexical scoping, closures and coroutines right (same in 5.0/5.1):

local function f(x)
  return coroutine.wrap(function(n)
    repeat x=x+n; n=coroutine.yield(x) until false end),
    coroutine.wrap(function(n)
    repeat x=x*n; n=coroutine.yield(x) until false end)
end

local a,b=f(3)
local c,d=f(5)
assert(d(b(c(a(d(b(c(a(1)))))))) == 168428160)

Bye,
     Mike