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

Generate identical atn simulation output across targets to compare parsing functionality #3957

Merged
merged 42 commits into from
Nov 19, 2022

Conversation

parrt
Copy link
Member

@parrt parrt commented Nov 11, 2022

Per #3814 trying to debug ATN sim. For ref, previous thoughts: #3817

@parrt
Copy link
Member Author

parrt commented Nov 12, 2022

@kaby76 @ericvergnaud @KvanTTT getting somewhere on this. Added some basic infrastructure and a command line tool.

@parrt
Copy link
Member Author

parrt commented Nov 12, 2022

Run this so we get right jars before trying this script:

cd ANTLR-ROOT-DIR
mvn install -DskipTests=true
cd runtime-tests 
mvn install -DskipTests=true # yes do it again

Run the script from runtime-tests dir with

../scripts/traceatn.sh /tmp/JSON.g4 json -target Go /tmp/foo.json

or whatever your test grammar, start rule, target, test input are.

Here is traceatn.sh:

export ANTLRJAR=/Users/parrt/.m2/repository/org/antlr/antlr4/4.11.2-SNAPSHOT/antlr4-4.11.2-SNAPSHOT-complete.jar
export TESTJAR=/Users/parrt/.m2/repository/org/antlr/antlr4-runtime-testsuite/4.11.2-SNAPSHOT/antlr4-runtime-testsuite-4.11.2-SNAPSHOT-tests.jar
export JUPITER=/Users/parrt/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.9.0/junit-jupiter-api-5.9.0.jar
export OPENTEST=/Users/parrt/.m2/repository/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar
java -classpath $ANTLRJAR:$TESTJAR:$JUPITER:$OPENTEST org.antlr.v4.test.runtime.TraceATN $@

@parrt parrt added the comp:doc label Nov 12, 2022
@kaby76
Copy link
Contributor

kaby76 commented Nov 13, 2022

Besides the hardwired user info in traceatn.sh, ':' path separators, it works fine on Windows for Java, but get no output for anything else (I have all targets installed).

@kaby76
Copy link
Contributor

kaby76 commented Nov 13, 2022

$ cat ../scripts/traceatn.sh
# Run this so we get right jars before trying this script:
#  cd ANTLR-ROOT-DIR
#  mvn install -DskipTests=true
#  cd runtime-tests
#  mvn install jar:test-jar -DskipTests=true
#
# Run script with
#
#  traceatn.sh /tmp/JSON.g4 json /tmp/foo.json

USERPROFILE=c:/Users/Kenne
export ANTLRJAR=$USERPROFILE/.m2/repository/org/antlr/antlr4/4.11.2-SNAPSHOT/antlr4-4.11.2-SNAPSHOT-complete.jar
export TESTJAR=$USERPROFILE/.m2/repository/org/antlr/antlr4-runtime-testsuite/4.11.2-SNAPSHOT/antlr4-runtime-testsuite-4.11.2-SNAPSHOT-tests.jar
export JUPITER=$USERPROFILE/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.9.0/junit-jupiter-api-5.9.0.jar
export OPENTEST=$USERPROFILE/.m2/repository/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar
java -classpath $ANTLRJAR\;$TESTJAR\;$JUPITER\;$OPENTEST org.antlr.v4.test.runtime.TraceATN $@

Kenne@DESKTOP-DL44R7B MINGW64 ~/issue-3814/antlr4/runtime-testsuite

$ bash ../scripts/traceatn.sh ~/big-index/grammars-v4/abnf/Abnf.g4 rulelist -target CSharp ~/big-index/grammars-v4/abnf/examples/postal.abnf


Kenne@DESKTOP-DL44R7B MINGW64 ~/issue-3814/antlr4/runtime-testsuite

Note: dotnet is the "go to" build tool for CSharp. MSBuild is not. In fact, I call "dotnet msbuild" if I want that explicitly. "dotnet" has tool packages that can be easily installed and works without any environment variable changes. That's why "Trash" package the apps as dotnet tools.

All the other target tools are on PATH.

For Cpp:

$ bash ../scripts/traceatn.sh ~/big-index/grammars-v4/abnf/Abnf.g4 rulelist -target Cpp ~/big-index/grammars-v4/abnf/examples/postal.
abnf
java.lang.Exception: command "MSBuild antlr4cpp-vs2022.vcxproj /p:configuration=Release DLL /p:platform=x64"
  in C:\msys64\home\Kenne\issue-3814\antlr4\runtime\Cpp\runtime failed:
  can't build c++ ANTLR runtime using MSBuild
        at org.antlr.v4.test.runtime.RuntimeRunner.runCommand(RuntimeRunner.java:334)
        at org.antlr.v4.test.runtime.cpp.CppRunner.initRuntime(CppRunner.java:107)
        at org.antlr.v4.test.runtime.RuntimeRunner.initAntlrRuntimeIfRequired(RuntimeRunner.java:272)
        at org.antlr.v4.test.runtime.RuntimeRunner.run(RuntimeRunner.java:181)
        at org.antlr.v4.test.runtime.TraceATN.test(TraceATN.java:164)
        at org.antlr.v4.test.runtime.TraceATN.execParse(TraceATN.java:217)
        at org.antlr.v4.test.runtime.TraceATN.main(TraceATN.java:230)
