Macro

26 views
Skip to first unread message

Joel Jauregui

unread,
Oct 28, 2012, 6:59:57 AM10/28/12
to treet...@googlegroups.com
I am trying to figure out a way in which I can make a given rule expand into code that could be parsed by other rules, so essentially a simple macro. Something along the lines of the C preprocessor where it is just doing a simple lexical replacement which is then parsed by the language grammar. Is there an easy way to do this, or should I use a different tool besides treetop to do the preprocessing?

"Martin J. Dürst"

unread,
Oct 28, 2012, 9:28:40 PM10/28/12
to treet...@googlegroups.com, Joel Jauregui
First, macros such as in C are considered a bad idea these days. You may
start out with a simple use case, and for that, they are quite simple
and straightforward, but then you add more use cases, and it gets more
and more complex. As far as I know, newer languages all avoid macros.
Most of what is done with macros in C can be done in the language in
C++, for example.

Second, if you really decide to have some macro language, then this is a
separate step of (pre)processing. You may be able to use treetop for
both steps, or may want to use it only for one or the other step. For
very simple macros, you may be able to get away with a script based on
pure ruby and regular expressions, but that won't last long. Using a
different parsing formalism for preprocessing and for the actual
processing may help in some cases, but usually, it's easier for you as
the programmer to use the same formalism twice.

Regards, Martin.

Joel Jauregui

unread,
Oct 28, 2012, 9:38:04 PM10/28/12
to treet...@googlegroups.com, Joel Jauregui
Yeah I ended up just using a ruby regex to do the preprocessing. This is for a simple assembler so its not really adding full blown C macros, its just to generate some synthetic instructions. Thanks for the advice anyway!
Reply all
Reply to author
Forward
0 new messages