Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

Syntax checker

Thread view: 
dhanya - 22 Dec 2006 10:07 GMT
Hi folks,

   Have an interesting problem. Hope someone can help me out. I have a
GUI where the end user enters some formula, mostly price calculations.
Also they can enter conditions like
IF(a>b) THEN a=10 ELSE a=20. I have to check whether the syntax is
correct and save this. Later in the application i have to get these
formulas and convery into java if else stmts. Please give me your views
about how to deal with this.

Thanks in advance,
Dhanya
Steve W. Jackson - 22 Dec 2006 15:31 GMT
> Hi folks,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> Dhanya

Depending on whether this is a classroom assignment or not, you might
look into JavaCC.

JavaCC allows you to define a syntax for a language.  In our usage of
it, we have an application which allows users to include what we call
"expression language" statements.  JavaCC was used to define the syntax
and generate base code for all the various statement types.  We then had
to fill in the internals of many of the resulting classes, and add some
code to others.  But it's not for the faint of heart...

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Oliver Wong - 22 Dec 2006 15:54 GMT
>> Hi folks,
>>
[quoted text clipped - 15 lines]
> to fill in the internals of many of the resulting classes, and add some
> code to others.  But it's not for the faint of heart...

   More generally, you're going to have to write a parser. You can do it by
hand, or use a parser generator like JavaCC or ANTLR. You might want to read
up on compiler theory, and maybe ask for advice in the comp.compilers
newsgroup.

   - Oliver
Hemal  Pandya - 22 Dec 2006 18:14 GMT
> Hi folks,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> Dhanya

Other posters have already described one approach, of writing  a
parser. If you take this approach, my advice is to keep the language
simple. Also look at ANTLR besides JavaCC, I have used ANTLR in past
for very similar functionality (allowing users to extend) and was quite
happy with it.

A couple other approaches that may be suitable:

1. Define a Java interface that the user has to implement. You can
compile there java code using javac (or the compiler api), load the
class dynamically, instantiate an object and call some method(s) on it.
This is on the lines of plug-in architecture.

2. Embed a javascript engine. I think Java 6 already does this. Can the
functionality the user has to provide be implemented in javascript?
dhanya - 26 Dec 2006 05:49 GMT
> > Hi folks,
> >
[quoted text clipped - 24 lines]
> 2. Embed a javascript engine. I think Java 6 already does this. Can the
> functionality the user has to provide be implemented in javascript?

Thank You all, for your replies. I have some few doubts. I have
designed a JSP Page as an UI for the user to enter formulas. If the
user enters a formula like if(price>localPrice) then price=10 else
price = 20.
1) When should i check this line for errors? Like it should be done
before saving this formula or while entering itself?
2) Also can i save these in the database itself? If yes what should i
do to retreive and convert to java code, as placing braces "{" properly
etc.

This is not a class room assignment. Pls help me in this.
Oliver Wong - 27 Dec 2006 17:18 GMT
> I have
> designed a JSP Page as an UI for the user to enter formulas. If the
> user enters a formula like if(price>localPrice) then price=10 else
> price = 20.
> 1) When should i check this line for errors? Like it should be done
> before saving this formula or while entering itself?

   You can do it either way, but writing a parser which can assyne the
input is complete is easier.

> 2) Also can i save these in the database itself?

   Any information which you can represent on a computer can be saved in a
DB, if only as a binary stream.

> If yes what should i
> do to retreive and convert to java code, as placing braces "{" properly
> etc.

   This is the first time I see you stating your intent to convert the
formula to Java. Doing this would require writing a full blown compiler. As
I mentioned earlier, ask on comp.compilers.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.