You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.4 KiB

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