[stringtemplate-interest] some templates do, some don't

krriggs at comcast.net krriggs at comcast.net
Mon Aug 13 09:49:53 PDT 2007


Probably a newbie error!

Re-wrote an HL proof to java code gnerator using rewrite and tokenStream and added some extensions.
Problem is this: some templates are invoked and others are not.
I assume rewrites are 'in' the token stream and evaluated during 'toString'
Am I overloking something basic?

For the really obliging, the grammar, the (UTF-8) input to the system, the actual output and the required output for a sumple example are below:

vvvvv       GRAMMAR vvvvvvvvvvvv
grammar Jape;
options {
     //output =AST;
     //ASTLabelType=CommonTree;
     output=template;
     rewrite=true;
     }
  @header{package j2j;}  
  @lexer::header{package j2j;} 
     @members {
                   JapeProgramWriter prg = new JapeProgramWriter();
 JapeProgramWriter results(){return prg;}
}
   
proof: lSection (cSection lSection)+ EOF;
lSection  :  '{' logic '}' 
            -> template(lx={$logic.text})
               " if (<lx>) passed(); else failed(); "
               ;
cSection: '(' cc=code ')' | cc=code
            -> template(cx={$cc.text}) " <cx> "
;
// took += out after ex2
logic  : ex1=impLogic (PIFF ex2=impLogic)*
            -> {ex2!=null}?  template(l1={$ex1.text},l2={$ex2.text})
                 " [[<lx1>) == [<lx2>]] && [![<lx1>)== ![<lx2>]] "
           ->  template(l3={$ex1.text})
                                 " <l3> "
;
// took += out after ex2
impLogic :  e1=orLogic (PIMPLIES e2=orLogic)*
            -> {e2!=null}? template(lx1={$e1.text},lx2={$e2.text})
                                 " !(<lx1>)||(<lx2>) "
             ->template(lx1={$e1.text})
                                " <lx1> "
                       ;
orLogic  :  andLogic (POR andLogic)*//->(LOR andLogic+) 
                       ;
andLogic :  bool (PAND bool)* //-> (LAND bool+)
                       ;
bool :  boolTerm 
                         | PNOT boolTerm
                         ;
boolTerm       :  'true' 
                        | 'false'
                        | BOOLVAR
                        | cExpr //-> (cExpr)
                        | universal
                        | existential
                        | '(' logic ')';
 universal  :  'forAll' ID '.' '(' logic ')' 
                     ->template(ix={$ID.text},lx={$logic.text})
                        " forAll(\"<ix>\",\"<lx>\")"
                     ;
 existential
  : 'eXists' ID '.' '(' logic ')'
                     ->template(ix={$ID.text},lx={$logic.text})
                        " eXists(\"<ix>\",\"<lx>\")"
  ;
// add for all afteradded to CharTransliterator
cExpr :  expr relOp expr ;
code: statement  (';' statement)* 
;
statement: assigns | ifs | whiles | skips;
assigns : ID ':=' expr 
                       -> template(id={$ID.text},e={$expr.text})"<id>=<e>;"
               | BOOLVAR ':=' cExpr
               {prg.addBool($BOOLVAR.text);}
                       -> template(id={$BOOLVAR.text},e={$cExpr.text})
                          "<id>=<e>;"
                       
               ;
ifs: PIF l=logic PTHEN e=code PELSE e2=code PFI
     -> template(lx={$logic.text},tx={$e.text},ex={$e2.text})
         "if (<lx>) {<tx>} else {<ex>}"
;
whiles: PWHILE lx=logic PDO cx=code POD
    -> template(ly={$lx.text},cy={$cx.text})
         << while (<ly>) { 
                 <cy> 
                 } >>
;
skips: 'skip' ->template()" ";
expr :   multExpr (addOp multExpr)* ;
addOp : PLUS | MINUS;
multExpr     :  powerExpr (multOp powerExpr)*  ; 
multOp : TIMES | DIVIDE | MOD ;
powerExpr:  value 
 | value (POWER e2=powerExpr )
               -> template(v1={$value.text},v2={$e2.text})
                    "Math.power(<v1>, <v2>)"
;
value: ID         {prg.addVar($ID.text);}   
           | NINT 
           | parameter {prg.addConst($parameter.text);}
           | BOOLVAR {if (!prg.boolNames.contains($BOOLVAR.text))
                                     prg.addConst($BOOLVAR.text);}
           ; 
parameter :  GIVEN ;
//id  :  ID;
anInt  :  NINT;
relOp: 
         LT  
         | GT 
         | EQ -> template()"=="
         | LTE 
         | GTE 
         | NE ;
// for the sake of the AST tree
//LAND :;
//LOR :;
//LIMPLIES :;
//LIFF :;
//MCODE :;
//MLOGIC :;
PIF:'if';
PFI:'fi';
PELSE:'else';
PTHEN:'then';
PWHILE:'while';
PDO:'do';
POD:'od';
PSKIP:'skip';
ASSIGN:':=';
LPAREN:  '(';
RPAREN: ')' ;
LBRKT:'{';
RBRKT:'}';
SEMIC:';';
POWER:'^';
PLUS  : '+' ;
MINUS : '-' ;
TIMES: '*';
DIVIDE:'/';
MOD : '%';

GTE:'>=';
LTE:'<=';
NE: '!=';
EQ: '=';
GT: '>';
LT:'<';
PNOT: '!';
POR: '||';
PAND: '&&';
PIMPLIES : '=>';
PIFF : '<=>';
ID    : ('a'..'z')( 'a'..'z' | '0'..'9')?;
BOOLVAR         : ('a'..'z')('a'..'z')('a'..'z')+;
GIVEN : 'K'( 'a'..'z' | '0'..'9')?;
NINT: /*'-'?*/ ('0'..'9')+;
WS : (' '|'\t'|'\n'|'\r\n')+ {$channel=HIDDEN;};
//JUNK : '\uFFFF' {$channel=HIDDEN;};

vvvvvvvv INPUT vvvvvvvv
{n¡Ã2}(i:=2){2¡Âi¡üi¡Ân¡ü¢£x.(2¡Âx¡üx<i¡æn mod x¡Á0)}while n mod i¡Á0 do i:=i+1 od{2¡Âi¡üi¡Ân¡ü¢£x.(2¡Âx¡üx<i¡æn mod x¡Á0)¡ün mod i=0}
--- an XLATION from UTF-8 is also done
vvvvvvvv ACTUAL OUTPUT (w/o pre-,post- amble) vvvvvvvvv
if ( n>=2 ) passed(); else failed(); 
(i=2;) 
if ( 2<=i&&i<=n&& forAll("x"," !(2<=x&&x<i)||(n%x!=0) ") ) passed(); else failed(); 
while n%i!=0 do i=i+1; od 
if ( 2<=i&&i<=n&& forAll("x"," !(2<=x&&x<i)||(n%x!=0) ")&&n%i==0 ) passed(); else failed(); 

vvvvvvvv DESIRED OUTPUT vvvvvvvvv
if ( n>=2 ) passed(); else failed(); 
i=2;
if ( 2<=i&&i<=n&& forAll("x"," !(2<=x&&x<i)||(n%x!=0) ") ) passed(); else failed(); 
while ( n%i!=0 ) { i=i+1; } 
if ( 2<=i&&i<=n&& forAll("x"," !(2<=x&&x<i)||(n%x!=0) ")&&n%i==0 ) passed(); else failed(); 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20070813/73b4c5d1/attachment.html 


More information about the stringtemplate-interest mailing list