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

ANTLR4 go-lang target #659

Closed
anidotnet opened this issue Jul 6, 2014 · 55 comments
Closed

ANTLR4 go-lang target #659

anidotnet opened this issue Jul 6, 2014 · 55 comments

Comments

@anidotnet
Copy link

Is there any plan or ongoing effort for a go-lang target?

@sharwell
Copy link
Member

sharwell commented Jul 6, 2014

Not to my knowledge. I looked into it briefly but abandoned the effort before I had anything working. It's safe to assume there will not be a Go target until the day someone donates a fully-working independent project implementing a Go target, similar to what happened with the C# and Python targets.

@anidotnet
Copy link
Author

Thanks. Out of curiosity, may I know what caused you to abandon the effort? Was there any specific hurdle to overcome? I am asking because if I ever try it I want to know what issues I can expect. Moreover, @sharwell I saw that you have created a go grammar for goworks, so can it be possible to create some kind of java to golang translator with it to do the heavy lifting?

@sharwell
Copy link
Member

sharwell commented Jul 8, 2014

I had some doubts about the future of Go. The port would be an enormous amount of work that I couldn't be sure would have long-term benefits, especially compared to some of the other targets which I can be involved with.

@markmcd
Copy link

markmcd commented Feb 25, 2015

I thought I'd add my support, just in case someone with the spare cycles comes here considering the project.

@wangjild
Copy link

@sharwell is there any other specification or guide for create new language target? except for
https://theantlrguy.atlassian.net/wiki/display/ANTLR3/How+to+build+an+ANTLR+code+generation+target

@pboyer
Copy link
Contributor

pboyer commented Jan 11, 2016

I'm currently working on this. I should have it open-sourced within the month. Not all of the tests pass yet, but I'm close. Let me know if you'd like to contribute.

@anidotnet
Copy link
Author

@pboyer Great to know! Sure I would like to help when my time permits. Currently busy with so much of daily job stuff, so could not commit anything.

@pboyer
Copy link
Contributor

pboyer commented Jan 13, 2016

Just so anyone knows who finds this discussion, this is the best place to find info on building a runtime target:

https://github.com/antlr/antlr4/blob/master/doc/creating-a-language-target.md

The awesome part about the system that @parrt @sharwell and @ericvergnaud have built is that the tests are actually generated from templates and executed across processes. This makes an implementor's life so much easier.

@willfaught
Copy link
Contributor

Is there a reference for the target template syntax?

@parrt
Copy link
Member

parrt commented Apr 28, 2016

Hiya Will. It's http://www.stringtemplate.org syntax

@willfaught
Copy link
Contributor

Thanks @parrt.


I see template functions that aren't used elsewhere in the template, like pythonTypeInitMap in the Python 3 template. Are those dead code, or are these functions called elsewhere?

@parrt
Copy link
Member

parrt commented Apr 28, 2016

Sometimes they are directly instanced from code not from other templates.

@willfaught
Copy link
Contributor

All the existing targets appear to have, and make heavy use of, inheritance, which seems to complicate things. Any guidance/tips on how those templates are structured around inheritance and how that structure would change for a target without inheritance (like C)?

@willfaught
Copy link
Contributor

I was thinking of converting the C# target, as it's typed like Go, but the Python ones seem simpler and Python has an instance-as-parameter concept similar to Go.

@parrt
Copy link
Member

parrt commented Apr 28, 2016

Honestly, it's hopeless so I'd fake inheritance if you can. The data inheritance wouldn't be bad but the virtual methods might be. Maybe fake inheritance with a "delegation" pointer?

@willfaught
Copy link
Contributor

Any way to figure out what template func params mean/where they come from, e.g. ParserFile(...namedActions...) and Parser(...sempredFuncs...)?

It's not clear why there are separate Parser and Parser_ template funcs. Parser just seems to fill in the string "parser_ctor" for the Parser_ parameter ctor. I'm wondering if there's something going on there I'm not seeing.

@kkbkris
Copy link

kkbkris commented Apr 28, 2016

