Grammar expression for representing comma-separated lists

Based on my experience, formal grammars usually express comma-separated lists in a form like this:

foo_list -> foo ("," foo)* 

What alternatives exist to not mention foo twice? Although this far-fetched example may seem innocent, I come across non-trivial expressions instead of foo . For instance:

 foo_list -> ( ( bar | baz | cat ) ) ( "," ( bar | baz | cat ) )* 
+4
source share
2 answers

I remember the (patented) parser I once worked with, and it would be written as

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

Yes, exactly. The actual metacharacters above are controversial, but I find the general approach acceptable.

When writing another syntax generator, I considered something like this for a while, but threw it in favor of saving the model.

The syntax diagram, of course, can perfectly represent it without unwanted repetition:

foo_list

+1
source

During my experiments, this syntax showed some potential:

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

The current ... refers to the previous expression (in this case ( bar | baz | cat ) ).

This is not an ideal solution, but I am posting it for discussion.

0
source

Source: https://habr.com/ru/post/1340003/


All Articles