From Wikipedia, the free encyclopedia
Jump to: navigation, search

Yacc (Yet Another Compiler-Compiler) is a computer program for the Unix operating system. It is a Look Ahead Left-to-Right (LALR) parser generator, generating a parser, the part of a compiler that tries to make syntactic sense of the source code, specifically a LALR parser, based on an analytic grammar written in a notation similar to Backus–Naur Form (BNF).[1] Yacc itself used to be available as the default parser generator on most Unix systems, though it has since been supplanted[citation needed] as the default by more recent, largely compatible, programs.


Yacc was originally developed in the early 1970s by Stephen C. Johnson at AT&T Corporation and written in the B programming language, but soon rewritten in C.[2] It appeared as part of Version 3 Unix,[3] and a full description of Yacc was published in 1975.[4]

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 node(label, left, right) constructs a binary parse tree node with the specified label and children, then the rule

expr : expr '+' expr  { $$ = node('+', $1, $3); }

recognizes summation expressions and constructs nodes for them. The special identifiers $$, $1 and $3 refer to items on the parser's stack.[4]

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 as the default 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 version is included as part of Sun's OpenSolaris project. Each offers slight improvements and additional features over the original Yacc, but the concept and syntax have remained the same.[citation needed] Yacc has also been rewritten for other languages, including OCaml,[5] Ratfor, ML, Ada, Pascal, Java, Python, Ruby, Go[6] and Common Lisp.[7]

Yacc produces only a parser (phrase analyzer); for full syntactic analysis this requires an external lexical analyzer to perform the first tokenization stage (word analysis), which is then followed by the parsing stage proper.[4] Lexical analyzer generators, such as Lex or Flex are widely available. The IEEE POSIX P1003.2 standard defines the functionality and requirements for both Lex and Yacc.[8]

Some versions of AT&T Yacc have become open source. For example, source code (for different implementations) is available with the standard distributions of Plan 9 and OpenSolaris.[citation needed]

See also[edit]

  • Berkeley Yacc: The Berkeley implementation of Yacc quickly became more popular than AT&T Yacc itself because of lack of reuse restrictions and performance.
  • LALR parser: The underlying parsing algorithm in Yacc-generated parsers.
  • Bison: The GNU version of Yacc.
  • Lex (and Flex lexical analyser), a token parser commonly used in conjunction with Yacc (and Bison).
  • BNF, is a metasyntax used to express context-free grammars: that is, a formal way to describe context-free languages.
  • PLY (Python Lex-Yacc) is an alternative implementation of Lex and Yacc in python.


  1. ^ "The A-Z of Programming Languages: YACC". Computerworld. Retrieved 30 November 2012. 
  2. ^ Ritchie, Dennis M. (April 1993). The Development of the C Language (PDF). Association for Computing Machinery, Inc. 
  3. ^ McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139. 
  4. ^ a b c Johnson, Stephen C. (1975). "Yacc: Yet Another Compiler-Compiler". AT&T Bell Laboratories Technical Reports. AT&T Bell Laboratories Murray Hill, New Jersey 07974 (32). Retrieved 31 October 2014. 
  5. ^ "OCaml User's Manual: Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc)". Retrieved 25 Nov 2013. 
  6. ^ "Yacc.go: A version of Yacc for the Go Programming Language". Retrieved 15 July 2017. 
  7. ^ "CL-Yacc: A Common Lisp version of Yacc". 
  8. ^ lex – Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group, yacc – Commands & Utilities Reference, The Single UNIX Specification, Issue 7 from The Open Group.

External links[edit]