File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes can you convert a String into an executable line? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can you convert a String into an executable line?" Watch "can you convert a String into an executable line?" New topic
Author

can you convert a String into an executable line?

Micah Pezdirtz
Greenhorn

Joined: Aug 14, 2006
Posts: 25
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

Joined: Oct 13, 2005
Posts: 38025
    
  22
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Micah Pezdirtz
Greenhorn

Joined: Aug 14, 2006
Posts: 25
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can you convert a String into an executable line?
 
Similar Threads
How to Make JAVA IO methods recognize new line? "\n"
Kind of like FlowLayout, but with new lines?
Threads Mock
string to be replaced in a file
Connecting to a MySQL DB