This article needs additional citations for verification. (October 2015) (Learn how and when to remove this template message)
In computer science, a compiler-compiler or compiler generator is a programming tool that creates a parser, interpreter, or compiler from some form of formal description of a programming language and machine. The input may be a text file containing the grammar written in BNF or EBNF that defines the syntax of a programming language, and whose generated output is some source code of the parser for the programming language, although other definitions exist. Usually, the resulting source code will have to be extended upon before a complete compiler emerges.
A metacompiler is a software development tool used chiefly in the construction of compilers, translators, and interpreters for other programming languages. The input to a metacompiler is a computer program written in a specialized programming metalanguage designed chiefly for the purpose of constructing compilers; the language of the compiler produced is called the object language. The minimal input producing a compiler is a metaprogram specifying the object language grammar and semantic transformations into an object program.
- 1 Variants
- 2 History
- 3 Schorre metalanguages
- 4 Examples
- 5 See also
- 6 References and notes
- 7 Further reading
- 8 External links
A typical parser generator associates executable code with each of the rules of the grammar that should be executed when these rules are applied by the parser; these pieces of code are sometimes referred to as semantic action routines since they define the semantics of the syntactic structure that is analyzed by the parser. Depending upon the type of parser that should be generated, these routines may construct a parse tree (or abstract syntax tree), or generate executable code directly.
One of the earliest (1964), surprisingly powerful, versions of compiler-compilers is META II, which accepted an analytical grammar with output facilities that produce stack machine code, and is able to compile itself and other languages.
Some experimental compiler-compilers take as input a formal description of programming language semantics, typically using denotational semantics; this approach is often called 'semantics-based compiling', and was pioneered by Peter Mosses' Semantic Implementation System (SIS) in 1978. However, both the generated compiler and the code it produced were inefficient in time and space. No production compilers are currently built in this way, but research continues.
The Production Quality Compiler-Compiler project at Carnegie-Mellon University does not formalize semantics, but does have a semi-formal framework for machine description.
Compiler-compilers exist in many flavors, including bottom-up rewrite machine generators (see JBurg) used to tile syntax trees according to a rewrite grammar for code generation, and attribute grammar parser generators (e.g. ANTLR can be used for simultaneous type checking, constant propagation, and more during the parsing stage).
Metacompilers reduce the task of writing compilers by automating the aspects that are the same regardless of the object language; this makes possible the design of domain-specific languages which are appropriate to the specification of a particular problem. A metacompiler reduces the cost of producing translators for such domain-specific object languages to a point where it becomes economically feasible to include in the solution of a problem a domain-specific language design.
Besides being useful for domain-specific language development, a metacompiler is itself a prime example of a domain-specific language, designed for the domain of compiler writing.
A metacompiler is a metaprogram usually written in its own metalanguage or an existing computer programming language; the process of a metacompiler, written in its own metalanguage, compiling itself is equivalent to self-hosting compiler. Most common compilers written today are Self-hosting compilers. Self-hosting is a powerful tool, of many metacompilers, allowing the easy extension of their own metaprogramming metalanguage; the feature that separates a metacompiler apart from other compiler compilers is that it takes as input a specialized metaprogramming language that describes all aspects of the compilers operation. A metaprogram produced by a metacompiler is as complete a program as a program written in C++, BASIC or any other general programming language; the metaprogramming metalanguage is a powerful attribute allowing the ease of development of computer programming languages and other computer tools. Command line processors, text string transforming and analysis are easily coded using metaprogramming metalanguages of metacompilers.
A full featured development package includes a linker and a run-time support library. Usually a machine oriented language is required for writing the support library. C or C++ could be used as a machine oriented language. A library consisting of support functions required for the compilation process usually rounds out the full metacompiler package.
The meaning of metacompiler
In Computer Science the prefix meta is commonly used to mean about (its own category). As in metadata (about data). Data describing other data. A language that is used to describe other languages is a metalanguage. Meta may also mean on a higher level of abstraction. A metalanguage operates on a higher level of abstraction in order to describe properties of a language. Backus–Naur form (BNF) is a formal metalanguage originally used to define ALGOL 60. BNF is a weak metalanguage, for it describes only the syntax and says nothing about the semantics or meaning. Metaprogramming is the writing of computer programs with the ability to treat programs as their data. A metacompiler takes as input a metaprogram written in a specialized metalanguages (a higher level abstraction) specifically designed for the purpose of metaprogramming; the output is an executable object program.
This section's tone or style may not reflect the encyclopedic tone used on Wikipedia. (August 2015) (Learn how and when to remove this template message)
Many Forth advocates call the process of creating a new implementation of Forth a meta-compilation and that it constitutes a metacompiler; the FORTH definition of metacompiler is:
- "A metacompiler is a compiler which processes its own source code, resulting in an executable version of itself."
This Forth use of metacompiler is disputed in mainstream computer science. See Forth (programming language) and History of compiler construction; the actual Forth process of compiling itself is a combination of a Forth being a Self-hosting Extensible programming language and sometimes Cross compilation, long established terminology in computer science. Metacompilers are a general compiler writing system. Besides the FORTH metacompiler concept being indistinguishable from self-hosting and extensible language; the actual process acts at a lower level defining a minimum subset of forth words, that can be used to define additional forth words, A full FORTH implementation can then be defined from the base set. This sounds like a bootstrap process; the problem is that almost every general purpose language compiler also fits the FORTH metacompiler description.
- When (self-hosting compiler) X processes its own source code, resulting in an executable version of itself, X is a metacompiler.
Just replace X with any common language, C, C++, Pascal, COBOL, FORTRAN, Ada, Modula-2, etc, and X would be a meta compiler according to the FORTH usage of metacompiler. A metacompiler operates at an abstraction level above the compiler it compiles, it only operates at the same (self-hosting compiler)level when compiling itself. One has to see the problem with this definition of metacompiler, it can be applied to most any language.
However, on examining the concept of programming in FORTH, adding new words to the dictionary, extending the language in this way is metaprogramming, it is this metaprogramming in forth that makes it a metacompiler.
Programming in FORTH is adding new words to the language. Changing the language in this way is metaprogramming. FORTH is a metacompiler because FORTH is a language specifically designed for metaprogramming. Programming in FORTH is extending FORTH adding WORDS to the forth vocabulary creates a new FORTH dialect. FORTH is a specialized metacompiler for FORTH language dialects.
The first compiler-compiler to use that name was written by Tony Brooker in 1960 and was used to create compilers for the Atlas computer at the University of Manchester, including the Atlas Autocode compiler.
The early history of metacompilers is closely tied with the history of SIG/PLAN Working group 1 on Syntax Driven Compilers; the group was started primarily through the effort of Howard Metcalfe in the Los Angeles area. In the fall of 1962 Howard Metcalfe designed two compiler-writing interpreters. One used a bottom-to-top analysis technique based on a method described by Ledley and Wilson; the other used a top-to-bottom approach based on a work by glennie to generate random English sentences from a context-free grammar.
At the same time, Val Schorre described two "meta machines". One generative and one analytic; the generative machine was implemented and produced random algebraic expressions. Meta I the first metacompiler was implemented by Schorre on an IBM 1401 at UCLA in January 1963, his original interpreters and metamachines were written directly in a pseudo-machine language. Meta II, however, was written in a higher-level metalanguage able to describe its own compilation into the pseudo-machine language.
Lee Schmidt at Bolt, Beranek, and Newman wrote a metacompiler in March 1963 that utilized a CRT display on the time-sharing PDP-l; this compiler produced actual machine code rather than interpretive code and was partially bootstrapped from Meta I.
Schorre bootstrapped Meta II from Meta I during the Spring of 1963; the paper on the refined metacompiler system presented at the 1964 Philadelphia ACM conference is the first paper on a metacompiler available as a general reference. The syntax and implementation technique of Schorre's system laid the foundation for most of the systems that followed; the system was implemented on a small 1401, and was used to implement a small ALGOL-like language.
Many similar systems immediately followed.
Roger Rutman of A. C. Sparkplug developed and implemented LOGIK, a language for logical design simulation, on the IBM 7090 in January 1964; this compiler used an algorithm that produced efficient code for Boolean expressions.
Another paper in the 1964 ACM proceedings describes Meta III, developed by Schneider and Johnson at UCLA for the IBM 7090. Meta III represents an attempt to produce efficient machine code, for a large class of languages. Meta III was implemented completely in assembly language. Two compilers were written in Meta III, CODOL, a compiler-writing demonstration compiler, and PUREGOL, a dialect of ALGOL 60. (It was pure gall to call it ALGOL).
Late in 1964, Lee Schmidt bootstrapped the metacompiler EQGEN, from the PDP-l to the Beckman 420. EQGEN was a logic equation generating language.
In 1964, System Development Corporation began a major effort in the development of metacompilers; this effort includes powerful metacompilers, Bookl, and Book2 written in LISP which have extensive tree-searching and backup capability. An outgrowth of one of the Q-32 systems at SDC is Meta 5; the Meta 5 system incorporates backup of the input stream and enough other facilities to parse any context-sensitive language. This system was successfully released to a wide number of users and had many string-manipulation applications other than compiling, it has many elaborate push-down stacks, attribute setting and testing facilities, and output mechanisms. The fact that Meta 5 successfully translates JOVIAL programs to PL/l programs clearly demonstrates its power and flexibility.
The LOT system was developed during 1966 at Stanford Research Institute and was modeled very closely after Meta II, it had new special-purpose constructs allowing it to generate a compiler which would in turn be able to compile a subset of PL/l. This system had extensive statistic-gathering facilities and was used to study the characteristics of top-down analysis.
SIMPLE is a specialized translator system designed to aid the writing of pre-processors for PL/I, SIMPLE, written in PL/I, is composed of three components: An executive, a syntax analyzer and a semantic constructor.
The TREE META compiler was developed at Stanford Research Institute in Menlo Park, California. April 1968; the early metacompiler history is well documented in the TREE META manual. TREE META paralleled some of the SDC developments. Unlike earlier metacompilers it separated the semantics processing from the syntax processing; the syntax rules contained tree building operations that combined recognized language elements with tree nodes. The tree structure representation of the input was then processed by a simple form of unparse rules; the unparse rules used node recognition and attribute testing that when matched resulted in the associated action being performed. In addition like tree element could also be tested in an unparse rule. Unparse rules were also a recursive language being able to call unparse rules passing elements of thee tree before the action of the unparse rule was performed.
The concept of the metarnachine originally put forth by Glennie is so simple that three hardware versions have been designed and one actually implemented; the latter at Washington University in St. Louis; this machine was built from macro-modular components and has for instructions the codes described by Schorre.
CWIC (Compiler for Writing and Implementing Compilers) is the last known Schorre metacompiler, it was developed at Systems Development Corporation by Erwin Book, Dewey Val Schorre and Steven J. Sherman With the full power of (lisp 2) a list processing language optimizing algorithms could operate on syntax generated lists and trees before code generation. CWIC also had a symbol table built into the language.
With the resurgence of domain-specific languages and the need for parser generators which are easy to use, easy to understand, and easy to maintain, metacompilers are becoming a valuable tool for advanced software engineering projects.
Other examples of parser generators in the yacc vein are ANTLR, Coco/R, CUP, GNU bison, Eli, FSL, SableCC, SID (Syntax Improving Device), and JavaCC. While useful, pure parser generators only address the parsing part of the problem of building a compiler. Tools with broader scope, such as PQCC, Coco/R and DMS Software Reengineering Toolkit provide considerable support for more difficult post-parsing activities such as semantic analysis, code optimization and generation.
The earliest Schorre metacompilers, META I and META II, were developed by D. Val Schorre at UCLA. Other Schorre based metacompilers followed; each adding improvements to language analysis and/or code generation.
In programming it is common to use the programming language name to refer to both the compiler and the programming language, the context distinguishing the meaning. A C++ program is compiled using a C++ compiler; that also applies in the following. For example, META II is both the compiler and the language.
The metalanguages in the Schorre line of metacompilers are functional programming languages that use top down grammar analyzing syntax equations having embedded output transformation constructs.
A syntax equation:
<name> = <body>;
is a compiled test function returning success or failure. <name> is the function name. <body> is a form of logical expression consisting of tests that may be grouped, have alternates, and output productions. A test is like a bool in other languages, success being true and failure being false.
Defining a programming language analytically top down is natural. For example, a program could be defined as:
program = $declarations;
Defining a program as a sequence of zero or more declarations.
In the Schorre META X languages there is a driving rule. The program rule above is an example of a driving rule; the program rule is a test function that calls declaration, a test rule, that returns success or failure. The $ loop operator repeatedly calling declarations until failure is returned; the $ operator is always successful, even when zero declarations occur. Above program would always return success.
The character sets of these early compilers were limited; the character / was used for the alternant (or) operator. "A or B" is written as A / B. Parentheses ( ) are used for grouping.
A (B / C)
Describes a construct of A followed by B or C; as a boolean expression it would be
A and (B or C)
A sequence X Y has an implied X and Y meaning. ( ) are grouping and / the or operator. The order of evaluation is always left to right as an input character sequence is being specified by the ordering of the tests.
Special operator words whose first character is a "." are used for clarity. .EMPTY is used as the last alternate when no previous alternant need be present.
X (A / B / .EMPTY)
Indicates that X is optionally followed by A or B. This is a specific characteristic of these metalanguages being programming languages. Backtracking is avoided by the above. Other compiler constructor systems may have declared the three possible sequences and left it up to the parser to figure it out.
The characteristics of the metaprogramming metalanguages above are common to all Schorre metacompilers and those derived from them.
META I was a hand compiled metacompiler used to compile META II. Little else is known of META I except that the initial compilation of META II produced nearly identical code to that of the hand coded META I compiler.
Each rule consists optionally of tests, operators, and output productions. A rule matches some part of the input program source character stream returning success or failure. On success the input is advanced over matched characters. On failure the input is not advanced.
Output productions produced a form of assembly code directly from a syntax rule.
TREE-META introduced tree building operators :<node_name> and [<number>] moving the output production transforms to unparse rules. The tree building operators were used in the grammar rules directly transforming the input into an abstract syntax tree. Unparse rules are also test functions that matched tree patterns. Unparse rules are called from a grammar rule when an abstract syntax tree is to be transformed into output code; the building of an abstract syntax tree and unparse rules allowed local optimizations to be performed by analyzing the parse tree.
Moving of output productions to the unparse rules made a clear separation of grammar analysis and code production; this made the programming easier to read and understand.
In 1968-1970, Erwin Book, Dewey Val Schorre, and Steven J. Sherman developed CWIC. (Compiler for Writing and Implementing Compilers) at System Development Corporation Charles Babbage Institute Center for the History of Information Technology (Box 12, folder 21),
CWIC is a meta-compiler system composed of three special-purpose languages, each intended to permit the description of certain aspects of translation in a straight forward, natural manner; the syntax language is used to describe the recognition of source text and the construction from it to an intermediate tree structure. The generator language is used to describe the transformation of the tree into appropriate object language.
The syntax language is similar to that of Dewey Val Schorre's line of metacompilers, it most resembles TREEMETA having tree building operations in the syntax language. The unparse rules of TREEMETA are extended to work with the object oriented generator language based on LISP 2.
CWIC includes three languages:
- Syntax: Transforms the source program input, into list structures using specialized grammar equations. A parsed expression structure is passed to a generator by placement of a generator call in a rule. A tree is a list whose first element is a node; the language has operators, < and >, specifically for making lists. The colon : operator is used to create node objects. :ADD creates an ADD node. The exclamation ! operator combines a number of parsed entries with the most previous created node to make a tree . Trees created by syntax rules are passed to generator functions, returning success or failure; the syntax language is very close to TREEMETA. Both use a colon to create a node. CWIC's tree building exclamation !<number> functions the same as TREEMETA's [<number>].
- Generator: a named series of transforming rules, each consisting of an unparse, pattern matching, rule. and an output production written in a LISP 2 like language. the translation was to IBM 360 binary machine code. Other facilities of the generator language generalized output.
- MOL-360: an independent mid level implementation language for the IBM System/360 family of computers developed in 1968 and used for writing the underlying support library.
function-name(first-unparse_rule) => first-production_code_generator (second-unparse_rule) => second-production_code_generator (third-unparse_rule) => third-production_code_generator ...
The code to process a given tree included the features of a general purpose programming language, plus a form: <stuff>, which would emit (stuff) onto the output file. A generator call may be used in the unparse_rule; the generator is passed the element of unparse_rule pattern in which it is placed and return values are listed in (). For example:
expr_gen(ADD[expr_gen(x),expr_gen(y)]) => <AR + (x*16)+y;> releasereg(y); return x; (SUB[expr_gen(x),expr_gen(y)])=> <SR + (x*16)+y;> releasereg(y); return x; (MUL[expr_gen(x),expr_gen(y)])=> . . . (x)=> r1 = getreg(); load(r1, x); return r1; ...
That is, if the parse tree looks like (ADD[<something1>,<something2>]), expr_gen(x) would be called with <something1> and return x. A variable in the unparse rule is a local variable that can be used in the production_code_generator. expr_gen(y) is called with <something2> and returns y. Of note here is a generator call in an unparse rule is passed the element in the position it occupies. Hopefully in the above x and y will be registers on return; the last transforms is intended to load an atomic into a register and return the register. The first production would be used to generate the 360 "AR" (Add Register) instruction with the appropriate values in general registers; the above example is only a part of a generator. Every generator expression evaluates to a value that con then be further processed; the last transform could just as well have been written as:
(x)=> return load(getreg(), x);
In this case load returns its first parameter, the register returned by getreg(). the functions load and getreg are other CWIC generators.
CWIC addressed domain-specific languages before the term domain-specific language existed
From the authors of CWIC:
"A metacompiler assists the task of compiler-building by automating its noncreative aspects, those aspects that are the same regardless of the language which the produced compiler is to translate. This makes possible the design of languages which are appropriate to the specification of a particular problem, it reduces the cost of producing processors for such languages to a point where it becomes economically feasible to begin the solution of a problem with language design."
- DMS Software Reengineering Toolkit, a program transformation system with parser generators.
- Epsilon Grammar Studio
- LRStar: LR(*) parser generator
- META II
- parboiled, a Java library for building parsers.
- Packrat parser
- PackCC, a packrat parser with left recursion support.
- PQCC, a compiler-compiler that is more than a parser generator.
- Syntax Improving Device (SID)
- SYNTAX, an integrated toolset for compiler construction.
- Parsing expression grammar
- LL parser
- LR parser
- Simple LR parser
- LALR parser
- GLR parser
- Domain analysis
- Domain-specific language
- History of compiler construction
- Program transformation
References and notes
- "A SYNTAX DIRECTED COMPILER FOR ALGOL 60" Edgar T. Irons, Communications of the ACM Volume 4 Issue 1, Jan. 1961."
- Metacompiler: (computer science) A compiler that is used chiefly to construct compilers for other programming languages."Sci-Tech Dictionary McGraw-Hill Dictionary of Scientific and Technical Terms, 6th edition, published by The McGraw-Hill Companies, Inc".
- Wikipedia, the free encyclopedia - Metaprogramming
- Book, Erwin; Dewey Val Schorre; Steven J. Sherman (June 1970). "The CWIC/36O system, a compiler for writing and implementing compilers". ACM SIGPLAN Notices. 5 (6): 11–29. doi:10.1145/954344.954345.
- C. Stephen Carr, David A. Luther, Sherian Erdmann, The TREE-META Compiler-Compiler System: A Meta Compiler System for the Univac 1108 and General Electric 645, University of Utah Technical Report RADC-TR-69-83.
- Peter Mosses, "SIS: A Compiler-Generator System Using Denotational Semantics," Report 78-4-3, Dept. of Computer Science, University of Aarhus, Denmark, June 1978
- Neighbors, J. M. Software Construction using Components. Technical Report 160, Department of Information and Computer Sciences, University of California, Irvine, 1980.
- Howard Metcalfe, "A Parameterized Compiler Based on Mechanical Linguistics" Planning Research Corporation R-311, March 1, 1963, also in Annual Review in Automatic Programming, Vol. 4
- Robert Ledley and J. B. Wilson, "Automatic Programming, Language Translation Through Syntactical Analysis", Communications of the Association for Computing Machinery, Vol. 5, No. 3 pp. 145–155, March 1962.
- A. E. Glennie, "On the Syntax Machine and the Construction of a Universal Computer", Technical Report Number 2, AD 240–512, Computation Center, Carnegie Institute of Technology, 1960.
- Schorre, D. V., META II a syntax-oriented compiler writing language, Proceedings of the 1964 19th ACM National Conference, pp. 41.301-41.3011, 1964
- Dewey, Val Schorre (1963). "A Syntax - Directed SMALGOL for the 1401". ACM Natl. Conf., Denver, Colo.
- Meta I is described in the paper given at the 1963 Colorado ACM conference. See SMALGOL.
- L. O. Schmidt, "The Status Bitt ACM SegPlan "Special Interest Group on Programming Languages" Working Group 1 News Letter, 1964.
- Roger Rutman, "LOGIK. A Syntax Directed Compiler for Computer Bit-Time Simulation", Master thesis, UCLA, August 1964.
- F. W. Schneider and (G. D. Johnson, "A Syntax-Directed Compiler-writing, Compiler to generate Efficient Code", Proceedings of the 19th National Conference of the Association for Computing Machinery, 1964
- D. Oppenheim and D. Haggerty, "META 5: A Tool to Manipulate Strings of Data", Proceedings of the 21st National Conference of the Association for Computing Machinery, 1966.
- Charles R. Kirkley and Johns F. Rulifson, "The LOT System of Syntax Directed Compiling", Stanford Research Institute Internal Report ISR 187531-139, 1966.
- George J. E. (1967a). Syntax Analyzer, Recognizer, Parser and Semantic interpretation System, Stanford Linear Accelerator Center, November 15, 1967.
- Robert W. Gray et al., "Eli: A complete, flexible compiler construction system." Communications of the ACM 35:2:121-130, 1992; see also http://eli.colorado.edu/
- J.M. Foster, "A syntax improving program." The Computer Journal 11:1:31-34, 1968
- Brooker, R .A.; MacCallum, I. R.; Morris, D.; Rohl, J. S. (1963), "The compiler-compiler", Annual Review in Automatic Programming, 3: 229–275, doi:10.1016/S0066-4138(63)80009-9
- Brooker, R. A., Morris, D. and Rohl, J. S., Experience with the Compiler Compiler, Computer Journal, Vol. 9, p. 350. (February 1967).
- Johnson, Stephen C., Yacc—yet another compiler-compiler, Computer Science Technical Report 32, Bell Laboratories, Murray Hill, NJ, July 1975
- McKeeman, William M.; Horning, James J.; Wortman, David B. (1970). A Compiler Generator. Englewood Cliffs, N.J.: Prentice-Hall. ISBN 978-0-13-155077-3. Retrieved 13 December 2012.