lua-users home
lua-l archive

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



On Feb 26, 2005, at 13:01, Asger Ottar Alstrup wrote:

The Lua grammar contains these three lines to describe the syntax for function calls:

	functioncall = p args | p ':' Name args
	p = var | functioncall | '(' exp ')'
	var = Name | p '[' exp ']' | p '.' Name

That is left recursive in strange ways, and thus Greek to me. Based on my understanding of the language, I've tried to rewrite it to something which is easier to understand for me. Does this match the grammar?

	functioncall =
		( Name | '(' exp ')' )
		( args | '.' Name | ':' Name | '[' exp ']' )*
		args

No.

The way to get to grips with grammar is just to play around. In this case you'd do well working from the right-hand end.

It's clear that all functioncalls must end in an args (from the first line of the grammar you quoted). That leaves the bit before the arguments. Let's call that the specifier. What can that look like? Well, it can either be a p or p ':' Name.

Perhaps at this point you'd better work through some cases. But note that p can be a functioncall, so you have things like:

f "a" "b" or f("a")("b")

p can also be a sort of generalised variable, like a simple name like

f(x)

or a more complex variable like
a[7](x)
or
a(y)[7].foo(x)

And for all of those you can just sneak a ':' Name in just before the (final) args, like this

a(y)[7].foo:meth(x)

Well, I don't know if that helps, but I never knew you could go f"a""b" before reading that grammar extract. :)

David Jones