blob: 328d87b9da53efe301ff16d001b50b537aeaaca3 [file] [log] [blame]
Rob Pike472576c2009-11-03 22:38:43 -08001// 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
Andrew Gerrand7e8ed8f2012-03-05 14:23:00 +11007Yacc is a version of yacc for Go.
Russ Cox18ccbc62009-11-09 11:45:15 -08008It is written in Go and generates parsers written in Go.
Rob Pike472576c2009-11-03 22:38:43 -08009
Andrew Gerrand7e8ed8f2012-03-05 14:23:00 +110010Usage:
11
12 go tool yacc args...
13
Rob Pike472576c2009-11-03 22:38:43 -080014It is largely transliterated from the Inferno version written in Limbo
15which in turn was largely transliterated from the Plan 9 version
16written in C and documented at
17
18 http://plan9.bell-labs.com/magic/man2html/1/yacc
19
Rob Pike79dc3442012-01-29 09:19:05 -080020Adepts of the original yacc will have no trouble adapting to this
21form of the tool.
Rob Pike472576c2009-11-03 22:38:43 -080022
Jeremy Jackins4eb93022015-03-17 13:00:31 +090023The directory $GOROOT/src/cmd/yacc/testdata/expr is a yacc program
Russ Cox9c721ae2014-09-06 15:27:52 -040024for a very simple expression parser. See expr.y and main.go in that
25directory for examples of how to write and build yacc programs.
Rob Pike472576c2009-11-03 22:38:43 -080026
Russ Coxc7fa3c62015-02-11 20:37:56 -050027The generated parser is reentrant. The parsing function yyParse expects
28to be given an argument that conforms to the following interface:
Roger Peppe6c3befc2010-04-06 13:29:27 -070029
30 type yyLexer interface {
31 Lex(lval *yySymType) int
Roger Peppebcdcf392010-05-17 12:23:48 -070032 Error(e string)
Roger Peppe6c3befc2010-04-06 13:29:27 -070033 }
34
35Lex should return the token identifier, and place other token
36information in lval (which replaces the usual yylval).
Roger Peppebcdcf392010-05-17 12:23:48 -070037Error is equivalent to yyerror in the original yacc.
Roger Peppe6c3befc2010-04-06 13:29:27 -070038
Russ Coxc7fa3c62015-02-11 20:37:56 -050039Code inside the grammar actions may refer to the variable yylex,
40which holds the yyLexer passed to yyParse.
41
42Clients that need to understand more about the parser state can
43create the parser separately from invoking it. The function yyNewParser
44returns a yyParser conforming to the following interface:
45
46 type yyParser interface {
47 Parse(yyLex) int
48 Lookahead() int
49 }
50
51Parse runs the parser; the top-level call yyParse(yylex) is equivalent
52to yyNewParser().Parse(yylex).
53
54Lookahead can be called during grammar actions to read (but not consume)
55the value of the current lookahead token, as returned by yylex.Lex.
56If there is no current lookahead token (because the parser has not called Lex
57or has consumed the token returned by the most recent call to Lex),
58Lookahead returns -1. Calling Lookahead is equivalent to reading
59yychar from within in a grammar action.
Roger Peppe6c3befc2010-04-06 13:29:27 -070060
Rob Pike41a23ca2011-02-28 20:47:52 -080061Multiple grammars compiled into a single program should be placed in
62distinct packages. If that is impossible, the "-p prefix" flag to
Rob Pike79dc3442012-01-29 09:19:05 -080063yacc sets the prefix, by default yy, that begins the names of
Rob Pike41a23ca2011-02-28 20:47:52 -080064symbols, including types, the parser, and the lexer, generated and
Rob Pike79dc3442012-01-29 09:19:05 -080065referenced by yacc's generated code. Setting it to distinct values
Rob Pike41a23ca2011-02-28 20:47:52 -080066allows multiple grammars to be placed in a single package.
Rob Pikebb0e7bd2011-02-28 17:43:16 -080067
Rob Pike472576c2009-11-03 22:38:43 -080068*/
Robert Griesemer3ee87d02013-02-19 11:19:58 -080069package main