One approach would be to have two different scanner states. To keep this
simple two conditions would have to be met:
1. A preprocessing statement can be recognized by it's first token.
I.e., the first token in any preprocessing statement doesn't appear in any
C code.
2. The end of each preprocessing statement can be determined without
having to look ahead at the next token (which would be the first C token to
copy after the preprocessing statement).
If these conditions can be met for your preprocessing statements, then the
two scanner states would be:
- A "copy" state that only recognizes and returns the first tokens for
each of the preprocessing statements and copies everything else.
- A "full scan" state that recognizes and returns all tokens.
The parser could then switch the scanner back and forth between these two
states. It would be written to recognize a series of preprocessing
statements and nothing else. The scanner would start in the "copy" state,
and all of the C code would not be seen by the parser. When the scanner
sees one of the first tokens, it returns it to the parser. Upon seeing the
first token for any preprocessing statement, the parser immediately
switches the scanner to the "full scan" state and parses the rest of that
preprocessing statement. When it encounters the end of the statement, it
switches the scanner back to the "copy" state.
Hope this helps.
BTW, are you making this code available? I've been thinking that it would
nice to have a general macro processing preprocessor that would be easier
to use than m4 <http://www.gnu.org/software/m4/>. Wondering if what you're
doing might be a start for such a thing?
-Bruce
On Sun, Jun 16, 2013 at 2:45 PM, [email protected] <
[email protected]> wrote:
> I've just completed a *PLY* project to do some preprocessing on a 'C'
> source file for a PIC microcontroller (specifically to handle my version of
> the #rom directive converting it to the compilers version). This is working
> just as I want but I have only defined the grammar for the specific #rom
> statements I wish to process.
>
> It would be convenient, in the same file, to have various other 'C'
> statements (free from text), #define or typedefs for example. Can someone
> suggest a way to do this without having to put the complete or subsections
> of the 'C' grammar into my preprocessor. That is to say I want the
> preprocessor to copy all non #rom statements (ie lines of other 'C' code)
> 'as is' to the output file. It is only the #rom statements I need to
> process on the way to the output file.
>
> Needless to say the 'free form text' and the #rom statements overlap in
> terms of some tokens. The free from text may include 'C' expressions as may
> a #rom statement, also literals such as { } ( ) @ etc.
>
> Any ideas would be appreciated.
>
> Regards,
>
> John
>
> --
> You received this message because you are subscribed to the Google Groups
> "ply-hack" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ply-hack/d8801853-3b90-41b2-a929-6ea2f97ece19%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"ply-hack" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ply-hack/CAEs%3D1RgKhxyTmcTY0SFwV_ziNUD%2BhXYcaDWWU39uGVvDhX%3DTcg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.