Re: Grammar for optional elements



I need to create a parser for a language something like this.

attribute1: value;
attribute2: value;
attribute3: value;

All the attributes are optional but can occur only once...

Parsing Expression Grammars are an interesting solution to the
problem,
but I don't think this one quite succeeds. In the term

!(attr* attr1 attr*)

it is my understanding is that the "*" operator is "greedy" and
will always consume the entire remainder of the string,

I would like to suggest an alternative. Using ABNF, which
differs only slightly in this example in its repetitions and ranges,
and
adding line enders for readability of the input string:

attr1 = name1 ":" value ";" CRLF !suffix1
suffix1 = *(!name1 any) name1
any = %d10-127
CRLF = %d13.10
....

I'm not familiar with ABNF, but I suspect, that adding the line enders
is not only for readability, but is essential for the success of the
example code. Wouldn't a suffix otherwise always consume or at least
look ahead the entire remainder of the string too?


By the way I present a solution with TextTransformer
(http://www.texttransformer.com), using semantic and sytactic
predicates:

{{
int mask = (1 << 1) | (1 << 2) | (1 << 3); // setting the first three bits
}}

// if a bit is still in the mask, test for the according attr
WHILE(mask & (1 << 1) && attr1() ||
mask & (1 << 2) && attr2() ||
mask & (1 << 3) && attr3() )
(
attr1 {{ mask &= ~(1 << 1); }} // deleting the first bit
| attr2 {{ mask &= ~(1 << 2); }} // deleting the second bit
| attr3 {{ mask &= ~(1 << 3); }} // deleting the third bit
)
END

attr1 = "attribute1" ":" VALUE ";"
....

In the WHILE-condition the productions attr1, attr2 and attr3 are
tested as look-ahead productions. No semantic code is executed thereby
and no text is consumed.

The bit-wise operators are just new in the TextTransformer 1.3.3.
interpreter, so I liked to play with them. In elder versions you could
use some booleans instead.

Regards

Detlef
.