YACC parser & conditional compilation directives

YACC parser & conditional compilation directives

Post by Vadi » Sat, 24 Jun 1995 04:00:00



I'm writing a kind of assembler translator, the grammar is perfectly
suitable for Yacc, but my problem is the conditional compilation
directives. For example, in the following source first and third
sections must be bypassed:

DEFINE a,2

IF a==1
   ... Do one thing ...
ELSE
  .IF a==2
    ... Do another thing
  .ELSE
    ... Do yet another thing
  .ENDIF
ENDIF

etc.

(All directives start with dot)

Of course, I could use some kind of hack in lexer, that would recognize
IFs, .ELSEs, .ENDIFs etc. and act accordindly, but I want to know
if there is a _nice_ way of skipping the conditioned out source ?
Besides, the conditional expression may be quite complex and I would have
problems with its parsing, had I used the hack.

Vadim
[Yacc doesn't handle this kind of two-level grammar at all well.  You need
either a two-pass system or some gross lexical hacks. -John]
--


 
 
 

YACC parser & conditional compilation directives

Post by John D. Mitche » Wed, 28 Jun 1995 04:00:00


[...Conditional compilation code example, etc....]

Quote:>Of course, I could use some kind of hack in lexer, that would recognize
>IFs, .ELSEs, .ENDIFs etc. and act accordindly, but I want to know
>if there is a _nice_ way of skipping the conditioned out source ?
>Besides, the conditional expression may be quite complex and I would have
>problems with its parsing, had I used the hack.

>Vadim
>[Yacc doesn't handle this kind of two-level grammar at all well.  You need
>either a two-pass system or some gross lexical hacks. -John]

Right, YACC is unsuitable for this sort of thing.  This is, however,
a perfect example where the 'semantic predicates' capability of the
ANTLR parser generator that is part of the PCCTS suit.

You want to be able to have semantic information that you have gathered
during parsing to affect how you continute to parse the input.  In your
example you don't need a two-pass system or any of that malarky as
your conditional directive is already decidable.

Anyways, to find out more about PCCTS (snarf the documentation, code,
examples, etc.) check out ftp://ftp.parr-research.com/pub/pccts
and/or the comp.compilers.tools.pccts newsgroup.

Hope this helps,
                John
--



 
 
 

YACC parser & conditional compilation directives

Post by Steffen Ullri » Wed, 28 Jun 1995 04:00:00


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()); }
  *     .
  * ...

  >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

                    http://www.xensei.com/users/ccrlphr/
--