github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/cmd/goyacc/doc.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 Goyacc is a version of yacc for Go. 7 It is written in Go and generates parsers written in Go. 8 9 Usage: 10 11 goyacc args... 12 13 It is largely transliterated from the Inferno version written in Limbo 14 which in turn was largely transliterated from the Plan 9 version 15 written in C and documented at 16 17 https://9p.io/magic/man2html/1/yacc 18 19 Adepts of the original yacc will have no trouble adapting to this 20 form of the tool. 21 22 The directory $GOPATH/src/golang.org/x/tools/cmd/goyacc/testdata/expr 23 is a yacc program for a very simple expression parser. See expr.y and 24 main.go in that directory for examples of how to write and build 25 goyacc programs. 26 27 The generated parser is reentrant. The parsing function yyParse expects 28 to be given an argument that conforms to the following interface: 29 30 type yyLexer interface { 31 Lex(lval *yySymType) int 32 Error(e string) 33 } 34 35 Lex should return the token identifier, and place other token 36 information in lval (which replaces the usual yylval). 37 Error is equivalent to yyerror in the original yacc. 38 39 Code inside the grammar actions may refer to the variable yylex, 40 which holds the yyLexer passed to yyParse. 41 42 Clients that need to understand more about the parser state can 43 create the parser separately from invoking it. The function yyNewParser 44 returns a yyParser conforming to the following interface: 45 46 type yyParser interface { 47 Parse(yyLex) int 48 Lookahead() int 49 } 50 51 Parse runs the parser; the top-level call yyParse(yylex) is equivalent 52 to yyNewParser().Parse(yylex). 53 54 Lookahead can be called during grammar actions to read (but not consume) 55 the value of the current lookahead token, as returned by yylex.Lex. 56 If there is no current lookahead token (because the parser has not called Lex 57 or has consumed the token returned by the most recent call to Lex), 58 Lookahead returns -1. Calling Lookahead is equivalent to reading 59 yychar from within in a grammar action. 60 61 Multiple grammars compiled into a single program should be placed in 62 distinct packages. If that is impossible, the "-p prefix" flag to 63 goyacc sets the prefix, by default yy, that begins the names of 64 symbols, including types, the parser, and the lexer, generated and 65 referenced by yacc's generated code. Setting it to distinct values 66 allows multiple grammars to be placed in a single package. 67 */ 68 package main