Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can you convert a String into an executable line?

 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to execute a String as a line of code? for example, could have:

String command = "x+=2";

and then have somehow run that string as though it were a line of code? im trying to make an action listener add "Requests" to an arraylist that will be executed in an orderly way at a time perhaps more appropriate than when the event occurs, and i dont know any other way to "put off" the execution of some code while keeping the rest of the program running. if you'd like, i can post the full code if my explanation didnt make anysense
thanks for any help, guys
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to scan it with a scanner, eg lex, flex, JFlex, then pass the resultant tokens to a parser eg yacc, bison, byacc, CUP, and set up TACs (triple address codes), or put executable code into the parser.

Get yourself a book about compiler design, because that is what you are doing. Yes, it is possible; that is what happens when an application is compiled, but it is well beyond the beginner's level. You will find computer sciences courses include modules about "languages" or "compilers;" that si the sort of thing they cover.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just need to evaluate math expressions like your example, it's very well covered territory. Topics like "infix to postfix" parsing or "expression evaluator" should find you lots of hits.

To get much more of a language interpreter going is, as suggested above, a pretty big job. Fortunately others have paved the way. Google for scripting support in Java 6.
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i see this is a much bigger job than i thought, i'll just have to think of another way to get around this, if i even decide to keep this approach
thanks for your help again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic