Grammar parsing in go

514 views
Skip to first unread message

Matt

unread,
Aug 18, 2011, 9:44:28 AM8/18/11
to golang-nuts
I'd like to be able to parse a regular c-like grammer within a go
program and export something approaching go code, which can later be
compiled.

As I've mentioned on this thread before; ideally I'd love to compile
the generated go code and run within the same executable but this
isn't possible.

Any ideas, interesting approaches.

I'm guessing building on go's parser package might be one option?

John Asmuth

unread,
Aug 18, 2011, 1:13:11 PM8/18/11
to golan...@googlegroups.com
For generating and printing go code, you can use the go/ast and go/printer packages. I suggest you parse some existing go code and walk the tree a bit to see how things should be laid out.

For parsing a custom syntax, try http://golang.org/cmd/goyacc

Matt

unread,
Aug 19, 2011, 11:56:17 AM8/19/11
to golang-nuts
Is there a way of invoking goyacc from within go (without using exec)?

bflm

unread,
Aug 19, 2011, 5:20:44 PM8/19/11
to golan...@googlegroups.com
On Friday, August 19, 2011 5:56:17 PM UTC+2, Matt wrote:
Is there a way of invoking goyacc from within go (without using exec)?

"Without using exec" && "from within go"? Sounds strange to me. Why?!? Are you sure this is what you want? Goyacc is a command line utility which transforms one source language (.y) to another one (.go). Often it is invoked by 'make' of some package or application.

I'm not sure what is actually to be accomplished...

Paul Borman

unread,
Aug 19, 2011, 5:27:47 PM8/19/11
to golan...@googlegroups.com
Actually, what I hear is that Matt wants to write a yet another compiler compiler that uses a different input language than what yacc uses.

Matt

unread,
Aug 23, 2011, 8:29:00 AM8/23/11
to golang-nuts
No Paul, I'm fine with using goyacc, was just curious if it was
available inside go, rather than as an external element. If I'm
parsing some pseudo-ish code inside my go program, and doing some
manipulation there, it seems a little overkill to save out to a file,
to exec goyacc on that file.

I really wanted a flow something like:

- extract pseudo
- manipulate pseudo (strip off unnecessary parts, cleanup, select
parts of interest)
- run goyacc on that (from inside the code already manipulating it)

Seems this isn't acheivable so I'll go the exec route or just process
outside of go using goyacc.

I certainly do not want to write yet another yet another compiler
compiler!

Jan Mercl

unread,
Aug 23, 2011, 8:46:24 AM8/23/11
to golan...@googlegroups.com
On Tuesday, August 23, 2011 2:29:00 PM UTC+2, Matt wrote:
No Paul, I'm fine with using goyacc, was just curious if it was
available inside go, rather than as an external element. If I'm
parsing some pseudo-ish code inside my go program, and doing some
manipulation there, it seems a little overkill to save out to a file,
to exec goyacc on that file.

That's unfortunately still seems like a misunderstanding of what goyacc is for, I think. Goyacc translates .y to .go. The later is compiled into your app and there is no need to execute goyacc from your app afterwards.

John C.

unread,
Aug 23, 2011, 8:59:57 AM8/23/11
to golan...@googlegroups.com
Matt,

I faced the same issue several months ago where it would have been awfully convenient for there to be something like a "package goyacc", kind of like a package regexp, where you could have a string defining the grammar, call myCFG := goyacc.MustCompile(...), then myCFG.Parse(...), then get as output a tree or something similar.  I was even willing to give up the capability for actions on reductions.  However, since it was a side project and didn't justify too much more looking into it, I ended up just using goyacc and some batch files (on win32) and ended with a clunky though functional solution.

John C.

Matt

unread,
Aug 31, 2011, 6:39:56 AM8/31/11
to golang-nuts
Looks like Rob Pike's latest talk at GTUG Sydney is of interest.

http://www.youtube.com/watch?v=HxaD_trXwRE

I think he understands my problem :)
Reply all
Reply to author
Forward
0 new messages