If-Then-Else syntax error....

25 views
Skip to first unread message

Sid Andal

unread,
Jan 4, 2025, 1:08:08 PM1/4/25
to FriCAS - computer algebra system
The file test.input contains the following lines from the manual
on acceptable (indented) if-then-else expressions:

cat test.input:

if 1 > 0 then output("positive") else output("nonpositive")

if 1 > 0 then output("positive")
   else output("nonpositive")

if 1 > 0 then output("positive")
else output("nonpositive")

if 1 > 0
then output("positive")
else output("nonpositive")

if i > 0
   then output("positive")
   else output("nonpositive")


Except for the first one-liner, the remaining ones give syntax errors:


(1) -> )r test
if 1 > 0 then output("positive") else output("nonpositive")
 
   positive

if 1 > 0 then output("positive")
 
   positive
else output("nonpositive")
 
  Line   4: else output("nonpositive")
           A
  Error  A: Improper syntax.
   1 error(s) parsing

   Continuing to read the file...


if 1 > 0 then output("positive")
 
   positive
else output("nonpositive")
 
  Line   7: else output("nonpositive")
           A
  Error  A: Improper syntax.
   1 error(s) parsing

   Continuing to read the file...


if 1 > 0
 
  Line   8:
  Line   9: if 1 > 0
           .......A
  Error  A: syntax error at top level
  Error  A: Possibly missing a then
   2 error(s) parsing

   Continuing to read the file...

then output("positive")
 
  Line  10: then output("positive")
           A
  Error  A: Improper syntax.
   1 error(s) parsing

   Continuing to read the file...

else output("nonpositive")
 
  Line  11: else output("nonpositive")
           A
  Error  A: Improper syntax.
   1 error(s) parsing

   Continuing to read the file...


if 1 > 0
 
  Line  12:
  Line  13: if 1 > 0
           .......A
  Error  A: syntax error at top level
  Error  A: Possibly missing a then
   2 error(s) parsing

   Continuing to read the file...

then output("positive")
 
  Line  14: then output("positive")
           A
  Error  A: Improper syntax.
   1 error(s) parsing

   Continuing to read the file...

else output("nonpositive")
 
  Line  15: else output("nonpositive")
           A
  Error  A: Improper syntax.
   1 error(s) parsing

   Continuing to read the file...

(4) ->

Thanks,
SWA

Kurt Pagani

unread,
Jan 5, 2025, 4:47:47 AM1/5/25
to FriCAS - computer algebra system
Use line continuation (_) or correct indentation (see last item)


if 1 > 0 then output("positive") else output("nonpositive")

if 1 > 0 then output("positive") _
   else output("nonpositive")

if 1 > 0 then output("positive") _
else output("nonpositive")

if 1 > 0 _
then output("positive") _
else output("nonpositive") _

if 1 > 0 _
   then output("positive") _

   else output("nonpositive")
   
   
if 1 > 0 then
  output("positive") else
    output("nonpositive")


  --


if 1 > 0 then output("positive") else output("nonpositive")
 
   positive
                                                                   Type: Void

if 1 > 0 then output("positive") _

   else output("nonpositive")
 
   positive
                                                                   Type: Void

if 1 > 0 then output("positive") _

else output("nonpositive")
 
   positive
                                                                   Type: Void

if 1 > 0 _
then output("positive") _
else output("nonpositive") _
 
   positive
                                                                   Type: Void

if 1 > 0 _
   then output("positive") _

   else output("nonpositive")
 
   positive
                                                                   Type: Void

   
   
if 1 > 0 then
  output("positive") else
    output("nonpositive")
 
   positive
                                                                   Type: Void
(12) ->

Kurt Pagani

unread,
Jan 5, 2025, 5:23:15 AM1/5/25
to FriCAS - computer algebra system
You are right, this does not hold anymore (apparently). The docs should be adjusted? I wasn't aware of this exception of the pile rule.

--- chapter 5.3
The usual rules for piles are suspended for conditional expressions. In .input files, the then and else keywords can begin in the same column as the corresponding if but may also appear to the right. Each of the following styles of writing if-then-else expressions is acceptable:


if i>0 then output("positive") else output("nonpositive")

if i > 0 then output("positive")
  else output("nonpositive")

if i > 0 then output("positive")
else output("nonpositive")

if i > 0
then output("positive")
else output("nonpositive")

if i > 0
  then output("positive")
  else output("nonpositive")
---

In function bodies it's ok.

f(x) ==
  if x > 0 then
    return 1
  else
    return 0

Waldek Hebisch

unread,
Jan 5, 2025, 8:06:02 AM1/5/25
to fricas...@googlegroups.com
On Sun, Jan 05, 2025 at 02:23:15AM -0800, Kurt Pagani wrote:
<fixed order>
> > On Saturday, 4 January 2025 at 19:08:08 UTC+1 Sid Andal wrote:
> >
> >> The file test.input contains the following lines from the manual
> >> on acceptable (indented) if-then-else expressions:
> >>
> >> cat test.input:
> >>
> >> if 1 > 0 then output("positive") else output("nonpositive")
> >>
> >> if 1 > 0 then output("positive")
> >> else output("nonpositive")
<snip>
> >> Except for the first one-liner, the remaining ones give syntax errors:

The ones above work for me.

> You are right, this does not hold anymore (apparently). The docs should be
> adjusted? I wasn't aware of this exception of the pile rule.
>
> --- chapter 5.3
> *The usual rules for piles are suspended for conditional expressions. In
> .input files,* the then and else keywords can begin in the same column as
> the corresponding if but may also appear to the right. Each of the
> following styles of writing if-then-else expressions is acceptable:

This if very loose formulation, actual rules are much more complicated
(and currently it is hard to explain them better than "follow the
source code of pile handling"). It could be true of handling in the
"old compiler", but this is no longer in use.

One thing is that handling at indentation level 0 (that is for top
level constructs) is different. So let me assume that we are inside
a function.

> if i>0 then output("positive") else output("nonpositive")
>
> if i > 0 then output("positive")
> else output("nonpositive")

This is misleading because

foo2(i) ==
if i > 0 then
output("positive")
else output("nonpositive")

does not work. That is example number 2 works because of rule
"single line makes no pile", not because of handling for 'if'
(and for me it also works at top level).

> if i > 0 then output("positive")
> else output("nonpositive")
>
> if i > 0
> then output("positive")
> else output("nonpositive")
>
> if i > 0
> then output("positive")
> else output("nonpositive")
> ---
>
> In function bodies it's ok.
>
> f(x) ==
> if x > 0 then
> return 1
> else
> return 0

Actually, for me

foo5(i) ==
if i > 0
then output("positive")
else output("nonpositive")

fails.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages