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

[golang target] Specific grammar generates code that fails to compile #1459

Closed
2 tasks done
ctopher78 opened this issue Dec 7, 2016 · 3 comments
Closed
2 tasks done

Comments

@ctopher78
Copy link

ctopher78 commented Dec 7, 2016

Before submitting an issue to ANTLR, please check off these boxes:

Please include information about the expected behavior, actual behavior, and the smallest grammar or code that reproduces the behavior. If appropriate, please indicate the code generation targets such as Java, C#, ... Pointers into offending code regions are also very welcome.

Expected behavior

Go code that successfully compiles.

Actual behavior

When trying to compile Golang code from a grammar that has a mixture of non-greedy subrules and two or more "?" wildcard characters, the code fails to compile. I'm using this grammar successfully with a Python2 target.

$ go run test_parse.go test.txt 
# _/Users/ccrook/git-repos/config_parser/antlr_playground/go_parser
go_parser/test_parser.go:337: no new variables on left side of :=

Steps to reproduce the behavior

Directory structure:

├── Test.g4
├── go_parser
│   ├── Test.tokens
│   ├── TestLexer.tokens
│   ├── test_base_listener.go
│   ├── test_lexer.go
│   ├── test_listener.go
│   └── test_parser.go
├── test.txt
├── test_parse.go

Grammar:
Notice multiple "?" wildcard chars in the foo parser rule.

grammar Test;

init
  : .*? (foo .*?)+ ;

foo
  : 'A' 'B'? 'C'?
  ;

ANLTR4 command:
Note: I'm pointing to a 4.6 version of the code (compiled from source 12/6).

java -jar ../lib/antlr4-4.6-SNAPSHOT.jar -Dlanguage=Go -o go_parser Test.g4

Attempt to complile/run go code:

$ go run test_parse.go test.txt 
# _/Users/ccrook/git-repos/config_parser/antlr_playground/go_parser
go_parser/test_parser.go:337: no new variables on left side of :=

The compiled go code attempts to declare the "la_" variable multiple times:

	la_ := p.GetInterpreter().AdaptivePredict(p.GetTokenStream(), 3, p.GetParserRuleContext())

	if la_ == 1 {
		{
			p.SetState(22)
			p.Match(TestParserT__1)
		}

	}
	p.SetState(26)
	p.GetErrorHandler().Sync(p)
	la_ := p.GetInterpreter().AdaptivePredict(p.GetTokenStream(), 4, p.GetParserRuleContext())

	if la_ == 1 {
		{
			p.SetState(25)
			p.Match(TestParserT__2)
		}

	}

	return localctx
}

@ctopher78
Copy link
Author

@pboyer - First off, thanks for all your hard work on the Golang target. I think my parsing use-case hits on some uncommon grammar edge-cases. But, I'm really excited to use ANTLR4 to solve some problems my team is facing.

I'm currently testing against ANLTR4 4.6 code, which includes fixes #1423 & #1436

@pboyer
Copy link
Contributor

pboyer commented Dec 8, 2016

@syn-ful Don't worry! Thanks for the help in finding the bugs. This looks like it should fix the problem: pboyer#91

Would you mind trying it out? Otherwise I'll do it tomorrow sometime.

@pboyer pboyer mentioned this issue Dec 8, 2016
parrt added a commit that referenced this issue Dec 8, 2016
@ctopher78
Copy link
Author

Thanks @pboyer - Everything is working as expected now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants