Re: Grammar for optional elements



Mohitz <coolmohitz@xxxxxxxxx> wrote:

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...

You can do this with Parsing Expression Grammars.
http://pdos.csail.mit.edu/~baford/packrat/

start = attr*
attr = attr1 / attr2 / attr3
attr1 = "attribute1" COLON value SEMICOLON !(attr* attr1 attr*)
attr2 = "attribute2" COLON value SEMICOLON !(attr* attr2 attr*)
attr3 = "attribute3" COLON value SEMICOLON !(attr* attr3 attr*)

This says that each attribute expression is not followed by a sequence
of attributes containing another of the same kind of attribute expression.

Tony.
--
f.a.n.finch <dot@xxxxxxxx> http://dotat.at/
.