开发者

A grammar expression for representing comma-delimited lists

Based on my experience, formal grammars typically express comma-delimited lists in a form similar to this:

foo_list -> foo ("," foo)*

What alternatives are there to avoid mentioning foo twice? Although this contrived开发者_Go百科 example may seem innocent enough, I am encountering non-trivial expressions instead of foo. For example:

foo_list -> ( ( bar | baz | cat ) ) ( "," ( bar | baz | cat ) )*


I remember a (proprietary) parser generator that I once worked with, which would have this production written as

foo_list ::= <* bar | baz | cat ; "," *>

Yes, exactly like that. The actual metacharacters above are disputable, but I deem the general approach acceptable.

When writing another parser generator, I considered something alike for a while, but dropped it in favor of keeping the model simple.

A syntax diagram of course can nicely represent it without the unwanted repetition:

A grammar expression for representing comma-delimited lists


During my experimentation, this syntax showed some potential:

foo_list -> ( bar | baz | cat ) ("," ...)*

The ... token refers to the preceding expression (in this case, ( bar | baz | cat )).

This is not a perfect solution, but I am putting it out there for discussion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