0

How do I go about translating a Natural language logical condition into its Java code counterpart?

Say I have this condition

(Color Equals Blue) AND (Name Contains Smith)

What can be done to translate this to a Java level code, which might look like

(Color.equals("Blue") && (Name.contains("Smith")))

I could not come up with any definitive approach to achieve the desired outcome, so here I am asking this question. Also, please let me know that reason before down-voting.

Anjan Baradwaj
  • 1,219
  • 5
  • 27
  • 54
  • 2
    Try [antlr](http://www.antlr.org/) – wmorrison365 Jun 17 '14 at 13:47
  • 1
    It may be easier for you if you choose different language than java. In language from lisp family (similar things can be done in python etc.), for example clojure, you can do (load-string "(+ 1 1)") which evaluate string and you will have macros to transform input. (Clojure provides easy acces to and from Java) Look at http://clojure.org/ – boucekv Jun 17 '14 at 13:54
  • 2
    Also this http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form may help you. – boucekv Jun 17 '14 at 13:57

1 Answers1

3

I would try a dsl that parses the syntax you specified (Color Equals Blue) AND (Name Contains Smith) into java code. Make sure you understand the grammar for this syntax very well though before you start translating it into your parser grammar. A language specification link would help a lot. In the past I used ANTLR and it was really easy to generate JAVA code once i had the syntax tree from the output string.

omu_negru
  • 4,642
  • 4
  • 27
  • 38