antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Grammar Name Conflict Golang with SPARQL issue

Open saucesaft opened this issue 2 years ago • 1 comments

Hi! So im trying to build a simple sparql parser for golang. Got the grammar for it from here: https://github.com/antlr/grammars-v4/tree/master/sparql

Then i ran this command to generate the code: java -jar antlr-4.10.1-complete.jar -Dlanguage=Go -o parser Sparql.g4

Now, after i included it into a Go program, without even using it, it errors out with this:

parser/sparql_parser.go:9806:9: cannot use s (variable of type *RdfLiteralContext) as type antlr.RuleContext in return statement:
	*RdfLiteralContext does not implement antlr.RuleContext (wrong type for String method)
		have String() IStringContext
		want String([]string, antlr.RuleContext) string
parser/sparql_parser.go:9829:13: cannot use NewRdfLiteralContext(p, p.GetParserRuleContext(), p.GetState()) (value of type *RdfLiteralContext) as type IRdfLiteralContext in assignment:
	*RdfLiteralContext does not implement IRdfLiteralContext (wrong type for String method)
		have String() IStringContext
		want String([]string, antlr.RuleContext) string

From what i've seen in another issue, i think this might be related to some grammar name conflicts with the code generation, but i dont know how to fix it. If someone recognizes the problem, i'll be glad to make the changes myself and open a pull request.

Thanks :)

saucesaft avatar Jun 21 '22 22:06 saucesaft

Correct. This is the result of a "symbol conflict".

After looking over the generated files, and finding nothing obvious, I decided to check for the problem using automated means. I created a script (see below) that creates a new grammar with all parser rule symbols with a trailing underscore. It renames a symbol back to the original name without the underscore, and test how it builds.

The problem is "string". A workaround is to rename it to something else, like "string_". This needs to be added to the escaping code in the Antlr tool for Go. @KvanTTT

#!/bin/sh

names=`trparse Sparql.g4 | trxgrep ' //parserRuleSpec/RULE_REF/text()' | sort -u | dos2unix | grep -v -E -e '_$' | grep -v -E -e '^query$'`
newnames=""
for i in $names
do
	newnames="$newnames"$i,$i"_;"
done
newnames="${newnames::-1}"
trparse Sparql.g4 | trrename -r "$newnames" | trsponge -c
for i in $names
do
	fixed=$i'_'
	echo Testing $fixed '=>' $i
	rm -rf testit
	mkdir testit
	pushd testit > /dev/null 2>&1
	cp ../Sparql.g4 .
	trparse Sparql.g4 | trrename -r "$fixed,$i" | trsponge -c > /dev/null 2>&1
	cp -r ../examples .
	cp ../pom.xml .
	trgen -t Go > /dev/null 2>&1
	cd Generated
	make > /dev/null 2>&1
	if [[ "$?" != "0" ]]
	then
		echo "==========================="
		echo $i did not work.
	fi
	cd ..
	rm -rf Generated
	popd > /dev/null 2>&1
	rm -rf testit
