views:

108

answers:

1

Hello all,

I'm writing a simple scripting language on top of Java/JVM, where you can also embed Java code using the {} brackets. The problem is, how do I parse this in the grammar? I have two options:

  1. Allow everything to be in it, such as: [a-z|a-Z|0-9|_|$], and go on
  2. Get an extra java grammar and use that grammar to parse that small code (is it actually possible and efficient?)

Since option 2] is basically a double-check since when evaluating java code it's also being checked. Now my last question is -- is way that can dynamically execute java code also with objects which have been created at runtime?

Thanks,

William van Doorn

A: 

1] Allow everything to be in it, such as: [a-z|a-Z|0-9|_|$], and go on

You can't just do that: you'll have to account for opening and closing brackets.

2] Get an extra java grammar and use that grammar to parse that small code (is it actually possible and efficient?)

Yes that's possible. But I suggest you first get something working, and then worry about efficiency (is that really an issue here?).

... is way that can dynamically execute java code also with objects which have been created at runtime?

Yes, since Java 6, there's a way to compile source files dynamically. See the JavaCompiler API.

Bart Kiers
Thanks, I'll mark this as answer, but, it looks like the JavaCompiler API only accepts file instances instead of plain lines?
wvd
@wvd: yes, you'll have to write your Java source (as String(s)) to a file first.
Bart Kiers