Rob Pike | 472576c | 2009-11-03 22:38:43 -0800 | [diff] [blame] | 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 | |
Andrew Gerrand | 7e8ed8f | 2012-03-05 14:23:00 +1100 | [diff] [blame] | 7 | Yacc is a version of yacc for Go. |
Russ Cox | 18ccbc6 | 2009-11-09 11:45:15 -0800 | [diff] [blame] | 8 | It is written in Go and generates parsers written in Go. |
Rob Pike | 472576c | 2009-11-03 22:38:43 -0800 | [diff] [blame] | 9 | |
Andrew Gerrand | 7e8ed8f | 2012-03-05 14:23:00 +1100 | [diff] [blame] | 10 | Usage: |
| 11 | |
| 12 | go tool yacc args... |
| 13 | |
Rob Pike | 472576c | 2009-11-03 22:38:43 -0800 | [diff] [blame] | 14 | It is largely transliterated from the Inferno version written in Limbo |
| 15 | which in turn was largely transliterated from the Plan 9 version |
| 16 | written in C and documented at |
| 17 | |
| 18 | http://plan9.bell-labs.com/magic/man2html/1/yacc |
| 19 | |
Rob Pike | 79dc344 | 2012-01-29 09:19:05 -0800 | [diff] [blame] | 20 | Adepts of the original yacc will have no trouble adapting to this |
| 21 | form of the tool. |
Rob Pike | 472576c | 2009-11-03 22:38:43 -0800 | [diff] [blame] | 22 | |
Jeremy Jackins | 4eb9302 | 2015-03-17 13:00:31 +0900 | [diff] [blame] | 23 | The directory $GOROOT/src/cmd/yacc/testdata/expr is a yacc program |
Russ Cox | 9c721ae | 2014-09-06 15:27:52 -0400 | [diff] [blame] | 24 | for a very simple expression parser. See expr.y and main.go in that |
| 25 | directory for examples of how to write and build yacc programs. |
Rob Pike | 472576c | 2009-11-03 22:38:43 -0800 | [diff] [blame] | 26 | |
Russ Cox | c7fa3c6 | 2015-02-11 20:37:56 -0500 | [diff] [blame] | 27 | The generated parser is reentrant. The parsing function yyParse expects |
| 28 | to be given an argument that conforms to the following interface: |
Roger Peppe | 6c3befc | 2010-04-06 13:29:27 -0700 | [diff] [blame] | 29 | |
| 30 | type yyLexer interface { |
| 31 | Lex(lval *yySymType) int |
Roger Peppe | bcdcf39 | 2010-05-17 12:23:48 -0700 | [diff] [blame] | 32 | Error(e string) |
Roger Peppe | 6c3befc | 2010-04-06 13:29:27 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Lex should return the token identifier, and place other token |
| 36 | information in lval (which replaces the usual yylval). |
Roger Peppe | bcdcf39 | 2010-05-17 12:23:48 -0700 | [diff] [blame] | 37 | Error is equivalent to yyerror in the original yacc. |
Roger Peppe | 6c3befc | 2010-04-06 13:29:27 -0700 | [diff] [blame] | 38 | |
Russ Cox | c7fa3c6 | 2015-02-11 20:37:56 -0500 | [diff] [blame] | 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. |
Roger Peppe | 6c3befc | 2010-04-06 13:29:27 -0700 | [diff] [blame] | 60 | |
Rob Pike | 41a23ca | 2011-02-28 20:47:52 -0800 | [diff] [blame] | 61 | Multiple grammars compiled into a single program should be placed in |
| 62 | distinct packages. If that is impossible, the "-p prefix" flag to |
Rob Pike | 79dc344 | 2012-01-29 09:19:05 -0800 | [diff] [blame] | 63 | yacc sets the prefix, by default yy, that begins the names of |
Rob Pike | 41a23ca | 2011-02-28 20:47:52 -0800 | [diff] [blame] | 64 | symbols, including types, the parser, and the lexer, generated and |
Rob Pike | 79dc344 | 2012-01-29 09:19:05 -0800 | [diff] [blame] | 65 | referenced by yacc's generated code. Setting it to distinct values |
Rob Pike | 41a23ca | 2011-02-28 20:47:52 -0800 | [diff] [blame] | 66 | allows multiple grammars to be placed in a single package. |
Rob Pike | bb0e7bd | 2011-02-28 17:43:16 -0800 | [diff] [blame] | 67 | |
Rob Pike | 472576c | 2009-11-03 22:38:43 -0800 | [diff] [blame] | 68 | */ |
Robert Griesemer | 3ee87d0 | 2013-02-19 11:19:58 -0800 | [diff] [blame] | 69 | package main |