Dear Will Faught,

I was actually meddling around with these inheritance structures that were implemented using a scripting language, which was specifically JavaScript in my situation if you needed to know, but I do gather that this is of not really of any help at all. Otherwise it is kind of a similar situation, at a stretch.

Yours Sincerely,
Kristian Robert David Stacey
Student reading Computer Science and Electronics Masters Degree in Engineering and Masters Degree in Science at the University of Bristol

On 28 Apr 2016, at 20:17, "Will Faught" notifications@github.com wrote:

All the existing targets appear to have, and make heavy use of, inheritance, which seems to complicate things. Any guidance/tips on how those templates are structured around inheritance and how that structure would change for a target without inheritance (like C)?


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub

@parrt
Copy link
Member

parrt commented Apr 28, 2016

Most templates match up with a model object in code generator. Look for ModelObjectWalker or some such. see it's comment.

@jwkohnen
Copy link
Contributor

Are there three parties independently hacking on this now? I'd like to help out, but only on an open joint venture.

@willfaught
Copy link
Contributor

@pboyer Have you pushed your Go target work? Can you point us to it?

@willfaught
Copy link
Contributor

Depending on how it's written, with the built-in Go parser it should be pretty straightforward to generate a C target from it.

@pboyer
Copy link
Contributor

pboyer commented May 1, 2016

@willfaught @wjkohnen @parrt I should give an update.

I have a working Go runtime and generator templates. 80-90% of the test cases pass and I'm currently using it for a non-trivial task at work.

The situation being what it is, I am going through the process at my employer (Autodesk) of open-sourcing the work. I actually just started this, so I can't say how long it will take. I'm guessing ~2 weeks.

So, if you can wait that long, we should have a working Go target fairly soon. As I said, there's still a few test cases to get to 100%, but it's not a bad starting point. I'll try and give an update soon.

@parrt
Copy link
Member

parrt commented May 1, 2016

Wow. I had no idea you had gotten so far! That's great news. Then a C target could be around the corner as well :) C++ is progressing nicely thanks to @DanMcLaughlin, @mike-lischke, and @davesisson woot!

@willfaught
Copy link
Contributor

Hi @pboyer, how's the Go target going? I don't mean to push, I'm just really interested in trying it out. I'd be happy to help if needed.

@pboyer
Copy link
Contributor

pboyer commented May 18, 2016

@willfaught Thanks for following up. Turns out OSS bureacracy is rather involved. Legal, business reviews are done. Should be out soon.

@pboyer
Copy link
Contributor

pboyer commented May 20, 2016

Hey @willfaught @parrt @sharwell @ericvergnaud,

Here's the fork including a go language target:

https://github.com/pboyer/antlr4

To run the tests, do the usual:

mvn install

To compile for Go, do the usual with:

-Dlanguage=Go

The new stuff:

  • runtime/Go - Obviously the run time for go, based off a mixture of the JavaScript and Java versions.
  • tool/src/org/antlr4/v4/codegen/GoTarget.java - Some directives for target code generation.
  • tool/resources/org/antlr4/v4/tool/templates/codegen/Go - Runtime templates!
  • runtime-testsuite/resources/org/antlr/v4/test/runtime/go/ - Test templates!

Please send me any issues there. I'll try and start adding issues to the fork to keep track of them. You'll notice I haven't synced with the main repo in a while. :P There's still a good bit of work to get 100% test completion. There's also a lot of room for improvement for the quality of the output code.

@parrt It is my hope that this work can someday find it's way into this repo. Is that possible? I figure this needs to live outside of mainline at least until the test completion is fixed, but I'm not sure what additional expectations exist (given this happens fairly rarely).

@parrt
Copy link
Member

parrt commented May 20, 2016

Woot! great news, thanks @pboyer !! I definitely intend to integrate it if I can get somebody to individually verify it all works ;)

@willfaught
Copy link
Contributor

@pboyer What's the pb directory for?

@base698
Copy link
Contributor

base698 commented Aug 1, 2016

