Question

I am a bit confused by the meaning of * and + in syntax specification. I expect something like

rascal>syntax Statement = "{" {Statement ";"}* "}";

to mean a block of statements separated and ended by semicolons. But in the documentation it says:

A block of statements separated by semicolons

I do see in some code, such a syntax specification allows statements like { x = 1; x } to be parsed. Why is it like that?

Was it helpful?

Solution

There is just a different notation for separated lists and normal lists. Lists of statements that end with ; can be expressed using the sequence operator: (...).

For example:

 (Expr ";")* 

will accept

 1; 2; 3;

while

 {Expr ";"}*

will produce a parse error on the final ;, and rather accept:

 1; 2; 3

Separated lists have special semantics in the pattern matching and construction features of Rascal too. When we match for example using concrete syntax, empty sub-lists imply the preceding and following separators are ignored:

 ({Expr ","}*) `1 ; <{Expr ","}* rest>` := ({Expr ","}*) `1`;

This pattern will succeed, and bind the empty list of expressions to rest while ignoring the ; in the pattern.

Similarly, when we build a new list:

 ({Expr ","}*) `1 ; <{Expr ","}* rest>`

will produce simply 1 and remove the ; from the constructed list since rest was empty.

Separated lists are typically used to define lists of formal and actual parameters in function definitions and function applications and such. People usually do not use separators for statements with semi-colons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top