Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to setup flex/bison rules for parsing a comma-delimited argument list

Tags:

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
                 {
                     ...
                 }
          ;