lua-users home
lua-l archive

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

On 26-Sep-05, at 7:40 AM, carol.hurwitz wrote:

I am working on a master's thesis in which I working with a group that has been developing tools that extend the lex/yacc (flex/bison) capabilities. One of those extensions is to handle significant whitespace, which appears in modern languages such as python and javascript Lua falls into that category as it has optional semicolons, and thus newlines become significant.

Lua's optional semicolons are not the same as javascript's optional semicolons. (I call the Lua style "semioptional semicolons" :). Newlines are not really significant, except in one case where a newline is an error. See the response to question 3.

I will be writing flex/bison type files, adapted to our format. The name of the project is Harmonia.[]. We are also working on handling embedded languages, and lua provides us with a good language for testing our tools.

At the moment I am looking at lua from a theoretical point of view, rather than as a programmer. I have written almost no code. I hope to introduce a junior/senior level course in my department with some game programming, at which time, I will have to have actually learned to program in lua. But not until I finish this project!!

Personally, I think Lua is a very useful language -- and I've never written a game in my life :)

1. What is the difference between chunk and block grammatically? My understanding is that chunk is the start symbol and that you view a "program" as a chunk, not a block. Within all the productions, only block is used, but since it is defined as a chunk, I don't really understand the intention or the precise meaning of the grammar.

In Lua 5.1, a chunk is semantically a function with prototype (...). In previous versions it was semantically a function with prototype (). Syntactically, there is no distinction (but see below).

2. Somewhere it is stated that a #-line can be the first line in a chunk so that it can be run as a lua script. The definition of block would mean that you could have such statements sprinkled throughout the the code, i.e., wherever there is a block. Is that what is intended? And is it used that way?

No, a # comment line can only be the first line in a chunk; this is actually a chunk transformation performed prior to parsing, and is intended to allow the use of Unix #! lines.

3. How do you do line continuation?  Do you use '\'?

Lines are not syntactically significant so there is no need to "do line continuation". However, there are (as always) a few exceptions.

1) Ordinary comments extend from -- to the end of the line. There is no mechanism for continuing such comments (although there is a "long comment" syntax which is explicitly delimited and consequently can cross line endings).

2) Ordinary string literals cannot include line-end characters unless such characters are escaped with \. This introduces a line-end character into the string literal, so it is not really a line continuation character.

3) The grammar includes an ambiguity. Function calls are expressed, as in many languages, with a postfix '(' <expression-list> ') notation. However, there are statements which may end with an expression, and statements which may begin with an expression; this creates a shift-reduce conflict which is resolved in favour of the shift. However, the function-call is a syntactic error if a newline intervenes between the function-expression and the '('. In this case, and only in this case, a ';' must be used to separate the statements.


The following is a syntactically correct function call statement:

  (a or b)(c or d)(e or f)(g or h)

(That is, it is three consecutive function calls, the first two of which are expected to return a function.)

The grammatical ambiguity introduces the possibility that this could be parsed as two statements:
  (a or b)(c or d); (e or f)(g or h)
but the ambiguity is resolved in favour of shift, so the first interpretation is used.

If two statements were actually desired, it would not be possible to write them with a newline (hence my claim that newlines are not syntactically significant); the following is a syntax error:

  (a or b)(c or d)
-- error
  (e or f)(g or h)

In this case, the use of a semi-colon is necessary; the statements could be written in any of the following ways (amongst others). (I use style (2) in code generation, by the way.)

  (a or b)(c or d); (e or f)(g or h)

  (a or b)(c or d)
  ; (e or f)(g or h)

  (a or b)(c or d);
  (e or f)(g or h)

4. Is there a nice little starter set of examples that I could look at? Other than what is in the manual?

There is a set of test programs in the Lua distribution. Also see Roberto Ierusalimischy's excellent book Programming in Lua ( which has a nice collection of example programs.