Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-08-31 14:04:17


"Joel de Guzman" <djowel_at_[hidden]> writes:

> Hi,
>
> Here's another use-case...
>
> We are working on an alternative semantic action scheme with Spirit
> that will be a lot more easier to use than the current scheme. With this
> scheme, the signature of the semantic action will be dependent on the
> rule where it is attached to. This is similar in some sense to YACC.
> For instance (I'll write in plain EBNF):
>
> r ::= a b c
>
> The attached semantic-action to this rule will have a signature:
>
> F(tuple<A, B, C>)
>
> Another example:
>
> r ::= a | b | c
>
> The attached semantic-action to this rule will have a signature:
>
> F(variant<A, B, C>)
>
> Now, unlike YACC, Spirit has iteration (kleene, plus, optional)
> Here's a more or less the complete suite of patterns and the
> corresponding semantic-action signatures:
>
> r ::= a b -----> F(tuple<A, B>)
> r ::= a | b -----> F(variant<A, B>)
> r ::= a* -----> F(vector<A>)
> r ::= a+ -----> F(vector<A>)
> r ::= a? -----> F(optional<A>)

This is somewhat OT, but I want to suggest you consider something
else. I did this in a parser generator system I wrote, and it worked
out really well.

If the rule is

   x -> a b | c d c

Then the way you refer to the semantic value associated with the a
symbol is by writing, simply, 'a'. The way you refer to the semantic
value of the first c symbol is by writing c[0], and you refer to the
2nd one as c[1]. I'm sure you get the idea.

Then the user almost never needs to worry about positional
associations between grammar symbols and their semantic values. It's
actually fairly rare that the same symbol appears twice on the rhs of
a rule (especially when rules are reduced to eliminate "|") and
keeping track of whether it's the first or nth instance of 'X' is much
easier than keeping track of precisely where the Xs fall in the
sequence of symbols.

My system was written in Python, but you might be able to adapt the
general idea to C++.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk