Re: YACC parser & conditional compilation directives

ccrlphr@xensei.com (Steffen Ullrich)
Tue, 27 Jun 1995 15:07:04 GMT

          From comp.compilers

Related articles
YACC parser & conditional compilation directives spx4vc@cf.ac.uk (Vadim) (1995-06-23)
Re: YACC parser & conditional compilation directives johnm@soda.CSUA.Berkeley.EDU (1995-06-27)
Re: YACC parser & conditional compilation directives ccrlphr@xensei.com (1995-06-27)
Re: YACC parser & conditional compilation directives scott@infoadv.mn.org (Scott Nicol) (1995-07-05)
| List of all articles for this month |

Newsgroups: comp.compilers,comp.unix.programmer
From: ccrlphr@xensei.com (Steffen Ullrich)
Keywords: yacc
Organization: FROGSTAR B (Sec Pl ZZ alpha)
References: 95-06-028
Date: Tue, 27 Jun 1995 15:07:04 GMT
Status: RO

One way is to do something like this:


    * %token IF ELSE ENDIF ...
    * %type <int> condition
    * %type <compiled> cond_compile compile_this statement ...
    * ...
    * %%
    * ...
    * cond_compile: compile_this
    * | IF condition cond_compile ELSE cond_compile ENDIF { if ($2) $$=$3 else $$=$5; }
    * ;
    * condition: ... { $$=evaluate($1,$....) /* evaluate the condition */ }
    * compile_this: statement
    * | compile_this statement { $$=concat($1,$2); /* concat the compiled statements */}
    * ;
    * statement: command param1 param2 ... { $$=compile($1,...); /* compile and put result in $$ */}
    * | kind2_stat ...
    * ;


Another way (and I think the easier way once you understand the idea)
is to use a OO-idea and let yacc build the syntax tree, then excecute
several methods on this tree. I used this in my thesis to write a
pascal-to-C and a pascal-interpreter and it's a wast of memory but
pretty fast (if the system dont need to swap continously) (the
notaion is similar to but simple than yacc)


    * file: cond_compile(c) { (c->compile())->print(); }
    * cond_compile: compile_this
    * | IF condition(c) cond_compile(cc1) ELSE cond_compile(cc2) ENDIF {
    * return c->evaluate() ? cc1->compile() : cc2->compile();
    * }
    * .
    * condition: ....
    * compile_this: statement
    * | compile_this(ct) statement(s) { return new Compiled(ct->compile(),s->compile()); }
    * .
    * ...


Vadim <spx4vc@cf.ac.uk> wrote:
    >I'm writing a kind of assembler translator, the grammar is perfectly
    >suitable for Yacc, but my problem is the conditional compilation
    >directives.


--
                                                                                Steffen Ullrich
                                                                          ccrlphr@xensei.com
                                        http://www.xensei.com/users/ccrlphr/
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.