• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursive problem - I need an idea.

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got a string construct of the type:



which I need to resolve, checking the boolean expression and returning the correct value appropriately (just like a ternary conditional would, e.g. booleanExression == true, return value1, otherwise return value2). Simple enough, however value1 and value2 will not necessarily be automic values, but can be (infinitely) nested instances of the same function. An example:



So basically to resolve this by finding the most deeply nested functions first, resolving them and working out to the least nested function.

I'm trying to think of an efficient way to do this, but nothing is springing to mind. If anyone has any suggestions, they would be much appreciated.
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

which I need to resolve, checking the boolean expression and returning the correct value appropriately (just like a ternary conditional would, e.g. booleanExression == true, return value1, otherwise return value2). Simple enough, however value1 and value2 will not necessarily be automic values, but can be (infinitely) nested instances of the same function



You can do it easily as



And as far function(value > 4, function(anotherValue % 2, "ONE", "TWO"), "THREE") code is concern. It will automatically evaluare the inner function and then evaluable the outer function i.e. function(anotherValue%2,"ONE","TWO") will be evaluated first and then other one is evaluated. So you don't need to bother this is compiler's work.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok scratch my last reply. I've thought more about this and have to ask: How is the compiler going to do that?
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Ali didn't get your question right, since the signature:

shows 'String val1', while the example passes a 'function'.

In your example

the inner function doesn't match the java-language

since 'value % 2' returns an int, not an boolean.

Perhaps you get a step further by method-overloading:

Perhaps the last form might do all the work, if the FO knows, whether it is a final string token, or another function.
In real, the situation is more complicated, since I understand, that the boolean expression is passed as String as well.
So you have to define a valid syntax (while it might be more easy, to be very restricted here, to make parsing more easy:

Are only arithmetic expressions allowed?
Are floats/ doubles allowed?
Which operators? ...

And of course you have the problem of parsing, and deciding 'function? String?'.

I guess you need some literature for compiler-building
[ May 20, 2004: Message edited by: Stefan Wagner ]
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Stefan, you seem to follow what I'm asking. I should have made it clearer that I'm getting this whole function as a string, so here's a little more background. There is no checking on this string to see if its syntactically valid before it gets to my code - the syntax is a from a legacy scripting language. I'm basically having to wrap the funcionality of a number of functions from this scripting language so I can perform the same work in my java app. I can't use a bridge or native calls to do this, since the java app runs entirely seperatly from the legacy app, and the legacy app requires licence costs, so asking user of my java app to install it too is out of the question (and its Windows only). However, both apps can in theory be run concurrently so I need support the legacy functionality.

Your questions:
- both arithmetic and boolean expressions are allowed (with arithmetic, a positive result == true. My example is acutally pretty pointless, but given a user can validly enter this nonsense code perhaps it isn't
)
- all the common types are allowed (including Dates)
- it needs to support all the common operators you see in most languages (e.g. common logical and arithmetic operators), not quite the full set of Java opertator, but a fairly large subset.
- the syntax is fairly loose. It a bit like VBScript, so functions need not be parentetised, it is case insensetive and uses variants as parameters (which is actually a bit of a help here, since I get everthing a a string so dates, ints, doubles etc. need to be wrapped in int( string ) etc. functions first - otherwise I'd have no idea what datatype the users is using).

Basically what I'm doing so far is creating classes to mimic the functionality of these legacy functions, and writing classes which provide the same sort of behaviour as the JavaScript evaluation function. However most of the work is going in to horrible resursive string manipulation and regular expression matching methods to spilt out the constituent parts of this (possibly infinitely nested) function before I can safely call any of these functions.

This is giving me a headache. Anyone with any ideas?
[ May 21, 2004: Message edited by: Paul Sturrock ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well - we are told not to present solutions, but small hints and clever questions.

But I was interested in this topic and started coding

It's not working correctly, but perhaps you want to have a look at it.
I assumed Integers but converting to double shouldn't be a problem.
Dates will be a problem.
The resultstring is supposed not to include blanks and not to be surrounded by ' " '. - they are treated as part of the string.
Only few operators are implemented, but (a % b) might easily be tested as ((a % b) != 0) - note that negative numbers are interpreted as TRUE in basic, c, c++ ... too - only 0 meaning false.
And I assume all Strings being restrictive well formated (though my parsing is still buggy).
And I reduced 'function(' to 'f('.

Perhaps it's possible to preformat the entire statement.

I'm later offline for approx. 24 h.

I send it by email - the ubb refuses my > < chars, even after I replacedAll (...).
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know whether you know lex and yacc - or jlexx jyacc or how they are named for java.

It's something new to learn, but might help, if understood, and is, AFAIK, OpenSource.
 
reply
    Bookmark Topic Watch Topic
  • New Topic