Caused by: java.io.IOException: Cannot run program "MSBuild" (in directory "C:\msys64\home\Kenne\issue-3814\antlr4\runtime\Cpp\runtime"): CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
        at org.antlr.v4.test.runtime.Processor.start(Processor.java:52)
        at org.antlr.v4.test.runtime.Processor.run(Processor.java:29)
        at org.antlr.v4.test.runtime.RuntimeRunner.runCommand(RuntimeRunner.java:327)
        ... 6 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessImpl.create(Native Method)
        at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:494)
        at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:159)
        at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
        ... 10 more
State: Compile; java.lang.Exception: C++ ANTLR runtime is not initialized

@parrt
Copy link
Member Author

parrt commented Nov 13, 2022

Note: dotnet is the "go to" build tool for CSharp. MSBuild is not. In fact, I call "dotnet msbuild" if I want that explicitly. "dotnet" has tool packages that can be easily installed and works without any environment variable changes.

Yeah, that must be something that's baked into the test rig. If you turn on WATCH_COMMANDS_EXEC in RuntimeRunner, you'll see the sequence of command during testing. Happy to replace as you suggest.

…ava target.

Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
…et to get ATN output to stdout.

Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
…sting.

Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
Signed-off-by: Terence Parr <parrt@antlr.org>
2. Pass RunOptions to all the runners during init
3. Make C++ set a cmake file if traceATN is set.

Signed-off-by: Terence Parr <parrt@antlr.org>
@parrt
Copy link
Member Author

parrt commented Nov 19, 2022

Ok, and now C# looks like Java! @ericvergnaud 19344b8

@parrt
Copy link
Member Author

parrt commented Nov 19, 2022

@marcospassos Could you update PHP target to reflect one of this commits so PHP ATN simulation trace output is same so we can compare parsing behavior? Not required but could be useful to you. Found issues with Go and JS for example :)

Here is C# commit: 19344b8

Signed-off-by: Terence Parr <parrt@antlr.org>
@parrt
Copy link
Member Author

parrt commented Nov 19, 2022

I can't get Swift to build so I can't update that target. I'm out of gas on this so I'll merge if tests pass, leaving Swift/PHP to update if somebody wants to help.

@parrt parrt changed the title Compare atn sim output across targets Generate identical atn simulation output across targets to compare parsing functionality Nov 19, 2022
@marcospassos
Copy link
Contributor

@marcospassos Could you update PHP target to reflect one of this commits so PHP ATN simulation trace output is same so we can compare parsing behavior? Not required but could be useful to you. Found issues with Go and JS for example :)

Here is C# commit: 19344b8

Sure!

@parrt parrt marked this pull request as ready for review November 19, 2022 20:41
@parrt parrt merged commit fe38615 into antlr:dev Nov 19, 2022
@parrt parrt deleted the compare-ATN-sim-output branch November 19, 2022 20:41
@kaby76
Copy link
Contributor

kaby76 commented Dec 17, 2022

Trying to use this in comparing CSharp vs PHP.

In my CSharp driver, I set parser.Trace = true;. Works great. Output goes to stdout, one line at a time per entry.

$ ./bin/Debug/net6.0/Test.exe ../examples/access.aql
enter   arangodb_query, LT(1)=RETURN
enter   data_query, LT(1)=RETURN
enter   data_access_query, LT(1)=RETURN
enter   return_expr, LT(1)=RETURN
consume [@0,0:5='RETURN',<9>,1:0] rule return_expr
enter   expr, LT(1)=1
enter   literal, LT(1)=1
enter   numeric_literal, LT(1)=1
consume [@2,7:7='1',<86>,1:7] rule numeric_literal
exit    numeric_literal, LT(1)=<EOF>
exit    literal, LT(1)=<EOF>
exit    expr, LT(1)=<EOF>
exit    return_expr, LT(1)=<EOF>
exit    data_access_query, LT(1)=<EOF>
exit    data_query, LT(1)=<EOF>
consume [@4,9:8='<EOF>',<-1>,2:0] rule arangodb_query
exit    arangodb_query, LT(1)=<EOF>
CSharp 0 ../examples/access.aql success 0.0505942
Total Time: 0.0921106

