3 ms·
Fun fact: A list with leading markers is simpler to parse than a comma separated one. If the marker is "-", EBNF might look like this: leading-marker-list =
by any1 4y ago
Fun fact: A list with leading markers is simpler to parse than a comma separated one.
If the marker is "-", EBNF might look like this:
leading-marker-list = *("-", element)
Comma separated would look like this:
comma-separated = [ element, *(",", element) ]
This is not meant as an argument for or against; just an observation.
Edit: Amended comma separated EBNF to account for an empty list.
- gliptic 4y agoIn actual code it's not much difference, e.g.: while (test("-")) { element() } vs. do { element(); } while (test("-"))
- any1 4y agoIt depends on your system of parsing. For example, code generated by a parser generator would be slightly more complex. A regular expression would also be slightly more complex. I have written recursive descent parsers in the past, and if I remember correctly, the way I set things up, I had to handle leading list elements specially. It's possible that this was unnecessary and I was simply blinded to a simpler way of doing things because I was following the EBNF too strictly. Edit: I noticed that we're not actually accounting for an empty list here. That makes things slightly more complex: comma-separated = [ element, *(",", element) ]; Your code would need to test if there is an element or a list terminator before processing the element.
- layer8 4y agoWhether the marker is leading or trailing is orthogonal to whether the first/last marker is optional. And usually you want to have it optional in either case so that e.g. you can write `f(arg)` and don’t have to write `f(,arg)` or `f(arg,)`. Hence it would be: leading-marker-list = [ element, *("-", element) ]