bug#52873: expr unexpected syntax error

2021-12-29 Thread Martin Rixham
ok I appreciate the explanation.

On Wed, 29 Dec 2021 at 20:58, Paul Eggert  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.
>


bug#52873: expr unexpected syntax error

2021-12-29 Thread Martin Rixham
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  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.
>


bug#52873: expr unexpected syntax error

2021-12-29 Thread Paul Eggert

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.






bug#52873: expr unexpected syntax error

2021-12-29 Thread Paul Eggert

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.





bug#52873: expr unexpected syntax error

2021-12-29 Thread Davide Brini
On Wed, 29 Dec 2021 12:42:24 +, Martin Rixham
 wrote:

> I'm getting an error from the following:
>
> [martin@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.





bug#52873: expr unexpected syntax error

2021-12-29 Thread Martin Rixham
I'm getting an error from the following:

[martin@fedora ~]$ expr ')' : '.*'
expr: syntax error: unexpected ')'

There also seems to be a similar problem with:

expr '(' : '.*'

Here's the version:

[martin@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@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