done
Testing additiveExpression_ => additiveExpression
Testing argList_ => argList
Testing askQuery_ => askQuery
Testing baseDecl_ => baseDecl
Testing blankNode_ => blankNode
Testing blankNodePropertyList_ => blankNodePropertyList
Testing booleanLiteral_ => booleanLiteral
Testing brackettedExpression_ => brackettedExpression
Testing builtInCall_ => builtInCall
Testing collection_ => collection
Testing conditionalAndExpression_ => conditionalAndExpression
Testing conditionalOrExpression_ => conditionalOrExpression
Testing constraint_ => constraint
Testing constructQuery_ => constructQuery
Testing constructTemplate_ => constructTemplate
Testing constructTriples_ => constructTriples
Testing datasetClause_ => datasetClause
Testing defaultGraphClause_ => defaultGraphClause
Testing describeQuery_ => describeQuery
Testing expression_ => expression
Testing functionCall_ => functionCall
Testing graphGraphPattern_ => graphGraphPattern
Testing graphNode_ => graphNode
Testing graphPatternNotTriples_ => graphPatternNotTriples
Testing graphTerm_ => graphTerm
Testing groupGraphPattern_ => groupGraphPattern
Testing groupOrUnionGraphPattern_ => groupOrUnionGraphPattern
Testing iriRef_ => iriRef
Testing iriRefOrFunction_ => iriRefOrFunction
Testing limitClause_ => limitClause
Testing limitOffsetClauses_ => limitOffsetClauses
Testing multiplicativeExpression_ => multiplicativeExpression
Testing namedGraphClause_ => namedGraphClause
Testing numericExpression_ => numericExpression
Testing numericLiteral_ => numericLiteral
Testing numericLiteralNegative_ => numericLiteralNegative
Testing numericLiteralPositive_ => numericLiteralPositive
Testing numericLiteralUnsigned_ => numericLiteralUnsigned
Testing objectList_ => objectList
Testing offsetClause_ => offsetClause
Testing optionalGraphPattern_ => optionalGraphPattern
Testing orderClause_ => orderClause
Testing orderCondition_ => orderCondition
Testing prefixDecl_ => prefixDecl
Testing prefixedName_ => prefixedName
Testing primaryExpression_ => primaryExpression
Testing prologue_ => prologue
Testing propertyList_ => propertyList
Testing propertyListNotEmpty_ => propertyListNotEmpty
Testing rdfLiteral_ => rdfLiteral
Testing regexExpression_ => regexExpression
Testing relationalExpression_ => relationalExpression
Testing selectQuery_ => selectQuery
Testing solutionModifier_ => solutionModifier
Testing sourceSelector_ => sourceSelector
Testing string_ => string
===========================
string did not work.
Testing triplesBlock_ => triplesBlock
Testing triplesNode_ => triplesNode
Testing triplesSameSubject_ => triplesSameSubject
Testing unaryExpression_ => unaryExpression
Testing valueLogical_ => valueLogical
Testing varOrIRIref_ => varOrIRIref
Testing varOrTerm_ => varOrTerm
Testing verb_ => verb
Testing whereClause_ => whereClause

kaby76 avatar Jun 23 '22 02:06 kaby76

This issue can now be closed.

jimidle avatar Aug 22 '22 03:08 jimidle

Checking into this now...

kaby76 avatar Aug 22 '22 11:08 kaby76

No, it doesn't seem fixed. But, maybe I'm not building the right version of https://github.com/antlr/antlr4/tree/dev.

First, I took the "dev" branch and built the tool complete.jar. Then, I created a Go workspace with that antlr4/runtime/Go/antlr/ directory, and adjusted my makefiles in my trgen-generated driver to make sure the "dev" complete.jar file is used. The build output is:

export GO111MODULE=on; go get github.com/antlr/antlr4/runtime/Go/[email protected]
java -jar C:/Users/Kenne/Documents/GitHub/antlr4-upstream-dev/tool/target/antlr4-4.10.2-SNAPSHOT-complete.jar -encoding utf-8 -Dlanguage=Go -o parser -lib parser -package parser  parser/Sparql.g4
export GO111MODULE=on; go build Test.go
# example.com/myparser/parser
parser\sparql_parser.go:9805:9: cannot use s (variable of type *RdfLiteralContext) as type antlr.RuleContext in return statement:
        *RdfLiteralContext does not implement antlr.RuleContext (wrong type for String method)
                have String() IStringContext
                want String([]string, antlr.RuleContext) string
parser\sparql_parser.go:9828:13: cannot use NewRdfLiteralContext(p, p.GetParserRuleContext(), p.GetState()) (value of type *RdfLiteralContext) as type IRdfLiteralContext in assignment:
        *RdfLiteralContext does not implement IRdfLiteralContext (wrong type for String method)
                have String() IStringContext
                want String([]string, antlr.RuleContext) string
mingw32-make: *** [makefile:16: program] Error 2

Note, I had to do a "go get golang.org/x/exp/slices" ni antlr/ because there is a dependency on the "slices" module.

