[antlr-interest] Henry Butowsky wrote: >> Hi Guys, >> Im using a vary basic C grammer for a scientific application >> I want to be able to use "hyper-slabs" on the left and right hand side >> of an assignment statement >>

Henry Butowsky henryb at ntlworld.com
Tue Nov 29 09:00:46 PST 2005


 >>   var1(expr,expr,expr)   
	>> 
	>>    The relevent grammar is:
	>>
 
	>> assign_statement:
	>>         hyper_slb  ASSIGN^ expr SEMI!
	>> 
     ;
	>> 
	>> 
	>> hyper_slb: VAR_ID (arg_list)?
	>>      ;
	>> 

 >> arg_list
	>>   : LPAREN! expr ( COMMA! expr )* RPAREN!
	>>    { #arg_list
 = #( [ARG_LIST, "arg_list"], #arg_list ); }
	>>   ;
	>>           
	>>
      
	>> so for say  temperaure(2,3,10*2)=100;
	>> I want to get the tree
 to be something like
	>> 
	>> ( = temperature ( arg_list (20),(3), (*(10,2)
 ) )  100)
	>> 
	>> At the momnet Im only getting only the first argument
 in the tree.
	>> Any Ideas ?
	
	
	Yeah, what's your tree grammar?
	

 Also, it would be good to print out the the tree you actually are
	producing
 using toStringList (or is it toStringTree)?
	
	Hyperslabs
Content-Type: multipart/mixed;
 boundary="------------080905080509040709030407"

This is a multi-part message in MIME format.
--------------080905080509040709030407
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Henry Butowsky wrote:

 >> Hi Guys,
 >>   Im using a vary basic C grammer for a scientific application
 >>   I want to be able to use "hyper-slabs" on the left and right hand side
 >>   of an assignment statement
 >>
 >>   var1(expr,expr,expr)
 >>
 >>    The relevent grammar is:
 >>
 >> assign_statement:
 >>         hyper_slb  ASSIGN^ expr SEMI!
 >>      ;
 >>
 >>
 >> hyper_slb: VAR_ID (arg_list)?
 >>      ;
 >>
 >> arg_list
 >>   : LPAREN! expr ( COMMA! expr )* RPAREN!
 >>    { #arg_list = #( [ARG_LIST, "arg_list"], #arg_list ); }
 >>   ;
 >>
 >>
 >> so for say  temperaure(2,3,10*2)=100;
 >> I want to get the tree to be something like
 >>
 >> ( = temperature ( arg_list (20),(3), (*(10,2) ) )  100)
 >>
 >> At the momnet Im only getting only the first argument in the tree.
 >> Any Ideas ?


Yeah, what's your tree grammar?

Also, it would be good to print out the the tree you actually are
producing using toStringList (or is it toStringTree)?

Monty
----------------------------------------------------------------------

Hi Monty

With the following script

a=10.12-20-30-40;
b=-20*-100.987;
c=-100e2/20;
d=10S+20b;
e=1.25f+1e2f+1f;
f=1.25d+1e2+1d;
g=.12;
h=.2e4f+.2e-1f+.2d;
i=2;
j(10)=1-19;

toStringTree() produces

( = a ( - ( - ( - 10.12 20 ) 30 ) 40 ) )
  ( = b ( * ( - 20 ) ( - 100.987 ) ) )
  ( = c ( / ( - 100e2 ) 20 ) )
  ( = d ( + 10S 20b ) )
  ( = e ( + ( + 1.25f 1e2f ) 1f ) )
  ( = f ( + ( + 1.25d 1e2 ) 1d ) )
  ( = g .12 )
  ( = h ( + ( + .2e4f .2e-1f ) .2d ) )
  ( = i 2 )
  ( = j ( arg_list 10 ) ( - 1 19 ) )

---------------------
Parser barfs when j has more than one argument

Attached is my tree grammer

Regards Henry





--------------080905080509040709030407
Content-Type: text/plain;
 name="tree.g"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="tree.g"


class CalcTreeWalker extends TreeParser;



assign 
{
  double r;
}
   :#(ASSIGN  v:VAR_ID  r=out ) {
       Symbol *sp;
       const char* var;                  
       var =v->getText().c_str();

 	   if( (sp=lookup(var)) ) {
		 sp->type=1;
		 sp->u.val=r;
	   }else{
		 sp=install(var,1,r);
	   }
            
      }
   ;

out returns [double r]
{
	double a,b;
	r=0;
}  
    // arithmetic operators 

    :  (#(PLUS out out)) =>  #( PLUS  a=out b=out)	{r = a+b;}
	|  (#(MINUS out out)) => #( MINUS a=out b=out)	{r = a-b;}
	|	#(TIMES a=out b=out)	{r = a*b;}
	|	#(DIVIDE a=out b=out)	{r = a/b;}
	|	#(MOD a=out b=out)	    {r = fmod(a, fabs(b) );}
    |   #(CARET a=out b=out)    {r= pow(a,b); }

    //unary Operators
    |   #(LNOT a=out )        { r=!a; }
	|   #(MINUS a=out )       {r=-a;}
    |   #(PLUS a=out )        {r=+a;}
    
    // math functions 
   |  #(m:FUNC a=out)         { Symbol *sp;
                                const char *var;
                                var =m->getText().c_str();
                                if( (sp=lookup(var)) ) {
                                    r=(sp->u.ptr)(a);
                                }else{
                cout << "Function  " << m->getText() << " not found" << endl;
                exit(1);
                           }
                               }
                                

    // Logical Operators
    | #(LAND a=out b=out)   {r = (a && b); }
    | #(LOR  a=out b=out)   { r = (a || b);   }

    // Comparison Operators
    | #(LTHAN  a=out b=out) { r = (a < b);   }
    | #(GTHAN  a=out b=out) { r = (a >  b);   }
    | #(GEQ  a=out b=out)   { r = (a >= b);   }
    | #(LEQ  a=out b=out)   { r = (a <= b);   }
    | #(EQ  a=out b=out)    { r = (a == b);   }
    | #(NEQ  a=out b=out)   { r = (a != b);   }


    // Naked numbers
	|	c:BYTE			{ 
                         r = atof(c->getText().c_str());
                         //cout << "lexing byte=" << r<< endl;           
                        }
	|	s:SHORT			{r = atof(s->getText().c_str());
                         //cout << "lexing short=" << r<< endl;           
                        }
	|	i:INT			{r = atof(i->getText().c_str());
                         //cout << "lexing int=" << r<< endl;           
                        }
    |   f:FLOAT        { r=strtod(f->getText().c_str(),(char**)NULL); 
                        //cout << "lexing float=" << r<< endl;           
                        }
    |   d:DOUBLE        { r=strtod(d->getText().c_str(),(char**)NULL);
                        //cout << "lexing double=" << r<< endl;           
                        }

    // variables & attributes
	|   v:VAR_ID       { Symbol *sp;
                         const char *var;
                         var =v->getText().c_str();
                           if( (sp=lookup(var)) ) {
                             r=sp->u.val;
                           }else{
                cout << "var " << v->getText() << " not found" << endl;
                exit(1);
                           }
                       }

    |   t:ATT_ID       { Symbol *sp;
                         const char *var;
                         var =t->getText().c_str();
                           if( (sp=lookup(var)) ) {
                             r=sp->u.val;
                           }else{
                cout << "Attribute " << v->getText() << " not found" << endl;
                exit(1);
                           }
                       }
    ;








--------------080905080509040709030407--



More information about the antlr-interest mailing list