Golang grammar definition

Hi,

Does anyone know where the lex/yacc or equivalent files for golang live.

TIA

regards

Martin

1 Like

Everything about the Go compiler lives here: https://github.com/golang/go/tree/master/src/cmd/compile/internal.

I’d recommend reading this article to quickly know where to look for different things.

2 Likes

Hi Martin,

From what I found, it appears that the Go compiler formerly used Bison, GNU’s yacc replacement, but since version 1.7 now uses a “hand-written” (non-automated) scanner (lexer) and parser.

These may be found in the $GOROOT/src/cmd/compile/internal/syntax directory, in the source files scanner.go and parser.go.

The EBNF specification for the Go language is included in the Go Programming Language Specification (https://golang.org/ref/spec), and you can use that to create the files for lex and yacc yourself if you want.

I haven’t tried, but maybe you can look in the source code for Go version 1.6 or previous versions to get the files you want. Assuming that works, you will still need to look at the Go specification to add in everything that’s been added to Go since then.

1 Like

Thanks for that, will take a prowl for 1.6
I basically have written a language and fancy adding some go like grammar to it, but an struggling with my .y file, so thought I’d see how go defined things.

1 Like

I looked into it some more to see if the information I’d read off of non-definitive sources (and even Wikipedia) was correct, and learned that by Go 1.5, they had already moved to a yacc-compatible parser generator written in Go. I could not find any yacc source files there (like a y.tab.c file).

But in version 1.2 (at least), they were still using Bison: There are yacc files in the Go source code, and I found the one for the Go parser. Here’s how to get to it:

  1. Go to https://golang.org/dl/, scroll to the bottom of the page and download go1.2.2.src.tar.gz.

  2. un-tar the file (tar xvfz go1.2.2.src.tar.gz), and look in the directory go/src/cmd/gc for a file called go.y.

There is also a y.tab.c file and in that, comments show that they were using Bison 2.5, and that go.y was used to generate y.tab.c.

Upon seeing that, I recollected that it was at version 1.5 that the Go Team had moved from C to Go to implement the compiler and runtime. That’s why Bison was no longer used at that point.

I looked at only the oldest version of Go listed on the download page. You can try more recent versions if you want. Other versions previous to 1.5 may have yacc files, too.

1 Like

Thanks for looking, I had actually found a lot of that by looking at 1.6 . I’m actually also using the goyacc parser, but it all seems to little avail, my grammar uses ansi c grammar style as its base, golang constructs things differently.
What I wanted to add was multiple return values, and I think to do that I’d need to almost totally rewrite.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.