MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Evaluating Passed Arithmetic Expression

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
KOUSIK
Beginner


Joined: 04 Jan 2005
Posts: 26
Topics: 13

PostPosted: Thu Nov 08, 2007 12:37 pm    Post subject: Evaluating Passed Arithmetic Expression Reply with quote

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
View user's profile Send private message
CICS Guy
Intermediate


Joined: 30 Apr 2007
Posts: 292
Topics: 3

PostPosted: Thu Nov 08, 2007 1:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
jyoung
Beginner


Joined: 10 Nov 2005
Posts: 36
Topics: 2
Location: Flint, MI

PostPosted: Thu Nov 08, 2007 1:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
CraigG
Intermediate


Joined: 02 May 2007
Posts: 202
Topics: 0
Location: Viginia, USA

PostPosted: Thu Nov 08, 2007 1:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
KOUSIK
Beginner


Joined: 04 Jan 2005
Posts: 26
Topics: 13

PostPosted: Thu Nov 08, 2007 2:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Nov 08, 2007 2:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
CICS Guy
Intermediate


Joined: 30 Apr 2007
Posts: 292
Topics: 3

PostPosted: Thu Nov 08, 2007 2:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
KOUSIK
Beginner


Joined: 04 Jan 2005
Posts: 26
Topics: 13

PostPosted: Thu Nov 08, 2007 5:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
semigeezer
Supermod


Joined: 03 Jan 2003
Posts: 1014
Topics: 13
Location: Atlantis

PostPosted: Thu Nov 08, 2007 6:58 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Fri Nov 09, 2007 9:09 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group