java - Rewriting parts of a string -


i have string rewrite. string contains substrings "ddt" plus 4 digits. i'll call these blocks. contains connectives "&" , "|", | represents "or", parentheses.

now rewrite string such blocks separated &s should written "min(x(block1), x(block2), etc.)", whereas blocks separated |s should written "max(x(block1), x(block2), etc.)".

looking @ example should help:

public class test{     public static void main(string[] arg) throws exception {   string str = "(ddt1453 & ddt1454) | (ddt3524 & ddt3523 & ddt3522 & ddt3520)";  system.out.println(str.replaceall("ddt\\d+","x($0)"));   }  } 

my desired output is:

max(min(x(ddt1453),x(ddt1454)),min(x(ddt3524),x(ddt3523),x(ddt3522),x(ddt3520))) 

as can see, performed initial substitution include x(block) part of output, cannot rest. ideas on how achieve desired output?

just doing string substitution wrong way go this. use recursive-descent parsing instead

first want define symbols create example:

program -> literalarg|fn(x)|program

literalarg -> literalarg

literalarg&literalarg -> fn(literalarg) & fn'(literalarg)

fn(x) -> fn(x)

fn(x) |fn(y) -> fn(x),fn(y)

from there make functions recursively parse data expecting things happen. example

 string finalresult = "";  function parse(basestring) {      if(basestring.isliteralarg)      {          if(peekaheadtocheckforampersand())          {               expectanotherliteralargafterampersandotherwisethrowerror();               finalresult += fn(literalarg) & fn'(literalarg)               parse(basestring - recenttoken);          }          else          {              finalresult += literalarg;               parse(basestring - recenttoken);          }      }      else if(basestring.isfunction()       {            if(peekaheadtocheckforpipe())            {               expectanotherfunctionafterampersandotherwisethrowerror();                 finalresult += fn(x),fn(y)                  parse(basestring - recenttoken);            }            else            {                finalresult += fn(x)                parse(basestring - recenttoken);            }       }  } 

as find tokens, take them off string , call parse function on remaining string.

rough example i'm basing off project did years ago. here relevant lecture: http://faculty.ycp.edu/~dhovemey/fall2009/cs340/lecture/lecture7.html


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -