Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spurious syntax error with ? : in combination with print #233

Open
McDutchie opened this issue May 14, 2024 · 2 comments
Open

spurious syntax error with ? : in combination with print #233

McDutchie opened this issue May 14, 2024 · 2 comments

Comments

@McDutchie
Copy link
Contributor

Found on comp.lang.awk:

From: [email protected]
Newsgroups: comp.lang.awk
Subject: One True AWK bug?
Date: Thu, 9 May 2024 16:43:03 -0000 (UTC)
Organization: A noiseless patient Spider
Message-ID: <[email protected]>

Is this a bug in original-awk, aka "The One True Awk" ?

I noticed in this one-liner if the conditional in the print statement
below lacks parentheses it produces and "illegal statement" error:

--
$ printf 'foo\n\nbar\n' |nawk '{print $0 != "" ? $0 : "[blank]"}'
nawk: syntax error at source line 1
 context is
	{print $0 >>>  != <<< 
nawk: illegal statement at source line 1
nawk: illegal statement at source line 1
--

Adding parentheses OR rewriting as a match fixes things:

--
$ printf 'foo\n\nbar\n' |nawk '{print $0 !~ /^$/ ? $0 : "[blank]"}'
foo
[blank]
bar

$ printf 'foo\n\nbar\n' |nawk '{print ($0 != "") ? $0 : "[blank]"}'
foo
[blank]
bar
--

Both mawk and gawk don't seem to care about lack of parentheses:

--
$ printf 'foo\n\nbar\n' |mawk '{print $0 != "" ? $0 : "[blank]"}'
foo
[blank]
bar

$ printf 'foo\n\nbar\n' |gawk '{print $0 != "" ? $0 : "[blank]"}'
foo
[blank]
bar
--

Note 'nawk' here is NOT gawk, it's a more recent version of original-awk:

$ nawk --version    # from https://github.com/onetrueawk/awk
awk version 20240122 

$ original-awk --version  # version Debian apt currently has
awk version 20220912
@arnoldrobbins
Copy link
Collaborator

printfix.txt

This may fix it, but it increases the number of shift/reduce and reduce/reduce conflicts. And I didn't run the test suite on it.

In general, the grammar is a MESS; in particular I don't understand the reason for separate pattern and ppattern and related non-terminals.

@plan9 Thoughts welcome.

@plan9
Copy link
Collaborator

plan9 commented May 15, 2024

I agree the grammar is in need of some careful cleanup. I had spent some time working on that for eg. issue #149 but left unfinished. I will take a close look at this one as well, and see if I can come up with a solution without increasing the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants