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

print does not print anything if not followed by a space or ";" #73

Open
ghuls opened this issue Sep 15, 2021 · 6 comments
Open

print does not print anything if not followed by a space or ";" #73

ghuls opened this issue Sep 15, 2021 · 6 comments

Comments

@ghuls
Copy link
Contributor

ghuls commented Sep 15, 2021

$ echo 'test' | frawk '{print}'
$ echo 'test' | frawk '{print }'
test
$ echo 'test' | frawk '{print;}'
test
$ echo 'test' | frawk '{print $0}'
test
@ezrosent ezrosent reopened this Sep 16, 2021
@ezrosent
Copy link
Owner

Nice find! This was a lexer issue.

Didn't intend to close this immediately, though I do think the commit above should fix things. I'll double-check soon and close this once there's a new version.

@ghuls
Copy link
Contributor Author

ghuls commented Sep 16, 2021

I found it by running one of the onetrueawk tests.

@ghuls
Copy link
Contributor Author

ghuls commented Sep 16, 2021

Similar issue with split:

$ echo 'a b c' | frawk '{n = split($0, x, " "); print n; }'
3

$ echo 'a b c' | frawk '{n = split ($0, x, " "); print n; }'

$ echo 'a b c' | mawk '{n = split ($0, x, " "); print n; }'
3

$ echo 'a b c' | tawk '{n = split ($0, x, " "); print n; }'
3

$ echo 'a b c' | gawk '{n = split ($0, x, " "); print n; }'
3

And substr:

$ echo 'a b c' | frawk '{ print substr ($0, 1, 3); print n; }'
Unrecognized token `,` found at line 1, column 19:line 1, column 20
Expected one of ")"

$ echo 'a b c' | mawk '{ print substr ($0, 1, 3); print n; }'
a b

$ echo 'a b c' | tawk '{ print substr ($0, 1, 3); print n; }'
a b

$ echo 'a b c' | gawk '{ print substr ($0, 1, 3); print n; }'
a b

I assume that spaces are always allowed after a function name and before a "(".

printf without arguments probably shouldn't be allowed (although tawk behaves differently):

$ echo 'a' | tawk '{printf}'
a[no_newline]

# Doesn't print anything.
$ echo 'a' | frawk '{printf}'

$ echo 'a' | frawk '{printf;}'
Unrecognized token `;` found at line 1, column 8:line 1, column 9
Expected one of "!", "$", "(", "+", "++", "-", "--", "CALLSTART", "FLOAT", "HEX", "IDENT", "INT", "PATLIT" or "STRLIT"

$ echo 'a' | mawk '{printf}'
mawk: line 1: no arguments in call to printf
$ echo 'a' | gawk '{printf}'
gawk: cmd. line:1: (FILENAME=- FNR=1) fatal: printf: no arguments

@ezrosent
Copy link
Owner

I assume that spaces are always allowed after a function name and before a "(".

This isn't quite true.. in awk you can add spaces but only for builtin functions. For user-defined functions no spaces are allowed:

% gawk 'function x(y) { return y+1; } BEGIN { print x (1); }'
gawk: cmd. line:1: error: function `x' called with space between name and `(',
or used as a variable or an array

frawk treats all functions the same. This was a deliberate choice on my part to simplify parsing, and also avoid confusion. I could see adding it in, but if so I'd probably file it as a separate issue.

@ghuls
Copy link
Contributor Author

ghuls commented Sep 17, 2021

This isn't quite true.. in awk you can add spaces but only for builtin functions. For user-defined functions no spaces are allowed:

% gawk 'function x(y) { return y+1; } BEGIN { print x (1); }'
gawk: cmd. line:1: error: function `x' called with space between name and `(',
or used as a variable or an array

In mawk it also works for custom functions (but not in gawk and onetrueawk).

frawk treats all functions the same. This was a deliberate choice on my part to simplify parsing, and also avoid confusion. I could see adding it in, but if so I'd probably file it as a separate issue.

It is indeed a pity that AWK allows so many small variations of the syntax.

It would be nice if frawk would have an option which would run the passed awk code trough gawk (5.x) --pretty-print option, so more valid awk programs can be executed with frawk.

# Valid AWK program that frawk is unable to handle atm.
$ frawk '{ if (NR <= 5) print( length    ($0  )  )  }' /etc/ssh/ssh_config
Unrecognized token `}` found at line 1, column 44:line 1, column 45
Expected one of "\n" or ";"

# Valid AWK program that frawk is unable to handle atm.
$  frawk '{ if (NR <= 5) print( length($0)  ) }' /etc/ssh/ssh_config
Unrecognized token `}` found at line 1, column 37:line 1, column 38
Expected one of "\n" or ";"

$  frawk '{ if (NR <= 5) print( length($0)  ); }' /etc/ssh/ssh_config
42
13
0
60
0


# Pass whole awk program (-f files and program given as argument) as a named pipe to gawk --pretty-print) and execute the resulting awk code with frawk.
$ printf '{ if (NR <= 5) print( length    ($0  )  )  }' | gawk --pretty-print=/dev/stdout -f /dev/stdin | frawk -f /dev/stdin /etc/ssh/ssh_config 
42
13
0
60
0

$ printf '{ if (NR <= 5) print( length    ($0  )  )  }' | gawk --pretty-print=/dev/stdout -f /dev/stdin
{
        if (NR <= 5) {
                print (length($0))
        }
}

This approach would of course only work when the provided awk programs don't contain frawk specific features.

@ezrosent
Copy link
Owner

Just so I'm following correctly, are there any new examples here other than "spaces between function name and args" and the semicolon issues you pointed out in #49? If not, I may file a separate issue for the first issue and close this one.

I'd really prefer to fix frawk's parsing rather than add a dependency on a separate tool, to say nothing of potential licensing issues of including something like gawk with frawk. I do understand that frawk has parsing bugs, though, and I really appreciate you pointing them out and filing these issues.

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

2 participants