Yacc Explained

Yacc
Author:Stephen C. Johnson
Programming Language:C
Operating System:Unix, Unix-like, Plan 9, Inferno
Platform:Cross-platform
Genre:Command
License:Plan 9: MIT License

Yacc (Yet Another Compiler-Compiler) is a computer program for the Unix operating system developed by Stephen C. Johnson. It is a lookahead left-to-right rightmost derivation (LALR) parser generator, generating a LALR parser (the part of a compiler that tries to make syntactic sense of the source code) based on a formal grammar, written in a notation similar to Backus–Naur form (BNF).[1] Yacc is supplied as a standard utility on BSD and AT&T Unix.[2] GNU-based Linux distributions include Bison, a forward-compatible Yacc replacement.[3]

History

In the early 1970s, Stephen C. Johnson, a computer scientist at Bell Labs / AT&T, developed Yacc because he wanted to insert an exclusive or operator into a B language compiler[4] (developed using McIlroy's TMG compiler-compiler), but it turned out to be a hard task. As a result, he was directed by his colleague at Bell Labs Al Aho to Donald Knuth's work on LR parsing, which served as the basis for Yacc.[5] Yacc was influenced by and received its name in reference to TMG compiler-compiler.[6]

Yacc was originally written in the B programming language, but was soon rewritten in C by Alan Snyder.[7] It appeared as part of Version 3 Unix,[8] and a full description of Yacc was published in 1975.[9]

Johnson used Yacc to create the Portable C Compiler.[8] Bjarne Stroustrup also attempted to use Yacc to create a formal specification of C++, but "was defeated by C's syntax".[10] While finding it unsuitable for a formal specification of the language, Stroustrup did proceed to use Yacc to implement Cfront, the first implementation of C++.[11]

In a 2008 interview, Johnson reflected that "the contribution Yacc made to the spread of Unix and C is what I'm proudest of".[12]

Description

The input to Yacc is a grammar with snippets of C code (called "actions") attached to its rules. Its output is a shift-reduce parser in C that executes the C snippets associated with each rule as soon as the rule is recognized. Typical actions involve the construction of parse trees. Using an example from Johnson, if the call constructs a binary parse tree node with the specified and children, then the ruleexpr : expr '+' expr recognizes summation expressions and constructs nodes for them. The special identifiers, and refer to items on the parser's stack.

Yacc produces only a parser (phrase analyzer) which can be used alone in the case of scannerless parsing however, full syntactic analysis typically requires an external lexical analyzer to perform a tokenization stage first (word analysis), which is then followed by the parsing stage proper.[9] Lexical analyzer generators, such as Lex or Flex, are widely available for this purpose. The IEEE POSIX P1003.2 standard defines the functionality and requirements for both Lex and Yacc.[13]

Some versions of AT&T Yacc have become open source. For example, source code is available with the standard distributions of Plan 9.[14]

Impact

Yacc and similar programs (largely reimplementations) have been very popular. Yacc itself used to be available as the default parser generator on most Unix systems, though it has since been supplanted by more recent, largely compatible, programs such as Berkeley Yacc, GNU Bison, MKS Yacc, and Abraxas PCYACC. An updated version of the original AT&T Yacc is included as part of Sun's OpenSolaris project. Each offers slight improvements and additional features over the original Yacc, but the concept and basic syntax have remained the same.[15]

Yacc was also one of several UNIX tools available for Charles River Data Systems' UNOS operating system under Bell Laboratories license.[16]

Among the languages that were first implemented with Yacc are AWK, C++,[17] eqn and Pic.[18] Yacc was also used on Unix to implement the Portable C Compiler, as well as parsers for such programming languages as FORTRAN 77, Ratfor, APL, bc, m4, etc.[8] [19]

Yacc has also been rewritten for other languages, including OCaml,[20] Ratfor, ML, Ada, Pascal, Java, PHP, Python, Ruby, Go,[21] Common Lisp[22] and Erlang.[23]

See also

External links

Notes and References

  1. Web site: The A-Z of Programming Languages: YACC . Computerworld . 30 November 2012 . 31 January 2013 . https://web.archive.org/web/20130131031011/http://www.techworld.com.au/article/252319/a-z_programming_languages_yacc/ . dead .
  2. Book: Levine, John . Lex & yacc . O'Reilly & Associates . Sebastopol, CA . 1992 . 1-56592-000-7 . xx . registration .
  3. Book: Levine, John . Flex & bison . O'Reilly Media . Sebastopol, Calif . 2009 . 978-0-596-15597-1 . xv.
  4. News: Morris. Richard. Stephen Curtis Johnson: Geek of the Week. 19 January 2018. Red Gate Software. 1 October 2009.
  5. News: Morris. Richard. Stephen Curtis Johnson: Geek of the Week. 19 January 2018. Red Gate Software. 1 October 2009.
  6. Web site: Early Translator Writing Systems . Atlas Computer Laboratory.
  7. Ritchie. Dennis M.. Dennis Ritchie. April 1993. The Development of the C Language. Association for Computing Machinery, Inc.. 10.1145/234286.1057834. free. History of programming languages---II. en. 0-201-89502-1. 1996-01-01. After the TMG version of B was working, Thompson rewrote B in itself(a bootstrapping step).…When Johnson returned to Bell Labs in 1973, he was disconcerted to find that the language whose seeds he had brought to Canada had evolved back home; even his own yacc program had been rewritten in C, by Alan Snyder.. 675,684.
  8. M. D. . McIlroy . Doug McIlroy . 1987 . A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 . CSTR . 139 . Bell Labs.
  9. Johnson. Stephen C.. Stephen C. Johnson. 1975. Yacc: Yet Another Compiler-Compiler. 32. AT&T Bell Laboratories. Murray Hill, New Jersey. 31 January 2020.
  10. Web site: Stroustrup. Bjarne . Bjarne Stroustrup . A History of C++: 1979−1991.
  11. Web site: Stroustrup. Bjarne . Bjarne Stroustrup . Cfront source code.
  12. News: Yacc, Unix, and advice from Bell Labs alumni Stephen Johnson. Hamilton. Naomi. 2008-07-09. www.computerworld.com. 2020-11-10. live. https://web.archive.org/web/20200822224200/https://www.computerworld.com/article/2534771/yacc--unix--and-advice-from--bell-labs-alumni-stephen-johnson.html. 2020-08-22.
  13. , .
  14. Web site: plan9: UC Berkeley release of Plan 9 under the GPLv2. . 2 January 2018. 26 December 2017.
  15. https://www.gnu.org/software/bison/manual/html_node/History.html#History Bison Manual: History
  16. Book: 1983. The Insider's Guide To The Universe. Charles River Data Systems, Inc.. 13.
  17. Web site: Stroustrup. Bjarne . Bjarne Stroustrup . Cfront source code.
  18. Web site: UNIX Special: Profs Kernighan & Brailsford . https://ghostarchive.org/varchive/youtube/20211211/vT_J6xc-Az0. 2021-12-11 . live. Computerphile . September 30, 2015 .
  19. Book: Kernighan. Brian W.. Pike. Rob. The Unix Programming Environment. 1984. Prentice Hall. 0-13-937681-X.
  20. Web site: OCaml User's Manual: Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) . 25 Nov 2013.
  21. Web site: Yacc.go: A version of Yacc for the Go Programming Language . 15 July 2017.
  22. Web site: CL-Yacc: A Common Lisp version of Yacc .
  23. Web site: yecc: An Erlang implementation of Yacc .