Commit bb0e7bda authored by Rob Pike's avatar Rob Pike

goyacc: provide -p flag to set prefix for names.

This should allow multiple goyacc grammars to be
compiled into the same binary. There's a chance it
Fixes #1562.

R=rsc, r2
CC=golang-dev
https://golang.org/cl/4256044
parent 432b4f30
...@@ -11,7 +11,7 @@ GOFILES=\ ...@@ -11,7 +11,7 @@ GOFILES=\
include ../../Make.cmd include ../../Make.cmd
units: goyacc units.y units: goyacc units.y
./goyacc units.y ./goyacc -p units_ units.y
$(GC) y.go $(GC) y.go
$(LD) -o units y.$O $(LD) -o units y.$O
...@@ -17,7 +17,8 @@ Yacc adepts will have no trouble adapting to this form of the tool. ...@@ -17,7 +17,8 @@ Yacc adepts will have no trouble adapting to this form of the tool.
The file units.y in this directory is a yacc grammar for a version of The file units.y in this directory is a yacc grammar for a version of
the Unix tool units, also written in Go and largely transliterated the Unix tool units, also written in Go and largely transliterated
from the Plan 9 C version. from the Plan 9 C version. It needs the flag "-p units_" (see
below).
The generated parser is reentrant. Parse expects to be given an The generated parser is reentrant. Parse expects to be given an
argument that conforms to the following interface: argument that conforms to the following interface:
...@@ -31,8 +32,14 @@ Lex should return the token identifier, and place other token ...@@ -31,8 +32,14 @@ Lex should return the token identifier, and place other token
information in lval (which replaces the usual yylval). information in lval (which replaces the usual yylval).
Error is equivalent to yyerror in the original yacc. Error is equivalent to yyerror in the original yacc.
Code inside the parser may refer to the variable yylex Code inside the parser may refer to the variable yylex,
which holds the yyLexer passed to Parse. which holds the yyLexer passed to Parse.
The "-p prefix" flag to goyacc sets the prefix, by default yy, that
begins the names of symbols, including types, the parser, and the
lexer, generated and referenced by goyacc's generated code. Setting
it to distinct values allows multiple grammars to be used in a
single binary.
*/ */
package documentation package documentation
This diff is collapsed.
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
// Distributed under the terms of the Lucent Public License Version 1.02 // Distributed under the terms of the Lucent Public License Version 1.02
// See http://plan9.bell-labs.com/plan9/license.html // See http://plan9.bell-labs.com/plan9/license.html
// Generate parser with prefix "units_":
// goyacc -p "units_"
%{ %{
// units.y // units.y
...@@ -215,7 +218,7 @@ expr0: ...@@ -215,7 +218,7 @@ expr0:
type UnitsLex int type UnitsLex int
func (UnitsLex) Lex(yylval *yySymType) int { func (UnitsLex) Lex(yylval *units_SymType) int {
var c, i int var c, i int
c = peekrune c = peekrune
...@@ -319,7 +322,7 @@ func main() { ...@@ -319,7 +322,7 @@ func main() {
continue continue
} }
peekrune = ':' peekrune = ':'
yyParse(UnitsLex(0)) units_Parse(UnitsLex(0))
} }
/* /*
...@@ -340,7 +343,7 @@ func main() { ...@@ -340,7 +343,7 @@ func main() {
} }
peekrune = '?' peekrune = '?'
nerrors = 0 nerrors = 0
yyParse(UnitsLex(0)) units_Parse(UnitsLex(0))
if nerrors != 0 { if nerrors != 0 {
continue continue
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment