help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parse and validate a comma separated list of strings


From: Hans Aberg
Subject: Re: Parse and validate a comma separated list of strings
Date: Fri, 9 Sep 2011 16:20:54 +0200

On 8 Sep 2011, at 05:04, Matt Friedman wrote:

> My goal is to parse a comma separated list and validate the values
> that I find therein. I don't have a problem parsing the list. This is
> fairly straightforward.
> 
> What I would like to do in addition to parsing the list is to validate
> that each value I find in that list is one of a set of strings. That
> is, I want to validate that each item in the list is correct.
> 
> Each token in the list can be a one or two character uppercase string.
> i.e. [A-Z]{1,2}
> 
> I'd like the parser to raise an error if the parsed token isn't one of
> the ones in the predetermined list. Let's say the predetermined valid
> list is: AA,BB,C,DE
> 
> If BD is entered this should raise an error.
> 
> Is this something that would be done in bison? Or would it be better
> to handle this post-parse? Or in another context?

Here is a somewhat more advanced illustrative case:

Suppose you have language with identifiers stacked in environments, which are 
stored in a lookup table. The lexer, say generated by Flex, has an identifier 
rule, checks in the lookup table and returns to the Bison parser a token 
depending on whether it is defined, and if it is, its type. The Bison parser 
will be responsible to moving the environment stack.

But if the list produced is validated by complicated contextual dependencies, 
one might first construct the list in the parser, and resolve it in the actions.

So it is possible to push it to different places, depending on what is most 
convenient.

Hans





reply via email to

[Prev in Thread] Current Thread [Next in Thread]