GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Make binary operators left-recursive by oowekyala · Pull Request #1979 · pmd/pmd

Conversation

@oowekyala

@oowekyala oowekyala commented Aug 15, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

So this should fix #1661 definitively

The structure of infix expressions is now always the same: a left-hand side, an operator, a right hand side. Since having different node types for each of those adds no information that the operator constant doesn't already provide, I think it's useful to merge all those nodes (there's 10) into a single one. This decreases the size of the API, and is for example what Eclipse JDT does

I also experimented with generating nice tree images for our javadocs. For example:

Capture du 2019-08-15 02-51-07

Unfortunately rendering the images is not really portable for now, as the build machine needs a LaTeX distribution. So I prerendered them and committed them here.

Copy of the discussion on left-recursiveness

What to do about #1661 (operator nodes) ? Sensible solution would be to make AdditiveExpression be parsed completely left-recursively, but then, it looks weird that all other binary expressions are not left-recursive…

The problem also arises for operators &, |, ^, which may be boolean non-shortcut expressions or numeric bitwise expressions

Arguments for left recursiveness:

  • Preserve all intermediary type resolution results, because of numeric promotion -> the whole point of [java] RFC: new layer of abstraction atop PrimaryExpressions #497
  • AST is JLS-like, easier to document (we don’t have to explain “sometimes it’s left-recursive and sometimes not”)
    ((1 + 4) + returnString()) = “14” “5”
    ((1 + 4) + 4) == (1 + 4 + 4)
  • Easier mapping of type resolution algorithms from the JLS
  • Stronger contract for binary expressions: all can present the same interface:
getLhs() : ASTExpression
getRhs() : ASTExpression
getOperator() : BinaryOp
  • Classpath independent

Arguments against:

  • Not super intuitive
  • Breaks away from previous AST structure, needs to be relearned
  • Needs more tooling for some use cases:

Eg how to catch a string concat statement like a = … + a + …; (use case taken from #1932's AvoidConcatInLoop)

  • In Java we can imagine the following method for binary expressions which yields all operands that are added with the same operator: operandStream(): NodeStream<ASTExpression>
    Eg for <a + b + c * d>, operandStream returns Stream.of(<a>, <b>, <c * d>). So you can do eg:
 additiveExpr.operandStream()
                   .filter(ASTVariableAccess.class)
                   .any(ref -> ref.getName().equals(“a”))
  • However in XPath it’s less trivial.. We’d have to provide access to the node stream with a function:
    • Old way, with flat additive expr:
AdditiveExpression[@Operator=”+”]/VariableReference[@Image = “a”]
  • New way:
AdditiveExpression[@Operator=”+”]
     [pmd-java:operandsOf(.)/self::VariableReference[@Image = “a”]]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
oowekyala added 5 commits August 14, 2019 21:53
@oowekyala
Make expressions left-recursive, merge them into a single node
7b9ccff
@oowekyala
Update images
1bd6d3d
@oowekyala
Remove useless base class
9369eb1
@oowekyala
Update image
df681ce
@oowekyala
Fix lookaheads
92e0b41
@oowekyala oowekyala added the in:ast About the AST structure or API, the parsing step label Aug 15, 2019
@oowekyala oowekyala added this to the 7.0.0 milestone Aug 15, 2019
oowekyala added 4 commits August 16, 2019 13:38
@oowekyala
Merge branch 'java-grammar' into grammar-left-recursive-ops
3e521d3
@oowekyala
Remove duplicated copyright
31cd079
@oowekyala
Remove image attribute of InfixExpression
d7f6855
@oowekyala
Merge branch 'java-grammar' into grammar-left-recursive-ops
8d47824
@oowekyala
oowekyala force-pushed the grammar-left-recursive-ops branch from a995701 to 8d47824 Compare September 25, 2019 21:53
@ghost

ghost commented Sep 25, 2019

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala
oowekyala merged commit 8d47824 into pmd:java-grammar Sep 27, 2019
@adangel

adangel commented Sep 28, 2019

Copy link
Copy Markdown
Member

I've added doc to https://github.com/pmd/pmd/wiki/Java_clean_changes#binary-operators-are-left-recursive - please update it, if I didn't get it right, thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala
oowekyala deleted the grammar-left-recursive-ops branch October 1, 2019 08:50
@oowekyala oowekyala mentioned this pull request Apr 25, 2020
[java] RFC: new layer of abstraction atop PrimaryExpressions #497
Closed
@adangel adangel mentioned this pull request Jan 23, 2023
PMD 7 Tracking Issue #3898
Closed
55 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

in:ast About the AST structure or API, the parsing step

Projects

None yet

Milestone

7.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@oowekyala @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.