golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.66k stars 17.62k forks source link

x/tools/cmd/goyacc: goyacc does not support `%code` #56944

Open pjbgf opened 1 year ago

pjbgf commented 1 year ago

What version of Go are you using (go version)?

$ go version
go version go1.19.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/pjbgf/.cache/go-build"
GOENV="/home/pjbgf/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/levi/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/levi/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/pjbgf/go/src/github.com/pjbgf/go-apparmor/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2200620041=/tmp/go-build -gno-record-gcc-switches"

What did you do?

$ curl -o parser_yacc.y https://gitlab.apertis.org/pkg/apparmor/-/raw/ubuntu/master/parser/parser_yacc.y
$ goyacc -o output parser_yacc.y
invalid escape, or illegal reserved word: code: parser_yacc.y:182

What did you expect to see?

Successful processing of parser_yacc.y.

What did you see instead?

invalid escape, or illegal reserved word: code: parser_yacc.y:182

robpike commented 1 year ago

Goyacc is derived from the original Unix 7th Edition yacc, with a few tweaks along the way. The %code token was not part of that implementation and I am unfamiliar with it.

What does it do, where have you seen it supported, and why do you need it?

pjbgf commented 1 year ago

I found this about %code:

The %code directive inserts code verbatim into the output parser source at any of a predefined set of locations. It thus serves as a flexible and user-friendly alternative to the traditional Yacc prologue, %{code%}.

I am trying to use the official yacc parser definition for AppArmor, which currently uses it. In terms of implementation, the GNU yacc CLI supports it.

robpike commented 1 year ago

The workaround is trivial, although it does require editing the grammar.