stanfordnlp/CoreNLP

Compile error, 'tree' can't be resolved...can't figure out what's going on!

scasadei opened this issue · 11 comments

Hello, I have just cloned CoreNLP from github into the most recent Eclipse environment (2023-09), and I am getting the compilation error shown in the attached image (Java 20 compiler). I can't figure out what's going on! The first reference to tree is OK, the following ones can't be resolved, even though they are in the same compile scope! Same errors occurred in an older eclipse environment (2022-12) with the Java 19 compiler.
A very similar error occurs in class AnCoraPOSStats, in block:
for (File file : fileList) { }

Thanks for your help

Screenshot 2023-11-22 at 4 44 43 PM

Honestly I don't have the slightest idea. It looks like legal Java to me, but no one ever hired me to be a Java compiler.

What happens if you comment out a line such as 91, if(tree.yield().size() > maxLen) continue;

Maybe Eclipse is incorrectly thinking the loop always shortcuts there?

Or a different variable name? No idea if something weird happened where that variable name was used elsewhere and that is confusing Eclipse

Maybe Eclipse is incorrectly thinking the loop always shortcuts there?
Interesting hypothesis...I have commented the first few lines, which made the first error disappear (the other errors are still there).
On the other hand, changing the variable name from tree to tree2 didn't change anything

treeD_Screenshot 2023-11-22 at 8 47 19 PM treeC_Screenshot 2023-11-22 at 8 42 09 PM

And a similar problem occurs here:
file_Screenshot 2023-11-22 at 8 54 27 PM

What if you took the variable out of the loop and only did the operations once? Would it compile then? (Obviously the functionality would be broken)

If I use it exactly once, it goes away...
treeF_Screenshot 2023-11-22 at 10 02 27 PM

However, here is a way to make it fail even at the first usage:
treeG_Screenshot 2023-11-22 at 10 01 18 PM

Hmm, I had actually meant to remove the loop and test compiling with nothing there, but this might still be useful to know.

If memory serves, yield is actually now a keyword in future versions of Java. (Anything after 8 is "future" from our point of view.) Perhaps there is some effect going on here. Each of the examples you've pasted so far has that in the error.

In the case of the one where the line has yield().size(), would you try replacing that with taggedYield().size()? If that clears up some of the errors, I'll put a little effort into generalizing it, especially if you can help point out where the compiler is spazzing.

Incidentally, handling this error is kinda reinforcing my belief that pasting images is a lot less useful than pasting text - I wanted to copy/paste the line I wanted to change, but couldn't

Good point, I will include text from now on.
Tried to remove the whole loop, all the errors disappear.

Here is the text where the four errors occur (lines 95,98,101,104)

91 for (Tree tree : tb) {
92 if(tree.yield().size() > maxLen) continue;
93 ++numTrees;
94 if (printTrees) {
95 pw.println(tree.toString());

  } else if (flattenTrees) {
    pw.println(SentenceUtils.listToString(tree.yield()));
    
  } else if (printPOS) {
    pw.println(SentenceUtils.listToString(tree.preTerminalYield()));
  
  } else if (printTnT) {
    List<CoreLabel> yield = tree.taggedLabeledYield();
    for (CoreLabel label : yield) {
      pw.printf("%s\t%s%n", label.word(), label.tag());
    }
    pw.println();
  }      
}

And the same error occurs in the class AnCoraPOSStats, at line 51 (below),
there is yield in this instance. The two instances have in common that the variable
is declared with the colon syntax inside of for (....)

48 Tree t;
49 for (File file : fileList) {
50 Reader in =
51 new BufferedReader(new InputStreamReader(new FileInputStream(file), ANCORA_ENCODING));
TreeReader tr = trf.newTreeReader(in);

  // Tree reading will implicitly perform tree normalization for us
  while ((t = tr.readTree()) != null) {
    // Update tagger with this tree
    List<CoreLabel> yield = t.taggedLabeledYield();
    for (CoreLabel leafLabel : yield) {
      if (leafLabel.tag().equals(SpanishTreeNormalizer.MW_TAG))
        continue;

      unigramTagger.incrementCount(leafLabel.word(), leafLabel.tag());
    }
  }
}

}

What happens if you try this one

92 if(tree.yield().size() > maxLen) continue;

with getLeaves() instead of yield()? That one would be an easy fix if it's the yield keyword causing the problem, since the effect should be exactly the same.

Would you try compiling the dev branch? I removed two usages of the keyword yield from in those functions. If that improves the compilation, I can do the same in any other places you find a compile error.

The dev branch does not trigger the error, thanks for finding a solution!
There is still one error that pops up in file OSXAdapter:
private static com.apple.eawt.Application app can not be resolved,
I will open a new issue for it