Mackie Messer
Mackie Messer

Reputation: 1306

Is there a standard Backus-Naur form for checking that lists contain consistent token types?

I'm using PLY to parse DNET files. Part of the grammar suggested by the DNET specification is (in my words):

<value>  -> NUMBER | STRING | ID
<values> -> <value> | <values> COMMA <value>
<list>   -> LPAREN <values> RPAREN | LPAREN RPAREN

This works fine if I don't care about mixing token types within a list. However, if I want to be sure that the parser only produces valid lists, it would be great if I could guarantee consistent token types within the list.

My intuition was to define a list production for each valid union of token types, and then use that list production where needed. For example, if I wanted to guarantee I have a list of STRING tokens:

<strings>     -> STRING | <strings> COMMA STRING
<string_list> -> LPAREN <strings> RPAREN | LPAREN RPAREN

But this means I would have to define a bajillion different list productions. Is there a smarter way to do this at the parser level, or should I save this sort of validation to be executed on the AST that I build?

Upvotes: 0

Views: 64

Answers (0)

Related Questions