View previous topic :: View next topic |
Author |
Message |
KOUSIK Beginner
Joined: 04 Jan 2005 Posts: 26 Topics: 13
|
Posted: Thu Nov 08, 2007 12:37 pm Post subject: Evaluating Passed Arithmetic Expression |
|
|
Can somebody please help me out in a way to evaluate a passed arithmetic expression in COBOL.
We have a logic wherein we form arithmetic expressions in our shop. But, we need to evaluate these arithmetic expressions to TRUE or FALSE and proceed based on the result to a different logic.
Say for example,
I may form an expression like : (A+B*(C-D)+E) < (F+G-(H+I))
or an expression like
(A-B+(C*D)-E) > (F+G*(H-I)) ,
I will have to evaluate such expressions to a true or false.
Note : The expressions are dynamic. We have logic to form these expressions.
As far I know COBOL can evaluate static expressions to either TRUE or FALSE.
Any help is greatly appreciated!
Regards,
Kousik |
|
Back to top |
|
|
CICS Guy Intermediate
Joined: 30 Apr 2007 Posts: 292 Topics: 3
|
Posted: Thu Nov 08, 2007 1:10 pm Post subject: |
|
|
Assuming the program has access to the values of the variables, the evaluation of the expression should be a straight forward programming exersize..... |
|
Back to top |
|
|
jyoung Beginner
Joined: 10 Nov 2005 Posts: 36 Topics: 2 Location: Flint, MI
|
Posted: Thu Nov 08, 2007 1:13 pm Post subject: |
|
|
Would this work?
compute x = (A+B*(C-D)+E)
compute j = (F+G-(H+I))
if x < j
do something (true)
else
do something else. (false) |
|
Back to top |
|
|
CraigG Intermediate
Joined: 02 May 2007 Posts: 202 Topics: 0 Location: Viginia, USA
|
Posted: Thu Nov 08, 2007 1:41 pm Post subject: |
|
|
CICS Guy wrote: | Assuming the program has access to the values of the variables, the evaluation of the expression should be a straight forward programming exersize..... |
I don't think so.
You have to parse the statement an find the < > = etc that separates the two parts. Then for each part you would have to find the inner most () and begin evaluating you would have evaluate the operands and the operation and work you way out. Being a compiled language rather than an interpretive one, COBOL is not well suited for this task. I'm not saying that it can't be done but I don't think a programmer that could do it would be asking if it could be done. |
|
Back to top |
|
|
KOUSIK Beginner
Joined: 04 Jan 2005 Posts: 26 Topics: 13
|
Posted: Thu Nov 08, 2007 2:18 pm Post subject: |
|
|
Thanks for the response!
But, jyoung wrote
Quote: |
Would this work?
compute x = (A+B*(C-D)+E)
compute j = (F+G-(H+I))
|
The expressions (A+B*(C-D)+E) and (F+G-(H+I)) are strings that will be passed from a different program. I guess these expressions should be made statements for the compiler to evaluate them. As long as they are not statements they would be considered strings.
CraigG, I would agree to what you said! I guess I am totally struck in doing this!!! |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Thu Nov 08, 2007 2:22 pm Post subject: |
|
|
As previously stated, it will be very difficult to come up with a solution in COBOL because it is a compiled language. You'd be better off using the right tool for the job. REXX would be a better choice because you could use the INTERPRET command which does exactly what you want; executes a statement generated at execution. You might be able to play around with DB2 and generate dynamic SQL with a CASE, but that's probably more trouble than it's worth. |
|
Back to top |
|
|
CICS Guy Intermediate
Joined: 30 Apr 2007 Posts: 292 Topics: 3
|
Posted: Thu Nov 08, 2007 2:36 pm Post subject: |
|
|
CraigG wrote: | COBOL is not well suited for this task. I'm not saying that it can't be done but I don't think a programmer that could do it would be asking if it could be done. | Oh I agree, but as a mental exercise, it might be fun to try.....I'm thinking about a 3 dimensional table, dropping down for each set of parenthesis or operational precedence..... |
|
Back to top |
|
|
KOUSIK Beginner
Joined: 04 Jan 2005 Posts: 26 Topics: 13
|
Posted: Thu Nov 08, 2007 5:46 pm Post subject: |
|
|
Sharon,
Thanks a lot...I definitely learnt a new instruction in REXX...May be I will try using this one and let you know how it works out...
I am not sure if I can couple REXX and COBOL somehow but high time I crack this down...
Really appreciate all you guys for the advise. |
|
Back to top |
|
|
semigeezer Supermod
Joined: 03 Jan 2003 Posts: 1014 Topics: 13 Location: Atlantis
|
Posted: Thu Nov 08, 2007 6:58 pm Post subject: |
|
|
The normal way to do this is to create a parse tree to represent the tokens and operators in tree form. If I recall, nodes are either operators or variables and the tree is built in a form such that the order of traversal matches the precedence of operators (which when traversed results in so-called "reverse polish notation"). For example, parenthesized operations are a subtree. To do the actual evaluation, you then traverse the tree either doing operations or variable value substitutions. It has been 25+ years since I looked at this type of thing, but I think that is the basic idea. If you want to get details, look in one of the standard compiler texts (like Aho, Sethi & Ullman) or a good algorithm book (presumably Knuth or Tremblay & Sorensen).
The problem with doing this in COBOL is that COBOL stinks when it comes to dynamic data structures. PL/I would be a better choice. For tree representation, you can, of course, use arrays and fake it (Indexes simulate pointers for example), but that is kind of ugly.
I suspect that you could buy commercial callable routines to do this sort of evaluation but I have no experience with that (and I haven't seen such utility packages advertised in a long time). _________________ New members are encouraged to read the How To Ask Questions The Smart Way FAQ at http://www.catb.org/~esr/faqs/smart-questions.html. |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Fri Nov 09, 2007 9:09 am Post subject: |
|
|
KOUSIK
Passing the expression from COBOL to REXX will be a little tricky. You stated that the expression is dynamic. How will you know what variables to pass from the COBOL module to the REXX exec? I'm not saying that it can't be done, just that you'd need to devise a way to do it. If possible, it would be easier to code and maintain if you let REXX do more of the work. In other words, generate and evaluate the expression in REXX. |
|
Back to top |
|
|
|
|