Author Topic: Nested Parentheses in Logical Expressions  (Read 11370 times)

Patrick Kuras

  • Guest
Nested Parentheses in Logical Expressions
« on: May 01, 2003, 05:43:23 AM »
I am using a TBasic function to read data from the serial port, and I need to strip possible extraneous characters from the front of the input string. I am using a WHILE loop with a fairly complex set of logical tests to examine the first character of the string, and remove it if it is not what I expect to see. I've also included a "safety valve" that prevents the loop from running more than 20 times, to avoid infinite loops.

My code follows:

  while ((strcmp(strupr$(mid$(I$,1,1)),"A")=0) or (strcmp(strupr$(mid$(I$,1,1)),"R")=0)) and (len(i$)>0) and (i<20)

    I$ = mid$(I$,2,len(I$)-1)
    i=i+1

  endwhile

The nested parentheses are used to ensure that the tests are executed in the correct order, but the code will not compile as shown above. The compiler seems unable to deal with the nested parentheses.

Is there another way to accomplish what I need to do? The basic requirement is to strip sharacters from the front of the string until I see an "A" or an "R" or the string is empty, or the string has iterated 20 times.
« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re: Nested Parentheses in Logical Expressions
« Reply #1 on: May 01, 2003, 01:35:28 PM »
If you just want to test one character, it is quite efficient to use the ASC function. If more than 1 character is needed for comparison, then replace it with a MID$ function.

The following code should do what you want to do:
---------------------------

I$ = INPUT$(1)

IF LEN(I$)=0 RETURN: ENDIF

IF ASC(I$,1)=65 OR ASC(I$,1)=82  ' 'A' is ascii 65, 'R is ascii 82
   I$ = MID$(I$,2,len(I$)-1)
ELSE
   I$ = "Wrong string"   ' or whatever !!
ENDIF
« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

pkuras

  • Guest
Re: Nested Parentheses in Logical Expressions
« Reply #2 on: May 01, 2003, 04:42:11 PM »
Thanks for the reply.

I have used the ASC() function in different versions of this code, but I find that the issue is not one of which function to use, but the complexity of the logical expression. The expression in my original post has many layers of nested parentheses, and fails to compile, with a syntax error. You can try it, and you'll see what I mean.

By the way, the code I posted originally is not right. The actual test I need to do is as follows (with the ASC() function).

  while ((asc(strupr$(I$),1)<>65) or (asc(strupr$(I$),1)<>82)) and (len(i$)>0) and (i<20)

    I$ = mid$(I$,2,len(I$)-1)
    i=i+1

  endwhile

By embedding the zero length test in the while loop, the loop will terminate if the string is composed entirely of undesirable characters and is stripped to zero length. And the iteration count is a wise precaution against executing in the loop infinitely.

This version still fails to compile, but I believe it is syntactically correct. It appears that your compiler is not happy with the use of a comparison expression (=, >, <, <>) in parentheses, perhaps only when used along with boolean operators (AND, OR). If I take the two outermost layers of parentheses off the comparison expressions, like this:

  while asc(strupr$(I$),1)<>65 or asc(strupr$(I$),1)<>82 and len(i$)>0 and i<20

the expression compiles, but is incorrect. According to your documentation, comparison operators have precedence over boolean operators in determining order of operations, so "65 or asc(strupr$(I$),1)", "82 and len(i$)" and "0 and i" will evaluate before the remainder of the expression. This is wrong, and if the compiler actually behaves this way, the code will not result in correct operation. With all the parentheses, the expression is syntactically and logically correct, but will not compile.

Are there limits as to the parsing capabilities of your compiler with regard to nested parentheses? Is the documentation with regard to order of operations correct? Have I discovered a compiler bug?

Please assist...

Thanks,

Patrick Kuras
« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re: Nested Parentheses in Logical Expressions
« Reply #3 on: May 02, 2003, 12:11:32 PM »
You are right that the current TBASIC compiler doesn't process complexed logical expression the way C language does. Basically a While expression expect a simple comparison expression that could be joined by an "AND" or an "OR" operator.  It doesn't process logical expression within brackets (that may change in future). That's why terse expression like your example will not compile properly.

An equivalent expression is as follow:

I$ = INPUT$(1)

FOR I = 1 TO 20
     X = ASC(strupr$(I$),1)
     IF LEN(i$)=0 RETURN: ENDIF
     IF ASC(strupr$(I$),1)<>65 AND asc(strupr$(I$),1)<>82
         GOTO @5
     ELSE
         I$ = mid$(I$,2,len(I$)-1)
     ENDIF
NEXT

@5  ' Out of loop location



« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re: Nested Parentheses in Logical Expressions
« Reply #4 on: June 23, 2003, 03:17:57 PM »
In the TRiLOGI version 5.2 we have just released, we have included support for compilation of complex logical expression as per your example.

All licensed TRiLOGI users can obtain free upgrade by following the "upgrade.htm" document in their CD ROM.
« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS