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

Allow parsing of "cond && var=value" and similar expressions #170

Merged
merged 1 commit into from
Feb 17, 2023

Conversation

benhoyt
Copy link
Owner

@benhoyt benhoyt commented Feb 12, 2023

Expressions like "1 && x=1" aren't really valid (IMO), because assignments are lower-precedence than binary operators, but onetrueawk, Gawk, and mawk all support this for logical, match and comparison operators.

The other awks support this by using a yacc grammar which supports backtracking, and as Vitus13 said on reddit: "If there are two syntactically valid parsings and one is a semantic error, the error handling may resolve the ambiguity towards the valid parsing. In this case, you can only assign to L values, so trying to assign to (1&&x) doesn't make any sense."

In GoAWK, this requires a form of backtracking (I call it "partial backtracking" because it's not actually backing up the lexer). It works by parsing as (1&&x)=1 according to the operator precedence, then determining that you're trying to assign something that isn't an lvalue, then confirming that 1&&x is a binary expression, that the "x" part is an lvalue, and that the operator (&& in this case) is one that the other awks handle similarly for this case.

Also make the error message a bit clearer when you don't have an lvalue on the left hand side of an assignment, like "rand() = 1".

Fixes #166

Expressions like "1 && x=1" aren't really valid (IMO), because
assignments are lower-precedence than binary operators, but onetrueawk,
Gawk, and mawk all support this for logical, match and comparison
operators.

The other awks support this by using a yacc grammar which supports
backtracking, and as Vitus13 said on reddit: "If there are two
syntactically valid parsings and one is a semantic error, the error
handling may resolve the ambiguity towards the valid parsing. In this
case, you can only assign to L values, so trying to assign to (1&&x)
doesn't make any sense."

In GoAWK, this requires a form of backtracking (I call it "partial
backtracking" because it's not actually backing up the lexer). It works
by parsing as (1&&x)=1 according to the operator precedence, then
determining that you're trying to assign something that isn't an
lvalue, then confirming that 1&&x is a binary expression, that the "x"
part is an lvalue, and that the operator (&& in this case) is one that
the other awks handle similarly for this case.

Also make the error message a bit clearer when you don't have an lvalue
on the left hand side of an assignment, like "rand() = 1".

Fixes #166
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

Successfully merging this pull request may close these issues.

Error parsing "cond && var=value"
1 participant