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

Add an iterative version of the ParseTreeWalker #1231

Merged
merged 5 commits into from
Nov 25, 2016
Merged

Add an iterative version of the ParseTreeWalker #1231

merged 5 commits into from
Nov 25, 2016

Conversation

twz123
Copy link
Contributor

@twz123 twz123 commented Jul 10, 2016

I hit a StackOverflowError while traversing deeply nested trees using the standard ParseTreeWalker, so I decided to implement an iterative version of it using a linked data structure instead of recursion during traversal.


@Override
public void walk(final ParseTreeListener listener, final ParseTree t) {
for (WalkerNode<?> currentNode = createNode(null, t, 0); currentNode != null;) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 This implementation requires at least one allocated object for every node in the tree. You could avoid all these allocations by instead using two stacks:

Deque<SyntaxTree> treeStack = new ArrayDeque<SyntaxTree>();
IntegerStack currentIndex = new IntegerStack();

Where the node you are currently visiting is treeStack.peek() and its index in the parent is currentIndex.peek().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I thought if I'd use a LinkedList for the stack, then I'd have object allocation anyways, just hidden inside the LinkedList. But I see that ArrayDeque does a better job here.

@@ -0,0 +1,151 @@
/*
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that copyright wrong? Neither exists this file since 2012 nor was it written by Sam + Ter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The year is particularly interesting. It turns out keeping the year constant and using the year when the code base started provides the longest and strongest copyright protections. If you want to see some of the details on this, take a look at the long and probably very boring comments in DotNetAnalyzers/StyleCopAnalyzers#1661.

The use of mine and Terence's names has to do with copyright assignment, which is (to the best of my knowledge) common practice in a number of circles. There are three ways we work to ensure the original author of patches remains recognized for the life of the project:

  1. The contributors.txt file, which is an essential and core part of this project. It's not distributed, but the distribution links back to it.
  2. The Git commit history.
  3. The pull requests and issues filed by everyone.

📝 I am not a lawyer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you certainly have your reasons, but to me it feels wrong. The header says the file exists since 2012, which is plain wrong. If you always use the same year then you wouldn't need individual copyrights in each file, but just can have a central COPYRIGHT file and save the work to maintain a per-file copyright. Anway, even though I don't agree with your reasoning, I certainly don't want to open a big discussion about this here. Thanks for explaining.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with everything here. Just tell me what, in your opinion, should be the copyright header, or if I can remove that completely? I must admit that I just copy/pasted the copyright notice from another file, trying to adhere to the code standards of this project, and not paying much attention to the header's content.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note I'm going to blast all headers to have consistent BSD license. I feel that the git history is best way to determine authorship. Adding a name to the license comment is very misleading/ambiguous sometimes. Did that person fix a typo or write most of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I wipe out the header from this file, then?

@parrt
Copy link
Member

parrt commented Nov 19, 2016

@twz123 we need a sig in the contributors.txt file :)

@parrt
Copy link
Member

parrt commented Nov 19, 2016

@ericvergnaud What is best way to "broadcast" to target authors that a new bit of functionality has been added to one target?

@ericvergnaud
Copy link
Contributor

Have them subscribe to all changes (which is what I do)

@mike-lischke
Copy link
Member

@parrt Isn't the ANTLR mailinglist supposed to serve as a central communication means between ANTLR developers? Even though there are many user questions the main purpose should be the discussions/exchange about the development of ANTLR, IMO. So, you could send out a notification mail about the addition and have target authors reply with their time estimates for implementation (or, if there is no reply for particular target you know nobody is taking care) and also report the completion.

Alternatively, you could add a comment with all the known target authors in Github when you merge a new feature, so they will all get a mail about this. Better than to subscribe to all changes.

@mike-lischke
Copy link
Member

@parrt I believe it would be very helpful if each change/addition of runtime code must also name an existing testcase (or a new one, if there is none yet) in the PR, which tests the changed/added code. Target authors then can use the same test for their implementations.

@parrt
Copy link
Member

parrt commented Nov 21, 2016

@ericvergnaud @mike-lischke how about I create a team with target folks and then ref the team in comments that they should notice?

@parrt
Copy link
Member

parrt commented Nov 21, 2016

@twz123 we need a sig in the contributors.txt file :)

@mike-lischke
Copy link
Member

@parrt Yes, a Github team could work as well.

@ericvergnaud
Copy link
Contributor

let’s try that and see how it goes

Le 22 nov. 2016 à 02:47, Mike Lischke notifications@github.com a écrit :

@parrt https://github.com/parrt Yes, a Github team could work as well.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #1231 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/ADLYJGLG_Zun-B4EL8oUtdFDGND099HJks5rAecrgaJpZM4JI1xt.

@twz123
Copy link
Contributor Author

twz123 commented Nov 25, 2016

@parrt Added myself to contributors at the date of my first commit. If I should better do it using the current date, just tell me. Would you like me to rebase this thing?

@parrt
Copy link
Member

parrt commented Nov 25, 2016

Note to @antlr/antlr-targets that a new tree walker is enter the runtime, if you would like to implement it.

@parrt parrt added this to the 4.6 milestone Nov 25, 2016
@parrt parrt merged commit ee614db into antlr:master Nov 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants