# Golang grammar definition

**URL:** <https://forum.golangbridge.org/t/golang-grammar-definition/14473>\
**Category:** Technical Discussion\
**Created:** [July 11, 2019, 4:25pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473 "2019-07-11T16:25:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Walker](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/martin_walker/32/5261_2.png) [@Martin\_Walker](https://forum.golangbridge.org/u/Martin_Walker)\
**Post date:** [July 11, 2019, 4:25pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/1 "2019-07-11T16:25:44Z")

</div>

Hi,

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

TIA

regards

Martin

---

<div class="post-metadata">

**Author:** ![iegomez](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/iegomez/32/3643_2.png) [@iegomez](https://forum.golangbridge.org/u/iegomez)\
**Post date:** [July 11, 2019, 4:33pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/2 "2019-07-11T16:33:08Z")

</div>

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

I’d recommend reading [this article](https://eli.thegreenplace.net/2019/go-compiler-internals-adding-a-new-statement-to-go-part-1/) to quickly know where to look for different things.

---

<div class="post-metadata">

**Author:** ![jayts](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/jayts/32/14806_2.png) [@jayts](https://forum.golangbridge.org/u/jayts)\
**Post date:** [July 11, 2019, 5:02pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/3 "2019-07-11T17:02:15Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Martin\_Walker](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/martin_walker/32/5261_2.png) [@Martin\_Walker](https://forum.golangbridge.org/u/Martin_Walker)\
**Post date:** [July 11, 2019, 5:14pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/4 "2019-07-11T17:14:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jayts](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/jayts/32/14806_2.png) [@jayts](https://forum.golangbridge.org/u/jayts)\
**Post date:** [July 11, 2019, 7:26pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/5 "2019-07-11T19:26:05Z")

</div>

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/](https://golang.org/dl/), scroll to the bottom of the page and download [go1.2.2.src.tar.gz](https://dl.google.com/go/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.

---

<div class="post-metadata">

**Author:** ![Martin\_Walker](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/martin_walker/32/5261_2.png) [@Martin\_Walker](https://forum.golangbridge.org/u/Martin_Walker)\
**Post date:** [July 11, 2019, 8:58pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/6 "2019-07-11T20:58:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [October 9, 2019, 8:58pm UTC](https://forum.golangbridge.org/t/golang-grammar-definition/14473/7 "2019-10-09T20:58:19Z")

</div>

Closed on October 9, 2019, 8:58pm UTC

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