In my PHP driver, I set $parser.setTrace(true);. The trace output for PHP has zero new lines separating each trace entry. Not sure where it goes to, but that's moot.

$ php -d memory_limit=2G Test.php -file ../examples/access.aql
enter   arangodb_query, LT(1)=RETURNenter   data_query, LT(1)=RETURNenter   data_access_query, LT(1)=RETURNenter   return_expr, LT(1)=RETURNconsume [@0,0:5='RETURN',<9>,1:0] rule return_exprenter   expr, LT(1)=1enter   literal, LT(1)=1enter   numeric_literal, LT(1)=1consume [@2,7:7='1',<86>,1:7] rule numeric_literalexit    numeric_literal, LT(1)=<EOF>exit    literal, LT(1)=<EOF>exit    expr, LT(1)=<EOF>exit    return_expr, LT(1)=<EOF>exit    data_access_query, LT(1)=<EOF>exit    data_query, LT(1)=<EOF>consume [@4,9:8='<EOF>',<-1>,2:0] rule arangodb_queryexit    arangodb_query, LT(1)=<EOF>

I would expect the trace functionality to have the same semantics. Basically the PHP trace is unusable at this moment in dev branch of https://github.com/antlr/antlr-php-runtime. I can sed -E the output, but doing so isn't right.

@kaby76
Copy link
Contributor

kaby76 commented Dec 17, 2022

Well, it seems that the "trace" that is activated by parser.Trace = true; and $parser.setTrace(true); settings is actually for a parser visitor tracer. CSharp:

public virtual void EnterEveryRule(ParserRuleContext ctx)
Go: https://github.com/antlr/antlr-php-runtime/blob/ac71eb377c816c886b674a7ef805b54ca881d96c/src/ParserTraceListener.php#L20.

This code is pretty old, and has nothing to do with this PR. But it looks like nobody tested the PHP target. echo in PHP is a write without a newline character. Will submit an Issue in the PHP runtime repo.

srowen pushed a commit to apache/spark that referenced this pull request Sep 26, 2023
### What changes were proposed in this pull request?
This pr is aims upgrade `antlr4` from 4.9.3 to 4.13.1

### Why are the changes needed?
After 4.10, antlr4 is using Java 11 for the source code and the compiled .class files for the ANTLR tool. There are some bug fix and Improvements after 4.9.3:
- antlr/antlr4#3399
- antlr/antlr4#1105
- antlr/antlr4#2788
- antlr/antlr4#3957
- antlr/antlr4#4394

The full release notes as follows:

- https://github.com/antlr/antlr4/releases/tag/4.13.1
- https://github.com/antlr/antlr4/releases/tag/4.13.0
- https://github.com/antlr/antlr4/releases/tag/4.12.0
- https://github.com/antlr/antlr4/releases/tag/4.11.1
- https://github.com/antlr/antlr4/releases/tag/4.11.0
- https://github.com/antlr/antlr4/releases/tag/4.10.1
- https://github.com/antlr/antlr4/releases/tag/4.10

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Pass GitHub Actions

### Was this patch authored or co-authored using generative AI tooling?
No

Closes #43075 from LuciferYang/antlr4-4131.

Authored-by: yangjie01 <yangjie01@baidu.com>
Signed-off-by: Sean Owen <srowen@gmail.com>
doruchiulan pushed a commit to BlueprintTechnologies/spark-lhm that referenced this pull request Feb 2, 2024
### What changes were proposed in this pull request?
This pr is aims upgrade `antlr4` from 4.9.3 to 4.13.1

### Why are the changes needed?
After 4.10, antlr4 is using Java 11 for the source code and the compiled .class files for the ANTLR tool. There are some bug fix and Improvements after 4.9.3:
- antlr/antlr4#3399
- antlr/antlr4#1105
- antlr/antlr4#2788
- antlr/antlr4#3957
- antlr/antlr4#4394

The full release notes as follows:

- https://github.com/antlr/antlr4/releases/tag/4.13.1
- https://github.com/antlr/antlr4/releases/tag/4.13.0
- https://github.com/antlr/antlr4/releases/tag/4.12.0
- https://github.com/antlr/antlr4/releases/tag/4.11.1
- https://github.com/antlr/antlr4/releases/tag/4.11.0
- https://github.com/antlr/antlr4/releases/tag/4.10.1
- https://github.com/antlr/antlr4/releases/tag/4.10

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Pass GitHub Actions

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#43075 from LuciferYang/antlr4-4131.

Authored-by: yangjie01 <yangjie01@baidu.com>
Signed-off-by: Sean Owen <srowen@gmail.com>
(cherry picked from commit 13cd291)
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.

4 participants