Amazing work, just tried to use go tool yacc--a fate worse than death. I'm excited about the prospects this could bring.

@parrt
Copy link
Member

parrt commented Aug 2, 2016

Can anybody else independently verify the go target is working? Do all the unit tests work as they do for other targets?

@willfaught
Copy link
Contributor

There are 8 or 9 tests that still fail in pboyer/antlr4, the fork that adds the Go target. I've fixed as much as I can, but the rest of the failures require understanding how ATNs and all that work. There hasn't been any activity by the other contributors in a while; not sure what happened there.

Answering #1216 and similar future issues would help.

See pboyer#40 for a summary of the remaining work. gitter.im/pboyer/antlr4 may also be of interest.

@willfaught
Copy link
Contributor

You can see the latest test failures here: https://travis-ci.org/pboyer/antlr4

Example:

Failed tests:
  testPositionAdjustingLexer(org.antlr.v4.test.runtime.go.TestLexerExec): expected:<...ns',<6>,1:0](..)
  testZeroLengthToken(org.antlr.v4.test.runtime.go.TestLexerExec): expected:<[@0,0:[4=''xxx'',<1>,1:0](..)
  testReturnValueAndActionsList1_2(org.antlr.v4.test.runtime.go.TestLeftRecursion): expected:<...pr (expr a) , (expr [c) >> (expr x])) <EOF>)(..)
  testReturnValueAndActionsList1_4(org.antlr.v4.test.runtime.go.TestLeftRecursion): expected:<...* (expr b)) , (expr [c) , (expr (expr x) * (expr y)) >> (expr r)]) <EOF>)(..)
  testBasic(org.antlr.v4.test.runtime.go.TestListeners): expected:<(a 1 2)(..)
  testTokenGetters_1(org.antlr.v4.test.runtime.go.TestListeners): expected:<(a 1 2)(..)
  testAmbiguityNoLoop(org.antlr.v4.test.runtime.go.TestFullContextParsing): expected:<[alt 1(..)
  testLoopsSimulateTailRecursion(org.antlr.v4.test.runtime.go.TestFullContextParsing): expected:<line 1:3 reportAttemptingFullContext d=3 (expr_primary), input='a(i)'(..)
  testTokenAndRuleContextString(org.antlr.v4.test.runtime.go.TestParseTrees): expected:<[a[,] s](..)

@pboyer
Copy link
Contributor

pboyer commented Oct 11, 2016

@parrt All of the 4.5.2 tests now pass with this change:

pboyer#64

There will be more work to synchronize with antlr/antlr4 and clean up, but expect a PR for the Go language target soon. :)

@parrt
Copy link
Member

parrt commented Oct 11, 2016

woot!

@mike-lischke
Copy link
Member

@pboyer Does this include tests for all other targets? Did you change anything in the test setup (template file etc.) that would need to be considered by the other language targets?

I'm working currently on the C++ target and had to change a few things which affects testing all other targets (this has mostly to do with additional parameters required for the code template), so I'd much appreciate if you could merge my branch into yours, for which I have a pending pull request already (should go smoothly if you are in synch with the upstream repo) and see if any of your tests got broken (and fix them if so). I'd be willing to do the same with your branch, of course, if this is required.

@pboyer
Copy link
Contributor

pboyer commented Oct 12, 2016

Hi @mike-lischke We tried to avoid changing the test templates very much. So, I don't think this should be a problem. Thanks for the heads up, though and I'll let you know if I run into any difficulties.

@owenthereal
Copy link

@pboyer Awesome! When will the Go target be ready?

@pboyer
Copy link
Contributor

pboyer commented Oct 15, 2016

@jingweno All of the language tests run in github.com/pboyer/antlr4. Right now, I'm just trying to make the API as idiomatic and consistent as I can before submitting the PR. The system is very usable at this point if you build from source.

@johnnadratowski
Copy link

@pboyer - I was trying to use your new Golang target last night, and I hit a bit of a snag:

image

This could very well be user error, as I have no experience with Antlr. I'm really excited to try this out though!

