In a RecDescent grammar, I'd like to add support in my language for "macros".
These macros would be very much like #define macros in C and would support arguments that are instantiated into the expansion. In C, this looks something like:
#define add(a,b) (a+b)
In my language, I'd like something like:
myMacro(x,y): any_expression
where "any_expression" is any old (deep) expression defined elsewhere in the grammar, and (x,y) would be replaced/instantiated in that expression.

This may seem straighforward, but, well, it ain't to me. (I am, after all, mush4brains.)
In my P::RD grammar, the "macro" rule without arguments would look like:
macro: identifier ':' expr
where, again, expr is defined fully in the grammar. Adding arguments to this... I need to instantiate my (x,y) into the "expr" string before the subrules interpret it.

Now... I thought of using standard Perl substitution in a pre-scan, but this seems less-than-ideal, since it requires some knowledge of the grammar in the Perl REs.

I thought of using two separate grammars, the first of which is a simplified version of the full second grammar, but this also seems a bit redundant.

Then I thought... "This hurts".
And then... "Hey, I'll ask the Monks!!!"

So... what am I missing here? Something obvious, I imagine?

Thanks for any help.
Jim Wiltshire

In reply to Macros in a RecDescent grammar by mush4brains

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.