Re: spirit grammar graphs

Larry Evans <[email protected]> Wed, 30 Mar 2011 19:20:09 -0500
Newsgroups gmane.comp.parsers.spirit.devel
Message-ID <[email protected]>
On 03/30/11 17:26, Gordon Woodhull wrote:
> Hi Spirit Developers,
>
> I am working on the MPL.Graph paper & talk for BoostCon.  I want to
> say a little bit about Spirit grammar rules, because I think it's a
> good example of a compile-time graph that many people are familiar
> with.

>
> So I'd like to summarize how type erasure and object references are
> used in Spirit to produce a graph when expressions only allow us
> trees.

>
> I will write/say something like this: (needs editing but is this
> basically correct?)

>
> Spirit generates a compile-time graph of function objects calling
> each other by tying together nodes of expression trees by common
> rule references.  The rules use type erasure to make it easy to
> construct the objects without having to specify insanely complicated
> types; type erasure is used as a substitute for C++0x "auto".  The
> technique also allows rule objects to remain uninitialized so they
> can be referred to before they are defined.

>
> Next, I was going to ask a simple question about type erasure, but I
> found the answer in the Spirit Rationale.  So I'll ask a more
> complex question.  :-)

>
> Since type erasure blocks inlining, isn't there some kind of type
> unerasure that can bring it back?  Or would that require templated
> virtual functions?

>
> Obviously I have an ulterior motive: if it is possible, then I want
> to be able to build compile-time graphs using the same nice syntax
> Spirit uses.  If it's not possible, then I want to see if Spirit
> could use compile-time graphs to solve the same problem with greater
> efficiency (and perhaps better clarity).

>
> Thanks,
> Gordon
>

Hi Gordon,

In the vault:

http://www.boostpro.com/vault/index.php?&directory=Strings%20-%20Text%20Processing

there's:

  subrule_simple.map_parse.1.50.zip

which contains:


http://www.boostpro.com/vault/index.php?&directory=Strings%20-%20Text%20Processing

which contains:

        mout()<<"test:3 subrules with sr0 terminal\n";
        spirit_simple::subrule<0> sr0;
        spirit_simple::subrule<1> sr1;
        spirit_simple::subrule<2> sr2;
        spirit_simple::terminal term0;
      #ifndef SubruleMapBuildMethodTemplate
            typedef
          map
          < typeof(sr0=sr1>>term0)
          , typeof(sr1=sr2)
          , typeof(sr2=sr0)
          >
        lhs2rhs_map0
        ;

where map is the mpl::map; hence, is generated at compile time.
The, sr0....sr2, are non-terminals, and term0 is a terminal in the
grammar defined by productions:

  sr0=sr1>>term0
  sr1=sr2
  sr2=sr0

Note, there's only 1 binary operator, >>, but it should be no problem
to define others similarly.  Also note that the grammar productions do
contain a cycle:

  sr0->sr1->sr2->sr0

Hope the code gives you some ideas :)

Good luck.

-regards,
Larry



------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself; 
WebMatrix provides all the features you need to develop and 
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf