简体   繁体   中英

JAVACC && JJTree (.jjt) Inserting || symbol into tree when parsing two tokens

For eg AB && C Tree should be like this (A || B) && (C). this should be like this. I tried out but every time extra || is added like this A || B || && C. this generates the wrong tree every time.

Code i do it like this

LOOKAHEAD((simpleTerm())+)

   (
       simpleTerm()
  {
     jjtthis.setValue("||");
   }
   )+

* Note simpleTerm could be &ltSTRING&gt or &ltQuotedString&gt *

How can i insert || symbol into tree so it form tree like this A || B && C.?

How can i insert token image into tree using javacode ?

Try something like the following

void Conjunction() #void :
{ }
{
  Disjunction()
  (  "&&"
     Disjunction()  
     {jjtThis.setValue( "&&" );} #BinOp(2)
  )*
}

void Disjunction() #void :
{}
{
  SimpleTerm()
  (  
     SimpleTerm()  
     {jjtThis.setValue( "||" );} #BinOp(2)
  )*
}

void SimpleTerm() :
{Token t ;}
{
    (t=<STRING> | t=<QUOTEDSTRING>)
    {jjtThis.setValue( t.image ) ;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM