Yacc


Yacc is a computer program for the Unix operating system developed by Stephen C. Johnson. It is a Look Ahead Left-to-Right 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. Yacc is supplied as a standard utility on BSD and AT&T Unix. GNU-based Linux distributions include Bison, a forward-compatible Yacc replacement.

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, but it turned out to be a hard task. As a result, he was directed by Bell Labs colleague Al Aho to Donald Knuth's work on LR parsing, which served as the basis for Yacc. Yacc was influenced by and received its name in reference to TMG compiler-compiler.
Yacc was originally written in the B programming language, but was soon rewritten in C. It appeared as part of Version 3 Unix, and a full description of Yacc was published in 1975.
Johnson used Yacc to create the Portable C Compiler. Bjarne Stroustrup, on the other hand, attempted to use Yacc for his initial work on C++, but "was defeated by C's syntax".
In a 2008 interview, Johnson reflected that "the contribution Yacc made to the spread of Unix and C is what I'm proudest of".

Description

The input to Yacc is a grammar with snippets of C code 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 rule

expr : 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 ; for full syntactic analysis this requires an external lexical analyzer to perform the first tokenization stage, which is then followed by the parsing stage proper. 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.
Some versions of AT&T Yacc have become open source. For example, source code is available with the standard distributions of Plan 9.

Impact

Yacc and similar programs 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 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 basic syntax have remained the same.
Among the languages that were first implemented with Yacc are AWK, eqn and Pic. 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.
Yacc has also been rewritten for other languages, including OCaml, Ratfor, ML, Ada, Pascal, Java, Python, Ruby, Go,, Common Lisp and Erlang.