Luke Palmer wrote:
> 
> > grammar Grammars::Languages::C::Preprocessor {
> >   rule CompilationUnit {
> >     ( <Directive> | <UnprocessedStuff> )*
> >   }
> >
> >   rule Directive {
> >     <Hash> ( Include
> >            | Line
> >            | Conditional
> >            | Define
> >     ) <Continuation>*
> >   }
> >
> >   rule Hash { /^\s*#\s*/ }
> >   rule Include {...}
> >   rule Line {...}
> >   rule Conditional {...}
> >   rule Define {...}
> >   rule Continuation {...}
> >   rule UnprocessedStuff {...}
> > }
> 
> We're not quite in the world of ACME::DWIM, so you can't just replace
> the important stuff with ... .  :-)
> 
> You're not outputting a parse tree, you're just outputting more text
> to be parsed with another, text-based, grammar.  It seems to me like
> it's a big s//ubstitution, of sorts.

Hmm... well, think about yacc for a moment.  You could either have the
handlers for rules assign to $$, based on $1, etc., and thus build a
huge structure, OR, you could have the handlers for rules print stuff to
stdout (which might possibly be a pipe to another process).

ISTM that we also want our grammers to be able to do things like that...

We want to be able to produce a tree, *and*, we want to be able to act
as a filter.  And probably also some combination thereof -- some rules
produce trees, and other rules within the same grammer (which contain
the tree-producing ones) somehow process and output those trees.

> Or maybe not, maybe we make an output stream which will be fed to
> Grammars::Languages::C.  Here's an implementation a #include
> processor, using as little made-up syntax as possible.
> 
>     use Pattern::Common;
> 
>     grammar Preprocessor {
>         rule include {
>             :w(/\h*/)
>             \# include "<Pattern::Common::filename>" $$
>                 { $0 := (<<< open "< $filename") ~~ /<main>/ }
>         }
> 
>         rule main {
>             $0 := ( [<include> | .]* )
>         }
>     }

Alas, this doesn't work right with a fairly common idiom:

   #ifdef HAVE_FOO_H
   #include <foo.h>
   #endif

Nor the even more common:

   #ifndef SYS_SOMEFILE_H
   #define SYS_SOMEFILE_H
   lots of stuff, possibly including some #includes, and some typedefs.
   #endif /* SYS_SOMEFILE_H */

-- 
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "[EMAIL PROTECTED]
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}

Reply via email to