I would like to be able to parse a non-empty, one-or-many element, comma-delimited (and optionally parenthesized) list using flex/bison parse rules.
some e.g. of parseable lists :
- 1
- 1,2
- (1,2)
- (3)
- 3,4,5
- (3,4,5,6)
etc.
I am using the following rules to parse the list (final result is parse element ‘top level list’), but they do not seem to give the desired result when parsing (I get a syntax-error when supplying a valid list). Any suggestion on how I might set this up ?
cList : ELEMENT
{
...
}
| cList COMMA ELEMENT
{
...
}
;
topLevelList : LPAREN cList RPAREN
{
...
}
| cList
{
...
}
;
This sounds simple. Tell me if i missed something or if my example doesnt work
However if you accept rvals as well as this list you’ll have a conflict confusing a regular rval with a single item list. In this case you can use the below which will either require the ‘(”)’ around them or require 2 items before it is a list