GNU bug report logs - #52873
expr unexpected syntax error

Previous Next

Package: coreutils;

Reported by: Martin Rixham <martin.rixham <at> equalexperts.com>

Date: Wed, 29 Dec 2021 16:14:02 UTC

Severity: normal

Tags: notabug

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 52873 in the body.
You can then email your comments to 52873 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#52873; Package coreutils. (Wed, 29 Dec 2021 16:14:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Martin Rixham <martin.rixham <at> equalexperts.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Wed, 29 Dec 2021 16:14:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Martin Rixham <martin.rixham <at> equalexperts.com>
To: bug-coreutils <at> gnu.org
Subject: expr unexpected syntax error
Date: Wed, 29 Dec 2021 12:42:24 +0000
[Message part 1 (text/plain, inline)]
I'm getting an error from the following:

    [martin <at> fedora ~]$ expr ')' : '.*'
    expr: syntax error: unexpected ')'

There also seems to be a similar problem with:

    expr '(' : '.*'

Here's the version:

    [martin <at> fedora ~]$ expr --version
    expr (GNU coreutils) 8.32
    Copyright (C) 2020 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <
https://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Written by Mike Parker, James Youngman, and Paul Eggert.

And a uname for good measure:

    [martin <at> fedora ~]$ uname -a
    Linux fedora 5.15.7-200.fc35.x86_64 #1 SMP Wed Dec 8 19:00:47 UTC 2021
x86_64 x86_64 x86_64 GNU/Linux

Thanks,