@robbert229
Copy link

You will need to make some serious changes to the cypher grammar to make it work with antlr. The grammar itself has values like return which causes errors because of the reserved keyword return.

I ran into the same issue while attempting to make a parser/lexer via antlr for cypher last week. I haven't cleaned everything up yet myself...

@johnnadratowski

@johnnadratowski
Copy link

johnnadratowski commented Nov 29, 2016

@robbert229 - Once again, I'm very much a novice at this, but it seemed that after I resolved some simple name conflicts it was able to properly parse it. I can send you what I did, it was super simple (literally just renaming 2 items).

Then again, perhaps I was getting those compilation errors because of deeper issues with the Cypher antlr spec...

I really have no clue on this stuff, I was using this as a learning activity (just wanted to output syntax-highlighted cypher for a project I'm working on)

@robbert229
Copy link

robbert229 commented Nov 30, 2016

@johnnadratowski The two changes that you made I made as well, they were my first step towards getting the go runtime working. ( still haven't gotten it working yet )

Additionally just wanted to say that I love your go bolt driver!

@johnnadratowski
Copy link

@robbert229 - Thanks, depending on how this initial learning went, I was thinking about adding some pre-flight cypher checks in the driver.

If you're interested in collaborating on antlr and/or the driver let me know.

@jcoder76
Copy link

jcoder76 commented Dec 9, 2016

Can anyone get the Go language target to work with the grammars?

C:\Users\Jeremy\Documents\Projects\grammars-v4\golang>java -cp c:\users\Jeremy\.m2\repository\org\antlr\antlr4\4.6-SNAPSHOT\antlr4-4.6-SNAPSHOT.jar org.antlr.v4.Tool -Dlanguage=Go Golang.g4
error(134): Golang.g4:341:0: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:101:17: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:294:6: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:344:10: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:376:6: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:382:10: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:401:16: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:431:6: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:443:29: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:592:14: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:627:6: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:137:23: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:540:22: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:294:17: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:596:31: symbol type conflicts with generated code in target language or runtime
error(134): Golang.g4:80:23: symbol type conflicts with generated code in target language or runtime

C:\Users\Jeremy\Documents\Projects\grammars-v4\golang>

@jcoder76
Copy link

jcoder76 commented Dec 9, 2016

Nevermind, fixed it myself. Apparently the Go language definition wasn't written with consideration for having a language with a keyword called type to actually parse it.

@willfaught
Copy link
Contributor

willfaught commented Dec 9, 2016 via email

@jwkohnen
Copy link
Contributor

jwkohnen commented Dec 9, 2016

The situation is not ideal, but not limited to Go. I've learnt that the hard way, when I tried to use a grammar with "super" as a keyword (or rule name?). Target language was Java.

@sharwell
Copy link
Member

sharwell commented Dec 9, 2016

For C# we solve this by converting reserved keywords into other identifiers that are allowed in the language. For example, if you name a rule base, you'll get @base as the method name in generated C# code.

One possibility for Java would be including $ as a prefix or suffix in such a case (but limited to Java keywords there of course). Other languages could automatically convert keywords in a similar manner.

@pboyer
Copy link
Contributor

pboyer commented Dec 9, 2016

@sharwell Thanks for the pointers! Do you do that somewhere in the templates? Not sure if the keyword list is somehow propagated into the stg props.

The other place where we've seen this come up is if the user chooses a rule name that is the same as a method on the RuleContext type - like toString. This could be repaired as well.

@parrt
Copy link
Member

parrt commented Dec 11, 2016

@pboyer Take a look at, say, JavaTarget.java and you'll see a list of "bad words" :) Add keywords analogously there in your GoTarget.java.

@curious-attempt-bunny
Copy link

Did this work stall out, @pboyer? It's sounding pretty promising!

@dgryski
Copy link

dgryski commented Nov 15, 2017

My understanding is that the Go target has been merged into antlr4. Any bugs should be opened as new GitHub issues to be properly prioritized.

@jwkohnen
Copy link
Contributor

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

No branches or pull requests