I made sure to use the "right" grammar in testing, the previous commit: https://github.com/antlr/grammars-v4/tree/72305302b8b82ec35a72593e2fd0c13a841cb602/sparql

I had to fix the grammar to work around this problem.

kaby76 avatar Aug 22 '22 11:08 kaby76

You have to wait for the PR to come in to dev

On Mon, Aug 22, 2022 at 19:54 Ken Domino @.***> wrote:

No, it doesn't seem fixed. But, maybe I'm not building the right version of https://github.com/antlr/antlr4/tree/dev.

First, I took the "dev" branch and built the tool complete.jar. Then, I created a Go workspace with that antlr4/runtime/Go/antlr/ directory, and adjusted my makefiles in my trgen-generated driver to make sure the "dev" complete.jar file is used. The build output is:

export GO111MODULE=on; go get @.*** java -jar C:/Users/Kenne/Documents/GitHub/antlr4-upstream-dev/tool/target/antlr4-4.10.2-SNAPSHOT-complete.jar -encoding utf-8 -Dlanguage=Go -o parser -lib parser -package parser parser/Sparql.g4 export GO111MODULE=on; go build Test.go

example.com/myparser/parser

parser\sparql_parser.go:9805:9: cannot use s (variable of type *RdfLiteralContext) as type antlr.RuleContext in return statement: *RdfLiteralContext does not implement antlr.RuleContext (wrong type for String method) have String() IStringContext want String([]string, antlr.RuleContext) string parser\sparql_parser.go:9828:13: cannot use NewRdfLiteralContext(p, p.GetParserRuleContext(), p.GetState()) (value of type *RdfLiteralContext) as type IRdfLiteralContext in assignment: *RdfLiteralContext does not implement IRdfLiteralContext (wrong type for String method) have String() IStringContext want String([]string, antlr.RuleContext) string mingw32-make: *** [makefile:16: program] Error 2

Note, I had to do a "go get golang.org/x/exp/slices" ni antlr/ because there is a dependency on the "slices" module.

I made sure to use the "right" grammar in testing, the previous commit: https://github.com/antlr/grammars-v4/tree/72305302b8b82ec35a72593e2fd0c13a841cb602/sparql

I had to fix the grammar to work around this problem.

— Reply to this email directly, view it on GitHub https://github.com/antlr/antlr4/issues/3758#issuecomment-1222250118, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ7TMHA2PBVGZ7Q2LHC7ODV2NS7XANCNFSM5ZOFG7ZQ . You are receiving this because you commented.Message ID: @.***>

jimidle avatar Aug 22 '22 12:08 jimidle

@jimidle Yes, your fix works (git clone https://github.com/jimidle/antlr4.git; cd antlr4; git checkout feature/3758; ..., but with the caveat that I need to "cd antlr4/runtime/Go/antlr4; go get golang.org/x/exp/slices").

kaby76 avatar Aug 22 '22 13:08 kaby76

Cool. Thanks for the verify.

On Mon, Aug 22, 2022 at 21:00 Ken Domino @.***> wrote:

@jimidle https://github.com/jimidle Yes, your fix works (git clone https://github.com/jimidle/antlr4.git; cd antlr4; git checkout feature/3758; ..., but with the caveat that I need to "cd antlr4/runtime/Go/antlr4; go get golang.org/x/exp/slices").

— Reply to this email directly, view it on GitHub https://github.com/antlr/antlr4/issues/3758#issuecomment-1222326262, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ7TMF5XHKCQPBCN23CZF3V2N2XDANCNFSM5ZOFG7ZQ . You are receiving this because you were mentioned.Message ID: @.***>

jimidle avatar Aug 22 '22 13:08 jimidle

go.mod is updated and go.sum is added to the repo. We can close this when the PR is merged.

@parrt There are I think 3 outstanding PRs to fix go runtime bugs. I could merge them but do not have permissions (which also means I cannot get a free Github copilot license ;)

jimidle avatar Aug 23 '22 02:08 jimidle

Merged. Closing.

parrt avatar Aug 23 '22 04:08 parrt