Martin
[Message part 2 (text/html, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#52873; Package coreutils. (Wed, 29 Dec 2021 16:32:01 GMT) Full text and rfc822 format available.

Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Davide Brini <dave_br <at> gmx.com>
To: bug-coreutils <at> gnu.org
Subject: Re: bug#52873: expr unexpected syntax error
Date: Wed, 29 Dec 2021 17:31:31 +0100
On Wed, 29 Dec 2021 12:42:24 +0000, Martin Rixham
<martin.rixham <at> equalexperts.com> wrote:

> I'm getting an error from the following:
>
>     [martin <at> fedora ~]$ expr ')' : '.*'
>     expr: syntax error: unexpected ')'
>
> There also seems to be a similar problem with:
>
>     expr '(' : '.*'

I think you need to use '+' before the offending token, at least according
to the man page:

       + TOKEN
              interpret TOKEN as a string, even if it is a
              keyword like 'match' or an operator like '/'

And indeed:

$ expr '+' ')' : '.*'
1

--
D.




Added tag(s) notabug. Request was from Paul Eggert <eggert <at> cs.ucla.edu> to control <at> debbugs.gnu.org. (Wed, 29 Dec 2021 19:53:02 GMT) Full text and rfc822 format available.

Reply sent to Paul Eggert <eggert <at> cs.ucla.edu>:
You have taken responsibility. (Wed, 29 Dec 2021 19:53:02 GMT) Full text and rfc822 format available.

Notification sent to Martin Rixham <martin.rixham <at> equalexperts.com>:
bug acknowledged by developer. (Wed, 29 Dec 2021 19:53:02 GMT) Full text and rfc822 format available.

Message #15 received at 52873-done <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Davide Brini <dave_br <at> gmx.com>
Cc: Martin Rixham <martin.rixham <at> equalexperts.com>, 52873-done <at> debbugs.gnu.org
Subject: Re: bug#52873: expr unexpected syntax error
Date: Wed, 29 Dec 2021 11:52:25 -0800
On 12/29/21 08:31, Davide Brini wrote:
> I think you need to use '+' before the offending token

Yes. That's a GNU extension. If you want to be portable to any POSIX 
implementation, you can use this instead:

expr "X(" : '.*' - 1

A similar example is given in the POSIX spec for 'expr':

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/expr.html

As this is not a bug, I'm closing the bug report.




Information forwarded to bug-coreutils <at> gnu.org:
bug#52873; Package coreutils. (Wed, 29 Dec 2021 20:59:02 GMT) Full text and rfc822 format available.

Message #18 received at 52873-done <at> debbugs.gnu.org (full text, mbox):

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Martin Rixham <martin.rixham <at> equalexperts.com>
Cc: 52873-done <at> debbugs.gnu.org, Davide Brini <dave_br <at> gmx.com>
Subject: Re: bug#52873: expr unexpected syntax error
Date: Wed, 29 Dec 2021 12:57:59 -0800
On 12/29/21 12:01, Martin Rixham wrote:
> What nonsense. I want to parse source code. ')' is not an uncommon line of
> source code. It should work.

Unfortunately, you're asking for what is in general impossible. If the 
left argument of ':' could be any string, then the grammar for 'expr' 
would be ambiguous. Consider the following shell command:

expr '(' : ')'

This outputs ':' because it evaluates the parenthesized string ':'; but 
if the operands of ':' could be any strings it could also be interpreted 
as matching '(' against ')', which means it should output the same thing 
as 'expr a : b', namely '0'.

Of course this means 'expr' was poorly designed in the 1970s, but we're 
stuck with that design now (it's standardized by POSIX), portable code 
must deal with this poor design, and for compatibility reasons it's 
better for GNU expr to support the design, poor as it is.

These days there are much better ways than 'expr' to parse code. For 
example, if you want to count the number of characters in a shell 
variable v, you can use this shell command:

nv=${#v}

This works even if v=')', whereas this:

nv=$(expr "$v" : '.*')

has the bug that you mentioned, plus it's harder to read and it's less 
efficient.




Information forwarded to bug-coreutils <at> gnu.org:
bug#52873; Package coreutils. (Wed, 29 Dec 2021 21:00:02 GMT) Full text and rfc822 format available.

Message #21 received at 52873-done <at> debbugs.gnu.org (full text, mbox):

From: Martin Rixham <martin.rixham <at> equalexperts.com>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 52873-done <at> debbugs.gnu.org, Davide Brini <dave_br <at> gmx.com>
Subject: Re: bug#52873: expr unexpected syntax error
Date: Wed, 29 Dec 2021 20:01:54 +0000
[Message part 1 (text/plain, inline)]
What nonsense. I want to parse source code. ')' is not an uncommon line of
source code. It should work.

On Wed, 29 Dec 2021 at 19:52, Paul Eggert <eggert <at> cs.ucla.edu> wrote:

> On 12/29/21 08:31, Davide Brini wrote:
> > I think you need to use '+' before the offending token
>
> Yes. That's a GNU extension. If you want to be portable to any POSIX
> implementation, you can use this instead:
>
> expr "X(" : '.*' - 1
>
> A similar example is given in the POSIX spec for 'expr':
>
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/expr.html
>
> As this is not a bug, I'm closing the bug report.
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#52873; Package coreutils. (Wed, 29 Dec 2021 22:15:02 GMT) Full text and rfc822 format available.

Message #24 received at 52873-done <at> debbugs.gnu.org (full text, mbox):

From: Martin Rixham <martin.rixham <at> equalexperts.com>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: 52873-done <at> debbugs.gnu.org, Davide Brini <dave_br <at> gmx.com>
Subject: Re: bug#52873: expr unexpected syntax error
Date: Wed, 29 Dec 2021 22:07:25 +0000
[Message part 1 (text/plain, inline)]
ok I appreciate the explanation.

On Wed, 29 Dec 2021 at 20:58, Paul Eggert <eggert <at> cs.ucla.edu> wrote:

> On 12/29/21 12:01, Martin Rixham wrote:
> > What nonsense. I want to parse source code. ')' is not an uncommon line
> of
> > source code. It should work.
>
> Unfortunately, you're asking for what is in general impossible. If the
> left argument of ':' could be any string, then the grammar for 'expr'
> would be ambiguous. Consider the following shell command:
>
> expr '(' : ')'
>
> This outputs ':' because it evaluates the parenthesized string ':'; but
> if the operands of ':' could be any strings it could also be interpreted
> as matching '(' against ')', which means it should output the same thing
> as 'expr a : b', namely '0'.
>
> Of course this means 'expr' was poorly designed in the 1970s, but we're
> stuck with that design now (it's standardized by POSIX), portable code
> must deal with this poor design, and for compatibility reasons it's
> better for GNU expr to support the design, poor as it is.
>
> These days there are much better ways than 'expr' to parse code. For
> example, if you want to count the number of characters in a shell
> variable v, you can use this shell command:
>
> nv=${#v}
>
> This works even if v=')', whereas this:
>
> nv=$(expr "$v" : '.*')
>
> has the bug that you mentioned, plus it's harder to read and it's less
> efficient.
>
[Message part 2 (text/html, inline)]

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 27 Jan 2022 12:24:12 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 194 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.