Sei sulla pagina 1di 85

Compiler Design

Rakesh KR

PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information. PDF generated at: Sat, 20 Oct 2012 01:29:53 UTC

Contents
Articles
Compiler Lexical analysis Parsing Top-down parsing Bottom-up parsing Recursive descent parser LR parser Syntax-directed translation Context-free grammar Run-time system Code generation (compiler) Program optimization Optimizing compiler Compiler-compiler 1 9 13 18 20 22 27 45 45 57 58 60 66 77

References
Article Sources and Contributors Image Sources, Licenses and Contributors 80 82

Article Licenses
License 83

Compiler

Compiler
A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program. The name "compiler" is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language or machine code). If the compiled program can run on a computer whose CPU or operating system is different from the one on which the compiler runs, the compiler is known as a cross-compiler. A program that translates from a low level language to a higher level one is a decompiler. A program that translates between high-level languages is A diagram of the operation of a typical multi-language, multi-target compiler usually called a language translator, source to source translator, or language converter. A language rewriter is usually a program that translates the form of expressions without a change of language. A compiler is likely to perform many or all of the following operations: lexical analysis, preprocessing, parsing, semantic analysis (Syntax-directed translation), code generation, and code optimization. Program faults caused by incorrect compiler behavior can be very difficult to track down and work around; therefore, compiler implementors invest significant effort to ensure the correctness of their software. The term compiler-compiler is sometimes used to refer to a parser generator, a tool often used to help create the lexer and parser.

History
Software for early computers was primarily written in assembly language. Higher level programming languages were not invented until the benefits of being able to reuse software on different kinds of CPUs started to become significantly greater than the costs of writing a compiler. The limited memory capacity of early computers led to substantial technical challenges when designing the first compilers. Towards the end of the 1950s machine-independent programming languages were first proposed. Subsequently several experimental compilers were developed. The first compiler was written by Grace Hopper, in 1952, for the A-0 programming language. The FORTRAN team led by John Backus at IBM is generally credited as having introduced the first complete compiler in 1957. COBOL was an early language to be compiled on multiple architectures, in 1960.[1]

Compiler In many application domains the idea of using a higher level language quickly caught on. Because of the expanding functionality supported by newer programming languages and the increasing complexity of computer architectures, compilers have become more complex. Early compilers were written in assembly language. The first self-hosting compiler capable of compiling its own source code in a high-level language was created in 1962 for Lisp by Tim Hart and Mike Levin at MIT.[2] Since the 1970s it has become common practice to implement a compiler in the language it compiles, although both Pascal and C have been popular choices for implementation language. Building a self-hosting compiler is a bootstrapping problemthe first such compiler for a language must be compiled either by hand or by a compiler written in a different language, or (as in Hart and Levin's Lisp compiler) compiled by running the compiler in an interpreter.

Compilers in education
Compiler construction and compiler optimization are taught at universities and schools as part of a computer science curriculum.[3] Such courses are usually supplemented with the implementation of a compiler for an educational programming language. A well-documented example is Niklaus Wirth's PL/0 compiler, which Wirth used to teach compiler construction in the 1970s.[4] In spite of its simplicity, the PL/0 compiler introduced several influential concepts to the field: 1. Program development by stepwise refinement (also the title of a 1971 paper by Wirth)[5] 2. 3. 4. 5. The use of a recursive descent parser The use of EBNF to specify the syntax of a language A code generator producing portable P-code The use of T-diagrams[6] in the formal description of the bootstrapping problem

Compilation
Compilers enabled the development of programs that are machine-independent. Before the development of FORTRAN (FORmula TRANslator), the first higher-level language, in the 1950s, machine-dependent assembly language was widely used. While assembly language produces more reusable and relocatable programs than machine code on the same architecture, it has to be modified or rewritten if the program is to be executed on different computer hardware architecture. With the advance of high-level programming languages that followed FORTRAN, such as COBOL, C, and BASIC, programmers could write machine-independent source programs. A compiler translates the high-level source programs into target programs in machine languages for the specific hardwares. Once the target program is generated, the user can execute the program.

Structure of a compiler
Compilers bridge source programs in high-level languages with the underlying hardware. A compiler requires 1) determining the correctness of the syntax of programs, 2) generating correct and efficient object code, 3) run-time organization, and 4) formatting output according to assembler and/or linker conventions. A compiler consists of three main parts: the frontend, the middle-end, and the backend. The front end checks whether the program is correctly written in terms of the programming language syntax and semantics. Here legal and illegal programs are recognized. Errors are reported, if any, in a useful way. Type checking is also performed by collecting type information. The frontend then generates an intermediate representation or IR of the source code for processing by the middle-end. The middle end is where optimization takes place. Typical transformations for optimization are removal of useless or unreachable code, discovery and propagation of constant values, relocation of computation to a less frequently executed place (e.g., out of a loop), or specialization of computation based on the context. The middle-end generates

Compiler another IR for the following backend. Most optimization efforts are focused on this part. The back end is responsible for translating the IR from the middle-end into assembly code. The target instruction(s) are chosen for each IR instruction. Register allocation assigns processor registers for the program variables where possible. The backend utilizes the hardware by figuring out how to keep parallel execution units busy, filling delay slots, and so on. Although most algorithms for optimization are in NP, heuristic techniques are well-developed.

Compiler output
One classification of compilers is by the platform on which their generated code executes. This is known as the target platform. A native or hosted compiler is one which output is intended to directly run on the same type of computer and operating system that the compiler itself runs on. The output of a cross compiler is designed to run on a different platform. Cross compilers are often used when developing software for embedded systems that are not intended to support a software development environment. The output of a compiler that produces code for a virtual machine (VM) may or may not be executed on the same platform as the compiler that produced it. For this reason such compilers are not usually classified as native or cross compilers. The lower level language that is the target of a compiler may itself be a high-level programming language. C, often viewed as some sort of portable assembler, can also be the target language of a compiler. E.g.: Cfront, the original compiler for C++ used C as target language. The C created by such a compiler is usually not intended to be read and maintained by humans. So indent style and pretty C intermediate code are irrelevant. Some features of C turn it into a good target language. E.g.: C code with #line directives can be generated to support debugging of the original source.

Compiled versus interpreted languages


Higher-level programming languages usually appear with a type of translation in mind: either designed as compiled language or interpreted language. However, in practice there is rarely anything about a language that requires it to be exclusively compiled or exclusively interpreted, although it is possible to design languages that rely on re-interpretation at run time. The categorization usually reflects the most popular or widespread implementations of a language for instance, BASIC is sometimes called an interpreted language, and C a compiled one, despite the existence of BASIC compilers and C interpreters. Interpretation does not replace compilation completely. It only hides it from the user and makes it gradual. Even though an interpreter can itself be interpreted, a directly executed program is needed somewhere at the bottom of the stack (see machine language). Modern trends toward just-in-time compilation and bytecode interpretation at times blur the traditional categorizations of compilers and interpreters. Some language specifications spell out that implementations must include a compilation facility; for example, Common Lisp. However, there is nothing inherent in the definition of Common Lisp that stops it from being interpreted. Other languages have features that are very easy to implement in an interpreter, but make writing a compiler much harder; for example, APL, SNOBOL4, and many scripting languages allow programs to construct arbitrary source code at runtime with regular string operations, and then execute that code by passing it to a special evaluation function. To implement these features in a compiled language, programs must usually be shipped with a runtime library that includes a version of the compiler itself.

Compiler

Hardware compilation
The output of some compilers may target computer hardware at a very low level, for example a Field Programmable Gate Array (FPGA) or structured Application-specific integrated circuit (ASIC). Such compilers are said to be hardware compilers or synthesis tools because the source code they compile effectively controls the final configuration of the hardware and how it operates; the output of the compilation is not instructions that are executed in sequence - only an interconnection of transistors or lookup tables. For example, XST is the Xilinx Synthesis Tool used for configuring FPGAs. Similar tools are available from Altera, Synplicity, Synopsys and other vendors.

Compiler construction
In the early days, the approach taken to compiler design used to be directly affected by the complexity of the processing, the experience of the person(s) designing it, and the resources available. A compiler for a relatively simple language written by one person might be a single, monolithic piece of software. When the source language is large and complex, and high quality output is required, the design may be split into a number of relatively independent phases. Having separate phases means development can be parceled up into small parts and given to different people. It also becomes much easier to replace a single phase by an improved one, or to insert new phases later (e.g., additional optimizations). The division of the compilation processes into phases was championed by the Production Quality Compiler-Compiler Project (PQCC) at Carnegie Mellon University. This project introduced the terms front end, middle end, and back end. All but the smallest of compilers have more than two phases. However, these phases are usually regarded as being part of the front end or the back end. The point at which these two ends meet is open to debate. The front end is generally considered to be where syntactic and semantic processing takes place, along with translation to a lower level of representation (than source code). The middle end is usually designed to perform optimizations on a form other than the source code or machine code. This source code/machine code independence is intended to enable generic optimizations to be shared between versions of the compiler supporting different languages and target processors. The back end takes the output from the middle. It may perform more analysis, transformations and optimizations that are for a particular computer. Then, it generates code for a particular processor and OS. This front-end/middle/back-end approach makes it possible to combine front ends for different languages with back ends for different CPUs. Practical examples of this approach are the GNU Compiler Collection, LLVM, and the Amsterdam Compiler Kit, which have multiple front-ends, shared analysis and multiple back-ends.

One-pass versus multi-pass compilers


Classifying compilers by number of passes has its background in the hardware resource limitations of computers. Compiling involves performing lots of work and early computers did not have enough memory to contain one program that did all of this work. So compilers were split up into smaller programs which each made a pass over the source (or some representation of it) performing some of the required analysis and translations. The ability to compile in a single pass has classically been seen as a benefit because it simplifies the job of writing a compiler and one-pass compilers generally perform compilations faster than multi-pass compilers. Thus, partly driven by the resource limitations of early systems, many early languages were specifically designed so that they could be compiled in a single pass (e.g., Pascal). In some cases the design of a language feature may require a compiler to perform more than one pass over the source. For instance, consider a declaration appearing on line 20 of the source which affects the translation of a statement appearing on line 10. In this case, the first pass needs to gather information about declarations appearing after statements that they affect, with the actual translation happening during a subsequent pass.

Compiler The disadvantage of compiling in a single pass is that it is not possible to perform many of the sophisticated optimizations needed to generate high quality code. It can be difficult to count exactly how many passes an optimizing compiler makes. For instance, different phases of optimization may analyse one expression many times but only analyse another expression once. Splitting a compiler up into small programs is a technique used by researchers interested in producing provably correct compilers. Proving the correctness of a set of small programs often requires less effort than proving the correctness of a larger, single, equivalent program. While the typical multi-pass compiler outputs machine code from its final pass, there are several other types: A "source-to-source compiler" is a type of compiler that takes a high level language as its input and outputs a high level language. For example, an automatic parallelizing compiler will frequently take in a high level language program as an input and then transform the code and annotate it with parallel code annotations (e.g. OpenMP) or language constructs (e.g. Fortran's DOALL statements). Stage compiler that compiles to assembly language of a theoretical machine, like some Prolog implementations This Prolog machine is also known as the Warren Abstract Machine (or WAM). Bytecode compilers for Java, Python, and many more are also a subtype of this. Just-in-time compiler, used by Smalltalk and Java systems, and also by Microsoft .NET's Common Intermediate Language (CIL) Applications are delivered in bytecode, which is compiled to native machine code just prior to execution.

Front end
The front end analyzes the source code to build an internal representation of the program, called the intermediate representation or IR. It also manages the symbol table, a data structure mapping each symbol in the source code to associated information such as location, type and scope. This is done over several phases, which includes some of the following: 1. Line reconstruction. Languages which strop their keywords or allow arbitrary spaces within identifiers require a phase before parsing, which converts the input character sequence to a canonical form ready for the parser. The top-down, recursive-descent, table-driven parsers used in the 1960s typically read the source one character at a time and did not require a separate tokenizing phase. Atlas Autocode, and Imp (and some implementations of ALGOL and Coral 66) are examples of stropped languages which compilers would have a Line Reconstruction phase. 2. Lexical analysis breaks the source code text into small pieces called tokens. Each token is a single atomic unit of the language, for instance a keyword, identifier or symbol name. The token syntax is typically a regular language, so a finite state automaton constructed from a regular expression can be used to recognize it. This phase is also called lexing or scanning, and the software doing lexical analysis is called a lexical analyzer or scanner. 3. Preprocessing. Some languages, e.g., C, require a preprocessing phase which supports macro substitution and conditional compilation. Typically the preprocessing phase occurs before syntactic or semantic analysis; e.g. in the case of C, the preprocessor manipulates lexical tokens rather than syntactic forms. However, some languages such as Scheme support macro substitutions based on syntactic forms. 4. Syntax analysis involves parsing the token sequence to identify the syntactic structure of the program. This phase typically builds a parse tree, which replaces the linear sequence of tokens with a tree structure built according to the rules of a formal grammar which define the language's syntax. The parse tree is often analyzed, augmented, and transformed by later phases in the compiler. 5. Semantic analysis is the phase in which the compiler adds semantic information to the parse tree and builds the symbol table. This phase performs semantic checks such as type checking (checking for type errors), or object binding (associating variable and function references with their definitions), or definite assignment (requiring all local variables to be initialized before use), rejecting incorrect programs or issuing warnings. Semantic analysis

Compiler usually requires a complete parse tree, meaning that this phase logically follows the parsing phase, and logically precedes the code generation phase, though it is often possible to fold multiple phases into one pass over the code in a compiler implementation.

Back end
The term back end is sometimes confused with code generator because of the overlapped functionality of generating assembly code. Some literature uses middle end to distinguish the generic analysis and optimization phases in the back end from the machine-dependent code generators. The main phases of the back end include the following: 1. Analysis: This is the gathering of program information from the intermediate representation derived from the input. Typical analyses are data flow analysis to build use-define chains, dependence analysis, alias analysis, pointer analysis, escape analysis etc. Accurate analysis is the basis for any compiler optimization. The call graph and control flow graph are usually also built during the analysis phase. 2. Optimization: the intermediate language representation is transformed into functionally equivalent but faster (or smaller) forms. Popular optimizations are inline expansion, dead code elimination, constant propagation, loop transformation, register allocation and even automatic parallelization. 3. Code generation: the transformed intermediate language is translated into the output language, usually the native machine language of the system. This involves resource and storage decisions, such as deciding which variables to fit into registers and memory and the selection and scheduling of appropriate machine instructions along with their associated addressing modes (see also Sethi-Ullman algorithm). Debug data may also need to be generated to facilitate debugging. Compiler analysis is the prerequisite for any compiler optimization, and they tightly work together. For example, dependence analysis is crucial for loop transformation. In addition, the scope of compiler analysis and optimizations vary greatly, from as small as a basic block to the procedure/function level, or even over the whole program (interprocedural optimization). Obviously, a compiler can potentially do a better job using a broader view. But that broad view is not free: large scope analysis and optimizations are very costly in terms of compilation time and memory space; this is especially true for interprocedural analysis and optimizations. Interprocedural analysis and optimizations are common in modern commercial compilers from HP, IBM, SGI, Intel, Microsoft, and Sun Microsystems. The open source GCC was criticized for a long time for lacking powerful interprocedural optimizations, but it is changing in this respect. Another open source compiler with full analysis and optimization infrastructure is Open64, which is used by many organizations for research and commercial purposes. Due to the extra time and space needed for compiler analysis and optimizations, some compilers skip them by default. Users have to use compilation options to explicitly tell the compiler which optimizations should be enabled.

Compiler correctness
Compiler correctness is the branch of software engineering that deals with trying to show that a compiler behaves according to its language specification. Techniques include developing the compiler using formal methods and using rigorous testing (often called compiler validation) on an existing compiler.

Related techniques
Assembly language is a type of low-level language and a program that compiles it is more commonly known as an assembler, with the inverse program known as a disassembler. A program that translates from a low level language to a higher level one is a decompiler.

Compiler A program that translates between high-level languages is usually called a language translator, source to source translator, language converter, or language rewriter. The last term is usually applied to translations that do not involve a change of language. A program that translates into an object code format that is not supported on the compilation machine is called a cross compiler and is commonly used to prepare code for embedded applications.

International conferences and organizations


Every year, the European Joint Conferences on Theory and Practice of Software (ETAPS) sponsors the International Conference on Compiler Construction (CC), with papers from both the academic and industrial sectors.[7]

Notes
[1] "IP: The World's First COBOL Compilers" (http:/ / www. interesting-people. org/ archives/ interesting-people/ 199706/ msg00011. html). interesting-people.org. 12 June 1997. . [2] T. Hart and M. Levin. "The New Compiler, AIM-39 - CSAIL Digital Archive - Artificial Intelligence Laboratory Series" (ftp:/ / publications. ai. mit. edu/ ai-publications/ pdf/ AIM-039. pdf). publications.ai.mit.edu. . [3] Chakraborty, P., Saxena, P. C., Katti, C. P., Pahwa, G., Taneja, S. A new practicum in compiler construction. Computer Applications in Engineering Education, In Press. http:/ / onlinelibrary. wiley. com/ doi/ 10. 1002/ cae. 20566/ pdf [4] "The PL/0 compiler/interpreter" (http:/ / www. 246. dk/ pl0. html). . [5] "The ACM Digital Library" (http:/ / www. acm. org/ classics/ dec95/ ). . [6] T diagrams were first introduced for describing bootstrapping and cross-compiling compilers in McKeeman et al. A Compiler Generator (1971). Conway described the broader concept before that with his UNCOL in 1958, to which Bratman added in 1961: H. Bratman, An alternate form of the UNCOL diagram, Comm. ACM 4 (March 1961) 3, p. 142. Later on, others, including P.D. Terry, gave an explanation and usage of T-diagrams in their textbooks on the topic of compiler construction. Cf. Terry, 1997, Chapter 3 (http:/ / scifac. ru. ac. za/ compilers/ cha03g. htm). T-diagrams are also now used to describe client-server interconnectivity on the World Wide Web: cf. Patrick Closhen, et al. 1997: T-Diagrams as Visual Language to Illustrate WWW Technology (http:/ / pu. rbg. informatik. tu-darmstadt. de/ docs/ HJH-19990217-etal-T-diagrams. doc), Darmstadt University of Technology, Darmstadt, Germany [7] ETAPS (http:/ / www. etaps. org/ ) - European Joint Conferences on Theory and Practice of Software. Cf. "CC" (Compiler Construction) subsection.

References
Compiler textbook references (http://www.informatik.uni-trier.de/~ley/db/books/compiler/index.html) A collection of references to mainstream Compiler Construction Textbooks Aho, Alfred V.; Sethi, Ravi; and Ullman, Jeffrey D., Compilers: Principles, Techniques and Tools (ISBN 0-201-10088-6) link to publisher (http://www.aw.com/catalog/academic/product/0,4096,0201100886,00. html). Also known as The Dragon Book. Allen, Frances E., "A History of Language Processor Technology in IBM" (http://www.research.ibm.com/ journal/rd/255/ibmrd2505Q.pdf), IBM Journal of Research and Development, v.25, no.5, September 1981. Allen, Randy; and Kennedy, Ken, Optimizing Compilers for Modern Architectures, Morgan Kaufmann Publishers, 2001. ISBN 1-55860-286-0 Appel, Andrew Wilson Modern Compiler Implementation in Java, 2nd edition. Cambridge University Press, 2002. ISBN 0-521-82060-X Modern Compiler Implementation in ML (http://books.google.com/books?id=8APOYafUt-oC& printsec=frontcover), Cambridge University Press, 1998. ISBN 0-521-58274-1 Bornat, Richard, Understanding and Writing Compilers: A Do It Yourself Guide (http://www.cs.mdx.ac.uk/ staffpages/r_bornat/books/compiling.pdf), Macmillan Publishing, 1979. ISBN 0-333-21732-2 Cooper, Keith D., and Torczon, Linda, Engineering a Compiler, Morgan Kaufmann, 2004, ISBN 1-55860-699-8.

Compiler Leverett; Cattel; Hobbs; Newcomer; Reiner; Schatz; Wulf, An Overview of the Production Quality Compiler-Compiler Project, in Computer 13(8):38-49 (August 1980) McKeeman, William Marshall; Horning, James J.; Wortman, David B., A Compiler Generator (http://www.cs. toronto.edu/XPL/), Englewood Cliffs, N.J. : Prentice-Hall, 1970. ISBN 0-13-155077-2 Muchnick, Steven, Advanced Compiler Design and Implementation (http://books.google.com/ books?id=Pq7pHwG1_OkC&printsec=frontcover&source=gbs_summary_r&cad=0), Morgan Kaufmann Publishers, 1997. ISBN 1-55860-320-4 Scott, Michael Lee, Programming Language Pragmatics (http://books.google.com/ books?id=4LMtA2wOsPcC&printsec=frontcover&dq=Programming+Language+Pragmatics), Morgan Kaufmann, 2005, 2nd edition, 912 pages. ISBN 0-12-633951-1 ( The author's site on this book (http://www.cs. rochester.edu/~scott/pragmatics/)). Srikant, Y. N.; Shankar, Priti, The Compiler Design Handbook: Optimizations and Machine Code Generation (http://books.google.com/books?id=0K_jIsgyNpoC&printsec=frontcover), CRC Press, 2003. ISBN 0-8493-1240-X Terry, Patrick D., Compilers and Compiler Generators: An Introduction with C++ (http://scifac.ru.ac.za/ compilers/conts.htm), International Thomson Computer Press, 1997. ISBN 1-85032-298-8, Wirth, Niklaus, Compiler Construction (http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf) (ISBN 0-201-40353-6), Addison-Wesley, 1996, 176 pages. Revised November 2005.

External links
Compilers (http://www.dmoz.org/Computers/Programming/Compilers//) at the Open Directory Project Compile-Howto (http://la-samhna.de/library/compile/index.html) Basics of Compiler Design (http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/) by Torben gidius Mogensen Short animation (http://www.youtube.com/watch?v=_C5AHaS1mOA) explaining the key conceptual difference between compilers and interpreters

Lexical analysis

Lexical analysis
In computer science, lexical analysis is the process of converting a sequence of characters into a sequence of tokens. A program or function which performs lexical analysis is called a lexical analyzer, lexer, or scanner. A lexer often exists as a single function which is called by a parser or another function.

Lexical grammar
The specification of a programming language often includes a set of rules which defines the lexer. These rules usually consist of regular expressions, and they define the set of possible character sequences that are used to form individual tokens or lexemes. In programming languages that delimit blocks with tokens (e.g., "{" and "}"), as opposed to off-side rule languages that delimit blocks with indentation, white space is also defined by a regular expression and influences the recognition of other tokens but does not itself contribute tokens. White space is said to be non-significant in such languages.

Token
A token is a string of characters, categorized according to the rules as a symbol (e.g., IDENTIFIER, NUMBER, COMMA). The process of forming tokens from an input stream of characters is called tokenization, and the lexer categorizes them according to a symbol type. A token can look like anything that is useful for processing an input text stream or text file. A lexical analyzer generally does nothing with combinations of tokens, a task left for a parser. For example, a typical lexical analyzer recognizes parentheses as tokens, but does nothing to ensure that each "(" is matched with a ")". Consider this expression in the C programming language: sum=3+2; Tokenized in the following table:
Lexeme sum = 3 + 2 ; Token type Identifier Assignment operator Integer literal Addition operator Integer literal End of statement

Tokens are frequently defined by regular expressions, which are understood by a lexical analyzer generator such as lex. The lexical analyzer (either generated automatically by a tool like lex, or hand-crafted) reads in a stream of characters, identifies the lexemes in the stream, and categorizes them into tokens. This is called "tokenizing." If the lexer finds an invalid token, it will report an error. Following tokenizing is parsing. From there, the interpreted data may be loaded into data structures for general use, interpretation, or compiling.

Lexical analysis

10

Scanner
The first stage, the scanner, is usually based on a finite-state machine (FSM). It has encoded within it information on the possible sequences of characters that can be contained within any of the tokens it handles (individual instances of these character sequences are known as lexemes). For instance, an integer token may contain any sequence of numerical digit characters. In many cases, the first non-whitespace character can be used to deduce the kind of token that follows and subsequent input characters are then processed one at a time until reaching a character that is not in the set of characters acceptable for that token (this is known as the maximal munch rule, or longest match rule). In some languages, the lexeme creation rules are more complicated and may involve backtracking over previously read characters.

Tokenizer
Tokenization is the process of demarcating and possibly classifying sections of a string of input characters. The resulting tokens are then passed on to some other form of processing. The process can be considered a sub-task of parsing input. Take, for example, The quick brown fox jumps over the lazy dog The string isn't implicitly segmented on spaces, as an English speaker would do. The raw input, the 43 characters, must be explicitly split into the 9 tokens with a given space delimiter (i.e. matching the string " " or regular expression /\s{1}/). The tokens could be represented in XML, <sentence> <word>The</word> <word>quick</word> <word>brown</word> <word>fox</word> <word>jumps</word> <word>over</word> <word>the</word> <word>lazy</word> <word>dog</word> </sentence> Or an s-expression, (sentence ((word The) (word quick) (word brown) (word fox) (word jumps) (word over) (word the) (word lazy) (word dog))) A lexeme, however, is only a string of characters known to be of a certain kind (e.g., a string literal, a sequence of letters). In order to construct a token, the lexical analyzer needs a second stage, the evaluator, which goes over the characters of the lexeme to produce a value. The lexeme's type combined with its value is what properly constitutes a token, which can be given to a parser. (Some tokens such as parentheses do not really have values, and so the evaluator function for these can return nothing. The evaluators for integers, identifiers, and strings can be considerably more complex. Sometimes evaluators can suppress a lexeme entirely, concealing it from the parser, which is useful for whitespace and comments.)

Lexical analysis For example, in the source code of a computer program, the string net_worth_future = (assets - liabilities); might be converted (with whitespace suppressed) into the lexical token stream: NAME "net_worth_future" EQUALS OPEN_PARENTHESIS NAME "assets" MINUS NAME "liabilities" CLOSE_PARENTHESIS SEMICOLON Though it is possible and sometimes necessary, due to licensing restrictions of existing parsers or if the list of tokens is small, to write a lexer by hand, lexers are often generated by automated tools. These tools generally accept regular expressions that describe the tokens allowed in the input stream. Each regular expression is associated with a production rule in the lexical grammar of the programming language that evaluates the lexemes matching the regular expression. These tools may generate source code that can be compiled and executed or construct a state table for a finite-state machine (which is plugged into template code for compilation and execution). Regular expressions compactly represent patterns that the characters in lexemes might follow. For example, for an English-based language, a NAME token might be any English alphabetical character or an underscore, followed by any number of instances of any ASCII alphanumeric character or an underscore. This could be represented compactly by the string [a-zA-Z_][a-zA-Z_0-9]*. This means "any character a-z, A-Z or _, followed by 0 or more of a-z, A-Z, _ or 0-9". Regular expressions and the finite-state machines they generate are not powerful enough to handle recursive patterns, such as "n opening parentheses, followed by a statement, followed by n closing parentheses." They are not capable of keeping count, and verifying that n is the same on both sides unless you have a finite set of permissible values for n. It takes a full-fledged parser to recognize such patterns in their full generality. A parser can push parentheses on a stack and then try to pop them off and see if the stack is empty at the end. (see example[1] in the SICP book). The Lex programming tool and its compiler is designed to generate code for fast lexical analysers based on a formal description of the lexical syntax. It is not generally considered sufficient for applications with a complicated set of lexical rules and severe performance requirements; for instance, the GNU Compiler Collection (gcc) uses hand-written lexers.

11

Lexer generator
Lexical analysis can often be performed in a single pass if reading is done a character at a time. Single-pass lexers can be generated by tools such as flex. The lex/flex family of generators uses a table-driven approach which is much less efficient than the directly coded approach. With the latter approach the generator produces an engine that directly jumps to follow-up states via goto statements. Tools like re2c and Quex have proven (e.g. RE2C - A More Versatile Scanner Generator (1994)[2] to produce engines that are between two to three times faster than flex produced engines. It is in general difficult to hand-write analyzers that perform better than engines generated by these latter tools. The simple utility of using a scanner generator should not be discounted, especially in the developmental phase, when a language specification might change daily. The ability to express lexical constructs as regular expressions facilitates the description of a lexical analyzer. Some tools offer the specification of pre- and post-conditions which

Lexical analysis are hard to program by hand. In that case, using a scanner generator may save a lot of development time.

12

Lexical analyzer generators


ANTLR - ANTLR generates predicated-LL(k) lexers. Flex - Alternative variant of the classic "lex" (C/C++). JFlex - A rewrite of JLex. Ragel - A state machine and lexical scanner generator with output support for C, C++, C#, Objective-C, D, Java, Go and Ruby source code.

The following lexical analysers can handle Unicode: JavaCC - JavaCC generates lexical analyzers written in Java. JLex - A lexical analyzer generator for Java. Quex (or "Que") - A fast universal lexical analyzer generator for C and C++.

References
[1] mitpress.mit.edu (http:/ / mitpress. mit. edu/ sicp/ full-text/ book/ book-Z-H-31. html#%_sec_5. 1. 4) [2] citeseerx.ist.psu.edu (http:/ / citeseerx. ist. psu. edu/ viewdoc/ summary?doi=10. 1. 1. 27. 1624)

Compiling with C# and Java, Pat Terry, 2005, ISBN 032126360X624 Algorithms + Data Structures = Programs, Niklaus Wirth, 1975, ISBN 0-13-022418-9 Compiler Construction, Niklaus Wirth, 1996, ISBN 0-201-40353-6 Sebesta, R. W. (2006). Concepts of programming languages (Seventh edition) pp.177. Boston: Pearson/Addison-Wesley. Word Mention Segmentation Task (http://www.gabormelli.com/RKB/Word_Mention_Segmentation_Task) analysis page On the applicability of the longest-match rule in lexical analysis (http://www.cis.nctu.edu.tw/~wuuyang/ papers/applicability2002.ps)

Parsing

13

Parsing
In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a text, made of a sequence of tokens (for example, words), to determine its grammatical structure with respect to a given (more or less) formal grammar. Parsing can also be used as a linguistic term, for instance when discussing how phrases are divided up in garden path sentences. Parsing is also an earlier term for the diagramming of sentences of natural languages, and is still used for the diagramming of inflected languages, such as the Romance languages or Latin. The term parsing comes from Latin pars (rtinis), meaning part (of speech).[1][2] Parsing is a common term used in psycholinguistics when describing language comprehension. In this context, parsing refers to the way that human beings, rather than computers, analyze a sentence or phrase (in spoken language or text) "in terms of grammatical constituents, identifying the parts of speech, syntactic relations, etc." [3] This term is especially common when discussing what linguistic cues help speakers to parse garden-path sentences.

Parser
In computing, a parser is one of the components in an interpreter or compiler that checks for correct syntax and builds a data structure (often some kind of parse tree, abstract syntax tree or other hierarchical structure) implicit in the input tokens. The parser often uses a separate lexical analyser to create tokens from the sequence of input characters. Parsers may be programmed by hand or may be (semi-)automatically generated (in some programming languages) by a tool.

Human languages
In some machine translation and natural language processing systems, human languages are parsed by computer programs. Human sentences are not easily parsed by programs, as there is substantial ambiguity in the structure of human language, whose usage is to convey meaning (or semantics) amongst a potentially unlimited range of possibilities but only some of which are germane to the particular case. So an utterance "Man bites dog" versus "Dog bites man" is definite on one detail but in another language might appear as "Man dog bites" with a reliance on the larger context to distinguish between those two possibilities, if indeed that difference was of concern. It is difficult to prepare formal rules to describe informal behaviour even though it is clear that some rules are being followed. In order to parse natural language data, researchers must first agree on the grammar to be used. The choice of syntax is affected by both linguistic and computational concerns; for instance some parsing systems use lexical functional grammar, but in general, parsing for grammars of this type is known to be NP-complete. Head-driven phrase structure grammar is another linguistic formalism which has been popular in the parsing community, but other research efforts have focused on less complex formalisms such as the one used in the Penn Treebank. Shallow parsing aims to find only the boundaries of major constituents such as noun phrases. Another popular strategy for avoiding linguistic controversy is dependency grammar parsing. Most modern parsers are at least partly statistical; that is, they rely on a corpus of training data which has already been annotated (parsed by hand). This approach allows the system to gather information about the frequency with which various constructions occur in specific contexts. (See machine learning.) Approaches which have been used include straightforward PCFGs (probabilistic context-free grammars), maximum entropy, and neural nets. Most of the more successful systems use lexical statistics (that is, they consider the identities of the words involved, as well as their part of speech). However such systems are vulnerable to overfitting and require some kind of smoothing to be effective. Parsing algorithms for natural language cannot rely on the grammar having 'nice' properties as with manually designed grammars for programming languages. As mentioned earlier some grammar formalisms are very difficult

Parsing to parse computationally; in general, even if the desired structure is not context-free, some kind of context-free approximation to the grammar is used to perform a first pass. Algorithms which use context-free grammars often rely on some variant of the CKY algorithm, usually with some heuristic to prune away unlikely analyses to save time. (See chart parsing.) However some systems trade speed for accuracy using, e.g., linear-time versions of the shift-reduce algorithm. A somewhat recent development has been parse reranking in which the parser proposes some large number of analyses, and a more complex system selects the best option.

14

Programming languages
The most common use of a parser is as a component of a compiler or interpreter. This parses the source code of a computer programming language to create some form of internal representation. Programming languages tend to be specified in terms of a context-free grammar because fast and efficient parsers can be written for them. Parsers are written by hand or generated by parser generators. Context-free grammars are limited in the extent to which they can express all of the requirements of a language. Informally, the reason is that the memory of such a language is limited. The grammar cannot remember the presence of a construct over an arbitrarily long input; this is necessary for a language in which, for example, a name must be declared before it may be referenced. More powerful grammars that can express this constraint, however, cannot be parsed efficiently. Thus, it is a common strategy to create a relaxed parser for a context-free grammar which accepts a superset of the desired language constructs (that is, it accepts some invalid constructs); later, the unwanted constructs can be filtered out.

Overview of process
The following example demonstrates the common case of parsing a computer language with two levels of grammar: lexical and syntactic. The first stage is the token generation, or lexical analysis, by which the input character stream is split into meaningful symbols defined by a grammar of regular expressions. For example, a calculator program would look at an input such as "12*(3+4)^2" and split it into the tokens 12, *, (, 3, +, 4, ), ^, 2, each of which is a meaningful symbol in the context of an arithmetic expression. The lexer would contain rules to tell it that the characters *, +, ^, ( and ) mark the start of a new token, so meaningless tokens like "12*" or "(3" will not be generated. The next stage is parsing or syntactic analysis, which is checking that the tokens form an allowable expression. This is usually done with reference to a context-free grammar which recursively defines components that can make up an expression and the order in which they must appear. However, not all rules defining programming languages can be expressed by context-free grammars alone, for example type validity and proper declaration of identifiers. These rules can be formally expressed with attribute grammars. The final phase is semantic parsing or analysis, which is working out the implications of the expression just validated and taking the appropriate action. In the case of a calculator or interpreter, the action is to evaluate the expression or program, a compiler, on the other hand, would generate some kind of code. Attribute grammars can also be used to define these actions.

Parsing

15

Types of parser
The task of the parser is essentially to determine if and how the input can be derived from the start symbol of the grammar. This can be done in essentially two ways: Top-down parsing- Top-down parsing can be viewed as an attempt to find left-most derivations of an input-stream by searching for parse trees using a top-down expansion of the given formal grammar rules. Tokens are consumed from left to right. Inclusive choice is used to accommodate ambiguity by expanding all alternative right-hand-sides of grammar rules.[4] Bottom-up parsing - A parser can start with the input and attempt to rewrite it to the start symbol. Intuitively, the parser attempts to locate the most basic elements, then the elements containing these, and so on. LR parsers are examples of bottom-up parsers. Another term used for this type of parser is Shift-Reduce parsing. LL parsers and recursive-descent parser are examples of top-down parsers which cannot accommodate left recursive production rules. Although it has been believed that simple implementations of top-down parsing cannot accommodate direct and indirect left-recursion and may require exponential time and space complexity while parsing ambiguous context-free grammars, more sophisticated algorithms for top-down parsing have been created by Frost, Hafiz, and Callaghan[5][6] which accommodate ambiguity and left recursion in polynomial time and which generate polynomial-size representations of the potentially exponential number of parse trees. Their algorithm is able to produce both left-most and right-most derivations of an input with regard to a given CFG (context-free grammar). An important distinction with regard to parsers is whether a parser generates a leftmost derivation or a rightmost derivation (see context-free grammar). LL parsers will generate a leftmost derivation and LR parsers will generate a rightmost derivation (although usually in reverse).[4]

Examples of parsers
Bottom-up parsers
Some of the parsers that use bottom-up parsing include: Precedence parser Operator-precedence parser Simple precedence parser BC (bounded context) parsing LR parser (Left-to-right, Rightmost derivation) Simple LR (SLR) parser LALR parser Canonical LR (LR(1)) parser GLR parser CYK parser

Parsing

16

Parser development software


Some of the well known parser development tools include the following. Also see comparison of parser generators. ANTLR Bison Coco/R GOLD JavaCC Lemon Lex Parboiled ParseIT Ragel SHProto (FSM parser language)[7] Spirit Parser Framework

Syntax Definition Formalism SYNTAX VisualLangLab XPL Yacc

Lookahead
Lookahead establishes the maximum incoming tokens that a parser can use to decide which rule it should use. Lookahead is especially relevant to LL, LR, and LALR parsers, where it is often explicitly indicated by affixing the lookahead to the algorithm name in parentheses, such as LALR(1). Most programming languages, the primary target of parsers, are carefully defined in such a way that a parser with limited lookahead, typically one, can parse them, because parsers with limited lookahead are often more efficient. One important change to this trend came in 1990 when Terence Parr created ANTLR for his Ph.D. thesis, a parser generator for efficient LL(k) parsers, where k is any fixed value. Parsers typically have only a few actions after seeing each token. They are shift (add this token to the stack for later reduction), reduce (pop tokens from the stack and form a syntactic construct), end, error (no known rule applies) or conflict (does not know whether to shift or reduce). Lookahead has two advantages. It helps the parser take the correct action in case of conflicts. For example, parsing the if statement in the case of an else clause. It eliminates many duplicate states and eases the burden of an extra stack. A C language non-lookahead parser will have around 10,000 states. A lookahead parser will have around 300 states. Example: Parsing the Expression 1 + 2 * 3 Set of expression parsing rules (called grammar) is as follows, Rule1: Rule2: Rule3: Rule4: E E E + E + E Expression is the sum of two expressions. E * E Expression is the product of two expressions. number Expression is a simple number has less precedence than *

Parsing Most programming languages (except for a few such as APL and Smalltalk) and algebraic formulas give higher precedence to multiplication than addition, in which case the correct interpretation of the example above is (1 + (2*3)). Note that Rule4 above is a semantic rule. It is possible to rewrite the grammar to incorporate this into the syntax. However, not all such rules can be translated into syntax. Simple non-lookahead parser actions 1. 2. 3. 4. 5. 6. 7. 8. Reduces 1 to expression E on input 1 based on rule3. Shift + onto stack on input 1 in anticipation of rule1. Reduce stack element 2 to Expression E based on rule3. Reduce stack items E+ and new input E to E based on rule1. Shift * onto stack on input * in anticipation of rule2. Shift 3 onto stack on input 3 in anticipation of rule3. Reduce 3 to Expression E on input 3 based on rule3. Reduce stack items E* and new input E to E based on rule2.

17

The parse tree and resulting code from it is not correct according to language semantics. To correctly parse without lookahead, there are three solutions: The user has to enclose expressions within parentheses. This often is not a viable solution. The parser needs to have more logic to backtrack and retry whenever a rule is violated or not complete. The similar method is followed in LL parsers. Alternatively, the parser or grammar needs to have extra logic to delay reduction and reduce only when it is absolutely sure which rule to reduce first. This method is used in LR parsers. This correctly parses the expression but with many more states and increased stack depth. Lookahead parser actions 1. Shift 1 onto stack on input 1 in anticipation of rule3. It does not reduce immediately. 2. Reduce stack item 1 to simple Expression on input + based on rule3. The lookahead is +, so we are on path to E +, so we can reduce the stack to E. 3. Shift + onto stack on input + in anticipation of rule1. 4. Shift 2 onto stack on input 2 in anticipation of rule3. 5. Reduce stack item 2 to Expression on input * based on rule3. The lookahead * expects only E before it. 6. Now stack has E + E and still the input is *. It has two choices now, either to shift based on rule2 or reduction based on rule1. Since * has more precedence than + based on rule4, so shift * onto stack in anticipation of rule2. 7. Shift 3 onto stack on input 3 in anticipation of rule3. 8. Reduce stack item 3 to Expression after seeing end of input based on rule3. 9. Reduce stack items E * E to E based on rule2. 10. Reduce stack items E + E to E based on rule1. The parse tree generated is correct and simply more efficient than non-lookahead parsers. This is the strategy followed in LALR parsers.

Parsing

18

References
[1] [2] [3] [4] "Bartleby.com homepage" (http:/ / www. bartleby. com/ 61/ 33/ P0083300. html). . Retrieved 28 November 2010. "parse" (http:/ / dictionary. reference. com/ search?q=parse& x=0& y=0). dictionary.reference.com. . Retrieved 27 November 2010. "parse" (http:/ / dictionary. reference. com/ browse/ parse). dictionary.reference.com. . Retrieved 27 November 2010. Aho, A.V., Sethi, R. and Ullman ,J.D. (1986) " Compilers: principles, techniques, and tools." Addison-Wesley Longman Publishing Co., Inc. Boston, MA, USA. [5] Frost, R., Hafiz, R. and Callaghan, P. (2007) " Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars ." 10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE , Pages: 109 - 120, June 2007, Prague. [6] Frost, R., Hafiz, R. and Callaghan, P. (2008) " Parser Combinators for Ambiguous Left-Recursive Grammars." 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN , Volume 4902/2008, Pages: 167 - 181, January 2008, San Francisco. [7] shproto.org (http:/ / shproto. org)

Further reading
Chapman, Nigel P., LR Parsing: Theory and Practice (http://books.google.com/books?id=nEA9AAAAIAAJ& printsec=frontcover), Cambridge University Press, 1987. ISBN 0-521-30413-X Grune, Dick; Jacobs, Ceriel J.H., Parsing Techniques - A Practical Guide (http://dickgrune.com/Books/ PTAPG_1st_Edition/), Vrije Universiteit Amsterdam, Amsterdam, The Netherlands. Originally published by Ellis Horwood, Chichester, England, 1990; ISBN 0-13-651431-6

External links
The Lemon LALR Parser Generator (http://www.hwaci.com/sw/lemon/)

Top-down parsing
In computer science, top-down parsing is a parsing strategy where one first looks at the highest level of the parse tree and works down the parse tree by using the rewriting rules of a formal grammar. LL parsers are a type of parser that uses a top-down parsing strategy. Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis. It occurs in the analysis of both natural languages and computer languages. Top-down parsing can be viewed as an attempt to find left-most derivations of an input-stream by searching for parse-trees using a top-down expansion of the given formal grammar rules. Tokens are consumed from left to right. Inclusive choice is used to accommodate ambiguity by expanding all alternative right-hand-sides of grammar rules.[1] Simple implementations of top-down parsing do not terminate for left-recursive grammars, and top-down parsing with backtracking may have exponential time complexity with respect to the length of the input for ambiguous CFGs.[2] However, more sophisticated top-down parsers have been created by Frost, Hafiz, and Callaghan [3][4] which do accommodate ambiguity and left recursion in polynomial time and which generate polynomial-sized representations of the potentially exponential number of parse trees.

Top-down parsing

19

Programming language application


A compiler parses input from a programming language to assembly language or an internal representation by matching the incoming symbols to production rules. Production rules are commonly defined using Backus-Naur form. An LL parser is a type of parser that does top-down parsing by applying each production rule to the incoming symbols, working from the left-most symbol yielded on a production rule and then proceeding to the next production rule for each non-terminal symbol encountered. In this way the parsing starts on the Left of the result side (right side) of the production rule and evaluates non-terminals from the Left first and, thus, proceeds down the parse tree for each new non-terminal before continuing to the next symbol for a production rule. For example: would match and attempt to match next. Then would be tried. As one may

expect, some languages are more ambiguous than others. For a non-ambiguous language in which all productions for a non-terminal produce distinct strings: the string produced by one production will not start with the same symbol as the string produced by another production. A non-ambiguous language may be parsed by an LL(1) grammar where the (1) signifies the parser reads ahead one token at a time. For an ambiguous language to be parsed by an LL parser, the parser must lookahead more than 1 symbol, e.g. LL(3). The common solution to this problem is to use an LR parser, which is a type of shift-reduce parser, and does bottom-up parsing.

Accommodating left recursion in top-down parsing


A formal grammar that contains left recursion cannot be parsed by a naive recursive descent parser unless they are converted to a weakly equivalent right-recursive form. However, recent research demonstrates that it is possible to accommodate left-recursive grammars (along with all other forms of general CFGs) in a more sophisticated top-down parser by use of curtailment. A recognition algorithm which accommodates ambiguous grammars and curtails an ever-growing direct left-recursive parse by imposing depth restrictions with respect to input length and current input position, is described by Frost and Hafiz in 2006.[5] That algorithm was extended to a complete parsing algorithm to accommodate indirect (by comparing previously computed context with current context) as well as direct left-recursion in polynomial time, and to generate compact polynomial-size representations of the potentially exponential number of parse trees for highly ambiguous grammars by Frost, Hafiz and Callaghan in 2007.[3] The algorithm has since been implemented as a set of parser combinators written in the Haskell programming language. The implementation details of these new set of combinators can be found in a paper [4] by the above-mentioned authors, which was presented in PADL'08. The X-SAIGA [6] site has more about the algorithms and implementation details.

Time and space complexity of top-down parsing


When top-down parser tries to parse an ambiguous input with respect to an ambiguous CFG, it may need exponential number of steps (with respect to the length of the input) to try all alternatives of the CFG in order to produce all possible parse trees, which eventually would require exponential memory space. The problem of exponential time complexity in top-down parsers constructed as sets of mutually recursive functions has been solved by Norvig in 1991.[7] His technique is similar to the use of dynamic programming and state-sets in Earley's algorithm (1970), and tables in the CYK algorithm of Cocke, Younger and Kasami. The key idea is to store results of applying a parser p at position j in a memotable and to reuse results whenever the same situation arises. Frost, Hafiz and Callaghan[3][4] also use memoization for refraining redundant

Top-down parsing computations to accommodate any form of CFG in polynomial time ((n4) for left-recursive grammars and (n3) for non left-recursive grammars). Their top-down parsing algorithm also requires polynomial space for potentially exponential ambiguous parse trees by 'compact representation' and 'local ambiguities grouping'. Their compact representation is comparable with Tomitas compact representation of bottom-up parsing.[8] Using PEG's, another representation of grammars, packrat parsers provide an elegant and powerful parsing algorithm. See Parsing expression grammar.

20

References
[1] Aho, A.V., Sethi, R. and Ullman ,J.D. (1986) " Compilers: principles, techniques, and tools." Addison-Wesley Longman Publishing Co., Inc. Boston, MA, USA. [2] Aho, A.V and Ullman, J. D. (1972) " The Theory of Parsing, Translation, and Compiling, Volume 1: Parsing." Englewood Cliffs, N.J.: Prentice-Hall. [3] Frost, R., Hafiz, R. and Callaghan, P. (2007) " Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars ." 10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE , Pages: 109 - 120, June 2007, Prague. [4] Frost, R., Hafiz, R. and Callaghan, P. (2008) " Parser Combinators for Ambiguous Left-Recursive Grammars." 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN , Volume 4902/2008, Pages: 167-181, January 2008, San Francisco. [5] Frost, R. and Hafiz, R. (2006) " A New Top-Down Parsing Algorithm to Accommodate Ambiguity and Left Recursion in Polynomial Time." ACM SIGPLAN Notices, Volume 41 Issue 5, Pages: 46 - 54. [6] http:/ / www. cs. uwindsor. ca/ ~hafiz/ proHome. html [7] Norvig, P. (1991) Techniques for automatic memoisation with applications to context-free parsing. Journal - Computational Linguistics Volume 17, Issue 1, Pages: 91 - 98. [8] Tomita, M. (1985) Efficient Parsing for Natural Language. Kluwer, Boston, MA.

External links
X-SAIGA (http://www.cs.uwindsor.ca/~hafiz/proHome.html) - eXecutable SpecificAtIons of GrAmmars

Bottom-up parsing
In computer science, parsing reveals the grammatical structure of linear input text, as a first step in working out its meaning. Bottom-up parsing identifies and processes the text's lowest-level small details first, before its mid-level structures, and leaving the highest-level overall structure to last.

Bottom-up Versus Top-down


The bottom-up name comes from the concept of a parse tree, in which the most detailed parts are at the bushy bottom of the (upside-down) tree, and larger structures composed from them are in successively higher layers, until at the top or "root" of the tree a single unit describes the entire input stream. A bottom-up parse discovers and processes that tree starting from the bottom left end, and incrementally works its way upwards and rightwards.[1] A parser may act on the structure hierarchy's low, mid, and highest levels without ever creating an actual data tree; the tree is then merely implicit in the parser's actions. Bottom-up parsing lazily waits until it has scanned and parsed all parts of some construct before committing to what the combined construct is.

Bottom-up parsing

21

Typical parse tree for A = B + C*2; D = 1

Bottom-up parse steps

Top-down parse steps

The opposite of this are top-down parsing methods, in which the input's overall structure is decided (or guessed at) first, before dealing with mid-level parts, leaving the lowest-level small details to last. A top-down parse discovers and processes the hierarchical tree starting from the top, and incrementally works its way downwards and rightwards. Top-down parsing eagerly decides what a construct is much earlier, when it has only scanned the leftmost symbol of that construct and has not yet parsed any of its parts. Left corner parsing is a hybrid method which works bottom-up along the left edges of each subtree, and top-down on the rest of the parse tree. If a language grammar has multiple rules that may start with the same leftmost symbols but have different endings, then that grammar can be efficiently handled by a deterministic bottom-up parse but cannot be handled top-down without guesswork and backtracking. So bottom-up parsers handle a somewhat larger range of computer language grammars than do deterministic top-down parsers. Bottom-up parsing is sometimes done by backtracking. But much more commonly, bottom-up parsing is done by a shift-reduce parser such as a LALR parser. One of the earlier documentations of a bottom-up parser is "A Syntax-Oriented Translator" by Peter Zilahy Ingerman, published in 1966 by Academic Press, NY.

References
[1] Compilers: Principles, Techniques, and Tools (2nd Edition), by Alfred Aho, Monica Lam, Ravi Sethi, and Jeffrey Ullman, Prentice Hall 2006.

Recursive descent parser

22

Recursive descent parser


In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure usually implements one of the production rules of the grammar. Thus the structure of the resulting program closely mirrors that of the grammar it recognizes. A predictive parser is a recursive descent parser that does not require backtracking. Predictive parsing is possible only for the class of LL(k) grammars, which are the context-free grammars for which there exists some positive integer k that allows a recursive descent parser to decide which production to use by examining only the next k tokens of input. (The LL(k) grammars therefore exclude all ambiguous grammars, as well as all grammars that contain left recursion. Any context-free grammar can be transformed into an equivalent grammar that has no left recursion, but removal of left recursion does not always yield an LL(k) grammar.) A predictive parser runs in linear time. Recursive descent with backup is a technique that determines which production to use by trying each production in turn. Recursive descent with backup is not limited to LL(k) grammars, but is not guaranteed to terminate unless the grammar is LL(k). Even when they terminate, parsers that use recursive descent with backup may require exponential time. Although predictive parsers are widely used, programmers often prefer to create LR or LALR parsers via parser generators without transforming the grammar into LL(k) form. Predictive parsers can be depicted using transition diagrams for each non-terminal symbol where the edges between the initial and the final states are labled by the symbols (terminals and non-terminals) of the right side of the production rule [1].

Example parser
The following EBNF-like grammar (for Niklaus Wirth's PL/0 programming language, from Algorithms + Data Structures = Programs) is in LL(1) form: program = block "." . block = ["const" ident "=" number {"," ident "=" number} ";"] ["var" ident {"," ident} ";"] {"procedure" ident ";" block ";"} statement . statement = [ident ":=" expression | "call" ident | "begin" statement {";" statement} "end" | "if" condition "then" statement | "while" condition "do" statement ] . condition = "odd" expression | expression ("="|"#"|"<"|"<="|">"|">=") expression . expression = ["+"|"-"] term {("+"|"-") term} .

Recursive descent parser

23

term = factor {("*"|"/") factor} . factor = ident | number | "(" expression ")" . Terminals are expressed in quotes. Each nonterminal is defined by a rule in the grammar, except for ident and number, which are assumed to be implicitly defined.

C implementation
What follows is an implementation of a recursive descent parser for the above language in C. The parser reads in source code, and exits with an error message if the code fails to parse, exiting silently if the code parses correctly. Notice how closely the predictive parser below mirrors the grammar above. There is a procedure for each nonterminal in the grammar. Parsing descends in a top-down manner, until the final nonterminal has been processed. The program fragment depends on a global variable, sym, which contains the next symbol from the input, and the function getsym, which updates sym when called. The implementations of the functions getsym and error are omitted for simplicity. typedef enum {ident, number, lparen, rparen, times, slash, plus, minus, eql, neq, lss, leq, gtr, geq, callsym, beginsym, semicolon, endsym, ifsym, whilesym, becomes, thensym, dosym, constsym, comma, varsym, procsym, period, oddsym} Symbol; Symbol sym; void getsym(void); void error(const char msg[]); void expression(void); int accept(Symbol s) { if (sym == s) { getsym(); return 1; } return 0; } int expect(Symbol s) { if (accept(s)) return 1; error("expect: unexpected symbol"); return 0; } void factor(void) { if (accept(ident)) { ;

Recursive descent parser } else if (accept(number)) { ; } else if (accept(lparen)) { expression(); expect(rparen); } else { error("factor: syntax error"); getsym(); } } void term(void) { factor(); while (sym == times || sym == slash) { getsym(); factor(); } } void expression(void) { if (sym == plus || sym == minus) getsym(); term(); while (sym == plus || sym == minus) { getsym(); term(); } } void condition(void) { if (accept(oddsym)) { expression(); } else { expression(); if (sym == eql || sym == neq || sym == lss || sym == leq || sym == gtr || sym == geq) { getsym(); expression(); } else { error("condition: invalid operator"); getsym(); } } } void statement(void) { if (accept(ident)) {

24

Recursive descent parser expect(becomes); expression(); else if (accept(callsym)) { expect(ident); else if (accept(beginsym)) { do { statement(); } while (accept(semicolon)); expect(endsym); else if (accept(ifsym)) { condition(); expect(thensym); statement(); else if (accept(whilesym)) { condition(); expect(dosym); statement();

25

} }

} } void block(void) { if (accept(constsym)) { do { expect(ident); expect(eql); expect(number); } while (accept(comma)); expect(semicolon); } if (accept(varsym)) { do { expect(ident); } while (accept(comma)); expect(semicolon); } while (accept(procsym)) { expect(ident); expect(semicolon); block(); expect(semicolon); } statement(); } void program(void) { getsym(); block();

Recursive descent parser expect(period); }

26

Implementation in functional languages


Recursive descent parsers are particularly easy to implement in functional languages such as Haskell, Lisp or ML as they tend to be better suited for recursion in general. See Functional Pearls: Monadic Parsing in Haskell [2]

References
[1] Alfred V. Aho, Ravi Sethi, Jeffrey Ullman: Compilers: Principles, Techniques and Tools. Addison Wesley, 1986, pp. 183 [2] http:/ / www. cs. nott. ac. uk/ ~gmh/ pearl. pdf

This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later. Compilers: Principles, Techniques, and Tools, first edition, Alfred V Aho, Ravi Sethi, and Jeffrey D Ullman, in particular Section 4.4. Modern Compiler Implementation in Java, Second Edition, Andrew Appel, 2002, ISBN 0-521-82060-X. Recursive Programming Techniques, W.H. Burge, 1975, ISBN 0-201-14450-6 Crafting a Compiler with C, Charles N Fischer and Richard J LeBlanc, Jr, 1991, ISBN 0-8053-2166-7. Parse::RecDescent (http://search.cpan.org/~dconway/Parse-RecDescent-1.94/lib/Parse/RecDescent.pod): A versatile recursive descent Perl module. pyparsing (http://pyparsing.sourceforge.net/): A versatile Python recursive parsing module that is not recursive descent ( python-list post (http://mail.python.org/pipermail/python-list/2007-November/636291.html)). Jparsec (http://jparsec.codehaus.org/) a Java port of Haskell's Parsec module. Compiling with C# and Java, Pat Terry, 2005, ISBN 0-321-26360-X, 624 Algorithms + Data Structures = Programs, Niklaus Wirth, 1975, ISBN 0-13-022418-9 Compiler Construction, Niklaus Wirth, 1996, ISBN 0-201-40353-6

External links
Introduction to Parsing (http://www.mollypages.org/page/grammar/index.mp) - an easy to read introduction to parsing, with a comprehensive section on recursive descent parsing How to turn a Grammar into C code (http://teaching.idallen.com/cst8152/98w/recursive_decent_parsing. html) - a brief tutorial on implementing recursive descent parser ExprTk Recursive descent mathematical expression parser and evaluator (http://partow.net/programming/ exprtk/index.html) in C++ Simple mathematical expressions parser (http://lukaszwrobel.pl/blog/math-parser-part-3-implementation) in Ruby Simple Top Down Parsing in Python (http://effbot.org/zone/simple-top-down-parsing.htm) Jack W. Crenshaw: Let's Build A Compiler (1988-1995) (http://compilers.iecc.com/crenshaw/), in Pascal, with assembly language output, using a "keep it simple" approach

LR parser

27

LR parser
In computer science, LR parsers are a type of bottom-up parsers that efficiently handle deterministic context-free languages in guaranteed linear time.[1] The LALR parsers and the SLR parsers are common variants of LR parsers. LR parsers are mechanically generated from a formal grammar for the language by some parser generator tool. They are very widely used for the translation of computer languages, more than other kinds of generated parsers.

LR and Other Kinds of Parsers


The name LR is an acronym. The L means that the parser reads input text in one direction without backing up; that direction is typically Left to right within each line, and top to bottom across the lines of the full input file. (This is true for most parsers.) The R means that the parser produces a reversed Rightmost derivation; it does a bottom-up parse, not a top-down LL parse or ad-hoc parse. The name LR is often followed by a numeric qualifier, as in LR(1) or sometimes LR(k). To avoid backtracking or guessing, the LR parser is allowed to peek ahead at k lookahead input symbols before deciding how to parse earlier symbols. Typically k is 1 and is not mentioned. The name LR is often preceded by other qualifiers, as in SLR and LALR. LR parsers are deterministic; they produce a single correct parse without guesswork or backtracking, in linear time. This is ideal for computer languages. But LR parsers are not suited for human languages which need more flexible but slower methods. Other parser methods that backtrack or yield multiple parses may take N2 or N3 time when they guess badly. The above properties of L, R, and k are actually shared by all shift-reduce parsers, including precedence parsers. But by convention, the LR name stands for the form of parsing invented by Donald Knuth, and excludes the earlier, less powerful precedence methods.[1] LR parsers can handle a larger range of languages and grammars than precedence parsers or top-down LL parsing.[2] This is because the LR parser waits until it has seen an entire instance of some grammar pattern before committing to what it has found. An LL parser has to decide or guess what it is seeing much sooner, when it has only seen the leftmost input symbol of that pattern. LR is also better at error reporting. It detects syntax errors as early in the input stream as possible.

LR Tutorial
When using an LR parser within some larger program, you can usually ignore all the mathematical details about states, tables, and generators. All of the parsing actions and outputs and their timing can be simply understood by viewing the LR parser as just a shift-reduce parser with some nifty decision method. If the generator tool complains about some parts of your grammar, you may need some understanding of states and the difference between LR and LALR in order to tweak your grammar into an acceptable form. Full understanding of grammar and state analysis algorithms is needed only by the tool implementer and by students of parsing theory courses.

LR parser

28

Bottom-Up Parse Tree for Example A*2 + 1


An LR parser scans and parses the input text in one forward pass over the text. The parser builds up the parse tree incrementally, bottom up, and left to right, without guessing or backtracking. At every point in this pass, the parser has accumulated a list of subtrees or phrases of the input text that have been already parsed. Those subtrees are not yet joined together because the parser has not yet reached the right end of the syntax pattern that will combine them. At step 6 in the example parse, only "A*2" has been parsed, incompletely. Only the shaded lower-left corner of the parse tree exists. None of the parse tree nodes numbered 7 and above exist yet. Nodes 3, 4, and 6 are the roots of isolated subtrees for variable A, operator *, and number 2, respectively. These three root nodes are temporarily held in a parse stack. The remaining unparsed portion of the input stream is "+ 1".

Bottom-up parse tree built in numbered steps

Shift & Reduce Actions


As with other shift-reduce parsers, an LR parser works by doing some combination of Shift steps and Reduce steps. A Shift step advances in the input stream by one symbol. That shifted symbol becomes a new single-node parse tree. A Reduce step applies a completed grammar rule to some of the recent parse trees, joining them together as one tree with a new root symbol. If the input has no syntax errors, the parser continues with these steps until all of the input has been consumed and all of the parse trees have been reduced to a single tree representing an entire legal input. LR parsers differ from other shift-reduce parsers in how they decide when to reduce, and how to pick between rules with similar endings. But the final decisions and the sequence of shift or reduce steps are the same. Much of the LR parser's efficiency is from being deterministic. To avoid guessing, the LR parser often looks ahead (rightwards) at the next scanned symbol, before deciding what to do with previously scanned symbols. The lexical scanner works one or more symbols ahead of the parser. The lookahead symbols are the 'right-hand context' for the parsing decision.[3]

LR parser

29

Bottom-Up Parse Stack


Like other shift-reduce parsers, an LR parser lazily waits until it has scanned and parsed all parts of some construct before committing to what the combined construct is. The parser then acts immediately on the combination instead of waiting any further. In the parse tree example, the phrase A gets reduced to Value and then to Products in steps 1-3 as soon as lookahead * is seen, rather than waiting any later to organize those parts of the parse tree. The decisions for how to handle A are based only on what the parser and scanner have already seen, without considering things that appear much later to the right.

Bottom-Up Parser at step 6

Reductions reorganize the most recently parsed things, immediately to the left of the lookahead symbol. So the list of already-parsed things acts like a stack. This parse stack grows rightwards. The base or bottom of the stack is on the left and holds the leftmost, oldest parse fragment. Every reduction step acts only on the rightmost, newest parse fragments. (This accumulative parse stack is very unlike the predictive, leftward-growing parse stack used by top-down parsers.)

Bottom-Up Parse Steps for Example A*2 + 1


Step 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Parse Stack empty id Value Products Products * Products * int Products * Value Products Sums Sums + Sums + int Sums + Value eof eof Unparsed A*2 + 1 shift *2 + 1 Value id *2 + 1 Products Value *2 + 1 shift 2 + 1 shift + 1 Value int + 1 Products Products * Value + 1 Sums Products + 1 shift 1 shift Value int Products Value Sums Sums + Products done Shift/Reduce

Sums + Products eof Sums eof

Step 6 applies a grammar rule with multiple parts: Products Products * Value

LR parser This matches the stack top holding the parsed phrases "... Products * Value". The reduce step replaces this instance of the rule's right hand side, "Products * Value" by the rule's left hand side symbol, here a larger Products. If the parser builds complete parse trees, the three trees for inner Products, =, and Value are combined by a new tree root for Products. Otherwise, semantic details from the inner Products and Value are output to some later compiler pass, or are combined and saved in the new Products symbol.[4]

30

LR Parse Steps for Example A*2 + 1


In LR parsers, the shift and reduce decisions are potentially based on the entire stack of everything that has been previously parsed, not just on a single, topmost stack symbol. If done in an unclever way, that could lead to very slow parsers that get slower and slower for longer inputs. LR parsers do this with constant speed, by summarizing all the relevant left context information into a single number called the LR(0) parser state. For each grammar and LR analysis method, there is a fixed (finite) number of such states. Besides holding the already-parsed symbols, the parse stack also remembers the state numbers reached by everything up to those points. At every parse step, the entire input text is divided into a stack of previously parsed phrases, and a current lookahead symbol, and the remaining unscanned text. The parser's next action is determined by its current LR(0) state number (rightmost on the stack) and the lookahead symbol. In the steps below, all the black details are exactly the same as in other non-LR shift-reduce parsers. LR parser stacks add the state information in purple, summarizing the black phrases to their left on the stack and what syntax possibilities to expect next. Users of an LR parser can usually ignore state information. These states are explained in a later section.
Step Parse Stack Symbol state ... state
0

Look Unscanned Parser Ahead Action id *2 + 1 shift

Grammar Rule

Next State 9 7 4 5 8 6

0 1 2 3 4 5 6 7 8 9 10 11 12 13

0 0 0 0 0 0 0 0 0 0 0 0 0

id9 Value7 Products4 Products4 *5 Products4 *5 int8

* * * int +

2 + 1 reduce Value id 2 + 1 reduce Products Value 2 + 1 shift + 1 shift 1 reduce Value int

Products4 *5 Value6 + Products4 Sums1 Sums1 +2 Sums1 +2 int8 Sums1 +2 Value7 + + int eof eof

1 reduce Products Products * Value 4 1 reduce Sums Products 1 shift eof shift reduce Value int reduce Products Value reduce Sums Sums + Products done 1 2 8 7 3 1

Sums1 +2 Products3 eof Sums1 eof

At initial step 0, the input stream "A*2 + 1" is divided into an empty section on the parse stack, lookahead text "A" scanned as an id symbol, and the remaining unscanned text "*2 + 1". The parse stack begins by holding only initial state 0. When state 0 sees the lookahead id, it knows to shift that id onto the stack, and scan the next input symbol *, and advance to state 9.

LR parser At step 4, the total input stream "A*2 + 1" is currently divided into the parsed section "A*" with 2 stacked phrases Products and *, lookahead text "2" scanned as an int symbol, and the remaining unscanned text " + 1". The states corresponding to the stacked phrases are 0, 4, and 5. The current, rightmost state on the stack is state 5. When state 5 sees the lookahead int, it knows to shift that int onto the stack as its own phrase, and scan the next input symbol +, and advance to state 8.

31

At step 11, all of the input stream has been consumed but only partially organized. The current state is 7. When state 7 sees the lookahead eof, it knows to apply the completed grammar rule Sums Sums + Products by combining the stack's rightmost three phrases for Sums, +, and Products into one thing. State 7 itself doesn't know what the next state should be. This is found by going back to state 0, just to the left of the phrase being reduced. When state 0 sees this new completed instance of a Sums, it advances to state 1 (again). This consulting of older states is why they are kept on the stack, instead of keeping only the current state.

Grammar for the Example A*2 + 1


LR parsers are constructed from a grammar that formally defines the syntax of the input language as a set of patterns. The grammar doesn't cover all language rules, such as the size of numbers, or the consistent use of names and their definitions in the context of the whole program. LR parsers use a context-free grammar that deals just with local patterns of symbols. The example grammar used here is a tiny subset of the Java or C language: r0: Goal Sums eof r1: Sums Sums + Products r2: Sums Products r3: Products Products * Value r4: Products Value r5: Value int r6: Value id The grammar's terminal symbols are the multi-character symbols or 'tokens' found in the input stream by a lexical scanner. Here these include + * and int for any integer constant, and id for any identifier name, and eof for end of input file. The grammar doesn't care what the int values or id spellings are, nor does it care about blanks or line breaks. The grammar uses these terminal symbols but does not define them. They are always at the bottom bushy end of the parse tree. The capitalized terms like Sums are nonterminal symbols. These are names for concepts or patterns in the language. They are defined in the grammar and never occur themselves in the input stream. They are always above the bottom of the parse tree. They only happen as a result of the parser applying some grammar rule. Some terminals are defined with two or more rules; these are alternative patterns. Rules can refer back to themselves. This grammar uses recursive rules to handle repeated math operators. Grammars for complete languages use recursive rules to handle lists, parenthesized expressions, and nested statements. Any given computer language can be described by several different grammars. An LR(1) parser can handle many but not all common grammars. It is usually possible to manually modify a grammar so that it fits the limitations of LR(1) parsing and the generator tool.

LR parser The grammar for an LR parser must be unambiguous itself, or must be augmented by tie-breaking precedence rules. This means there is only one correct way to apply the grammar to a given legal example of the language, resulting in a unique parse tree with just one meaning, and a unique sequence of shift/reduce actions for that example. LR parsing is not a useful technique for human languages with ambiguous grammars that depend on the interplay of words. Human languages are better handled by parsers like Generalized LR parser, the Earley parser, or the CYK algorithm that can simultaneously compute all possible parse trees in one pass.

32

Parse Table for the Example Grammar


Most LR parsers are table driven. The parser's program code is a simple generic loop that is the same for all grammars and languages. The knowledge of the grammar and its syntactic implications are encoded into unchanging data tables called parse tables. The tables shows whether to shift or reduce (and by which grammar rule), for every legal combination of parser state and lookahead symbol. The parse tables also tell how to compute the next state, given just a current state and a next symbol. The parse tables are much larger than the grammar. LR tables are hard to accurately compute by hand for big grammars. So they are mechanically derived from the grammar by some parser generator tool like Bison.[5] Depending on how the states and parsing table are generated, the resulting parser is called either a SLR (simple LR) parser, LALR (look-ahead LR) parser, or canonical LR parser. LALR parsers handle more grammars than SLR parsers. Canonical LR parsers handle even more grammars, but use many more states and much larger tables. The example grammar is SLR. LR parse tables are two-dimensional. Each current LR(0) parser state has its own row. Each possible next symbol has its own column. Many combinations of state and next symbol are impossible for valid input streams. These blank cells trigger syntax error messages. The left half of the table has columns for lookahead terminal symbols. These cells determine whether the next parser action is shift (to state n), or reduce (by grammar rule rn). The Goto right half of the table has columns for nonterminal symbols. These cells show which state to advance to, after some reduction's Left Hand Side has created an expected new instance of that symbol. This is like a shift action but for nonterminals; the lookahead terminal symbol is unchanged. The table column "Current Rules" documents the meaning and syntax possibilities for each state, as worked out by the parser generator. It is not included in the actual tables used at parsing time. The marker shows where the parser is now, within some partially recognized grammar rules. The things to the left of have been parsed, and the things to the right are expected soon. A state has several such current rules if the parser has not yet narrowed possibilities down to a single rule.
Curr State 0 1 Current Rules Goal Sums eof Goal Sums eof Sums Sums + Products 2 3 Sums Sums + Products Sums Sums + Products Products Products * Value 4 Sums Products Products Products * Value 5 6 Products Products * Value 8 Products Products * Value 9 r3 r3 r3 6 5 r2 r2 8 9 5 r1 r1 3 7 Lookahead int id * + 8 9 2 done eof LHS Goto Sums Products Value 1 4 7

LR parser

33
7 8 9 Products Value Value int Value id r4 r4 r5 r5 r6 r6 r4 r5 r6

In state 2 above, the parser has just found and shifted-in the + of grammar rule r1: Sums Sums + Products The next expected phrase is Products. Products begins with terminal symbols int or id. If the lookahead is either of those, the parser shifts them in and advances to state 8 or 9, respectively. When a Products has been found, the parser advances to state 3 to accumulate the complete list of summands and find the end of rule r0. A Products can also begin with nonterminal Value. For any other lookahead or nonterminal, the parser announces a syntax error.

In state 3, the parser has just found a Products phrase, that could be from two possible grammar rules: r1: Sums Sums + Products r3: Products Products * Value The choice between r1 and r3 can't be decided just from looking backwards at prior phrases. The parser has to check the lookahead symbol to tell what to do. If the lookahead is *, we are in rule 3 so the parser shifts in the * and advances to state 5. If the lookahead is eof, we are at the end of rule 1 and rule 0 so the parser is done.

In state 9 above, all the non-blank, non-error cells are for the same reduction r6. Some parsers save time and table space by not checking the lookahead symbol in these simple cases. Syntax errors are then detected somewhat later, after some harmless reductions, but still before the next shift action or parser decision. Individual table cells must not hold multiple, alternative actions, otherwise the parser would be nondeterministic with guesswork and backtracking. If the grammar is not LR(1), some cells will have shift/reduce conflicts between a possible shift action and reduce action, or reduce/reduce conflicts between multiple grammar rules. LR(k) parsers resolve these conflicts (where possible) by checking additional lookahead symbols beyond the first.

LR Parser Loop
The LR parser begins with a nearly empty parse stack containing just the start state 0, and with the lookahead holding the input stream's first scanned symbol. The parser then repeats the following loop step until done, or stuck on a syntax error: The topmost state on the parse stack is some state s, and the current lookahead is some terminal symbol t. Look up the next parser action from row s and column t of the Lookahead Action table. That action is either Shift, Reduce, Done, or Error: Shift n: Shift the matched terminal t onto the parse stack and scan the next input symbol into the lookahead buffer. Push next state n onto the parse stack as the new current state. Reduce rm: Apply grammar rule rm: Lhs S1 S2 ... SL Remove the matched topmost L symbols (and parse trees and associated state numbers) from the parse stack. This exposes a prior state p that was expecting an instance of the Lhs symbol. Join the L parse trees together as one parse tree with new root symbol Lhs. Lookup the next state n from row p and column Lhs of the LHS Goto table.

LR parser Push the symbol and tree for Lhs onto the parse stack. Push next state n onto the parse stack as the new current state. The lookahead and input stream remain unchanged. Done: Lookahead t is the eof marker. End of parsing. If the state stack contains just the start state report success. Otherwise, report a syntax error. No action: Report a syntax error. The parser ends, or attempts some recovery.

34

LR Generator Analysis
This section of the article can be skipped by most users of LR parser generators.

LR States
State 2 in the example parse table is for the partially parsed rule r1: Sums Sums + Products This shows how the parser got here, by seeing Sums then + while looking for a larger Sums. The marker has advanced beyond the beginning of the rule. It also shows how the parser expects to eventually complete the rule, by next finding a complete Product. But more details are needed on how to parse all the parts of that Products. The partially parsed rules for a state are called its "core LR(0) items". The parser generator adds additional rules or items for all the possible next steps in building up the expected Products: r3: Products Products * Value r4: Products Value r5: Value int r6: Value id Note that the marker is at the beginning of each of these added rules; the parser has not yet confirmed and parsed any part of them. These additional items are called the "closure" of the core items. For each nonterminal symbol immediately following a , the generator adds the rules defining that symbol. This adds more markers, and possibly different follower symbols. This closure process continues until all follower symbols have been expanded. The follower nonterminals for state 2 begins with Products. Value is then added by closure. The follower terminals are int and id. The kernel and closure items together show all possible legal ways to proceed from the current state to future states and complete phrases. If a follower symbol appears in only one item, it leads to a next state containing only one core item with the marker advanced. So int leads to next state 8 with core r6: Value id If the same follower symbol appears in several items, the parser cannot yet tell which rule applies here. So that symbol leads to a next state that shows all remaining possibilities, again with the marker advanced. Products appears in both r1 and r3. So Products leads to next state 4 with core r1: Sums Sums + Products r3: Products Products * Value In words, that means if you've seen a single Products, you might be done, or you might still have even more things to multiply together. Note that all the core items have the same symbol preceding the marker; all transitions into this state are always with that same symbol. Some transitions will be to cores and states that have been enumerated already. Other transitions lead to new states. The generator starts with the grammar's goal rule. From there it keeps exploring known states and transitions until all

LR parser needed states have been found. These states are called "LR(0)" states because they use a lookahead of k=0, i.e. no lookahead. The only checking of input symbols occurs when the symbol is shifted in. Checking of lookaheads for reductions is done separately by the parse table, not by the enumerated states themselves.

35

Finite State Machine


The parse table describes all possible LR(0) states and their transitions. They form a finite state machine. An FSM is a simple engine for parsing simple unnested languages, without using a stack. In this LR application, the FSM's modified "input language" has both terminal and nonterminal symbols, and covers any partially parsed stack snapshot of the full LR parse. Recall step 5 of the Parse Steps Example:
Step Parse Stack Look Unscanned Symbol state ... Ahead state
0

Products4 *5 int8

The parse stack shows a series of state transitions, from the start state 0, to state 4 and then on to 5 and current state 8. The symbols on the parse stack are the shift or goto symbols for those transitions. Another way to view this, is that the finite state machine can scan the stream "Products*int+1" (without using yet another stack) and find the leftmost complete phrase that should be reduced next. And that is indeed its job! How can a mere FSM do this, when the original unparsed language has nesting and recursion and definitely requires an analyzer with a stack? The trick is that everything to the left of the stack top has already been fully reduced. This eliminates all the loops and nesting from those phrases. The FSM can ignore all the older beginnings of phrases, and track just the newest phrases that might be completed next. The obscure name for this in LR theory is "viable prefix".

Lookahead Sets
The states and transitions give all the needed information for the parse table's shift actions and goto actions. The generator also needs to calculate the expected lookahead sets for each reduce action. In SLR parsers, these lookahead sets are determined directly from the grammar, without considering the individual states and transitions. For each nonterminal S, the SLR generator works out Follows(S), the set of all the terminal symbols which can immediately follow some occurrence of S. In the parse table, each reduction to S uses Follow(S) as its LR(1) lookahead set. Such follow sets are also used by generators for LL top-down parsers. A grammar that has no shift/reduce or reduce/reduce conflicts when using Follow sets is called an SLR grammar. LALR parsers have the same states as SLR parsers, but use a more complicated, more precise way of working out the minimum necessary reduction lookaheads for each individual state. Depending on the details of the grammar, this may turn out to be the same as the Follow set computed by SLR parser generators, or it may turn out to be a subset of the SLR lookaheads. Some grammars are okay for LALR parser generators but not for SLR parser generators. This happens when the grammar has spurious shift/reduce or reduce/reduce conflicts using Follow sets, but no conflicts when using the exact sets computed by the LALR generator. The grammar is then called LALR(1) but not SLR. An SLR or LALR parser avoids having duplicate states. But this minimization is not necessary, and can sometimes create unnecessary lookahead conflicts. Canonical LR parsers use duplicated (or "split") states to better remember the left and right context of a nonterminal's use. Each occurrence of a symbol S in the grammar can be treated independently with its own lookahead set, to help resolve reduction conflicts. This handles a few more grammars. Unfortunately, this greatly magnifies the size of the parse tables if done for all parts of the grammar. This splitting of states can also be done manually and selectively with any SLR or LALR parser, by making two or more named

LR parser copies of some nonterminals. A grammar that is conflict-free for a canonical LR generator but has conflicts in an LALR generator is called LR(1) but not LALR(1), and not SLR. SLR, LALR, and canonical LR parsers make exactly the same shift and reduce decisions when the input stream is correct language. When the input has a syntax error, the LALR parser may do some additional (harmless) reductions before detecting the error than would the canonical LR parser. And the SLR parser may do even more. This happens because the SLR and LALR parsers are using a generous superset approximation to the true, minimal lookahead symbols for that particular state.

36

Syntax Error Recovery


LR parsers can generate somewhat helpful error messages for the first syntax error in a program, by simply enumerating all the terminal symbols that could have appeared next instead of the unexpected bad lookahead symbol. But this does not help the parser work out how to parse the remainder of the input program to look for further, independent errors. If the parser recovers badly from the first error, it is very likely to mis-parse everything else and produce a cascade of unhelpful spurious error messages. In the yacc and bison parser generators, the parser has an ad hoc mechanism to abandon the current statement, discard some parsed phrases and lookahead tokens surrounding the error, and resynchronize the parse at some reliable statement-level delimiter like semicolons or braces. This often works well for allowing the parser and compiler to look over the rest of the program. Many syntactic coding errors are simple typos or omissions of a trivial symbol. Some LR parsers attempt to detect and automatically repair these common cases. The parser enumerates every possible single-symbol insertion, deletion, or substitution at the error point. The compiler does a trial parse with each change to see if it worked okay. (This requires backtracking to snapshots of the parse stack and input stream, normally unneeded by the parser.) Some best repair is picked. This gives a very helpful error message and resynchronizes the parse well. However, the repair is not trustworthy enough to permanently modify the input file. Repair of syntax errors is easiest to do consistently in parsers (like LR) that have parse tables and an explicit data stack.

Variants of LR Parsers
The LR parser generator decides what should happen for each combination of parser state and lookahead symbol. These decisions are usually turned into read-only data tables that drive a generic parser loop that is grammar- and state-independent. But there are also other ways to turn those decisions into an active parser. Some LR parser generators create separate tailored program code for each state, rather than a parse table. These parsers can run several times faster than the generic parser loop in table-driven parsers. The fastest parsers use generated assembler code. In the recursive ascent parser variation, the explicit parse stack structure is also replaced by the implicit stack used by subroutine calls. Reductions terminate several levels of subroutine calls, which is clumsy in most languages. So recursive ascent parsers are generally slower, less obvious, and harder to hand-modify than recursive descent parsers. Another variation replaces the parse table by pattern-matching rules in non-procedural languages such as Prolog. GLR Generalized LR parsers use LR bottom-up techniques to find all possible parses of input text, not just one correct parse. This is essential for highly ambiguous grammars such as for human languages. The multiple valid parse trees are computed simultaneously, without backtracking. GLR is sometimes helpful for computer languages that are not easily described by an unambiguous, conflict-free LALR(1) grammar. Left corner parsers use LR bottom-up techniques for recognizing the left end of alternative grammar rules. When the alternatives have been narrowed down to a single possible rule, the parser then switches to top-down LL(1) techniques for parsing the rest of that rule. LC parsers have smaller parse tables than LALR parsers and better error diagnostics. There are no widely used generators for deterministic LC parsers. Multiple-parse LC parsers are helpful

LR parser with human languages with very large grammars.

37

Theory
LR parsers were invented by Donald Knuth in 1965 as an efficient generalization of precedence parsers. Knuth proved that LR parsers were the most general-purpose parsers possible that would still be efficient in the worst cases. "LR(k) grammars can be efficiently parsed with an execution time essentially proportional to the length of the string." "A language can be generated by an LR(k) grammar if and only if it is deterministic, if and only if it can be generated by an LR(1) grammar."[1] In other words, if a language was reasonable enough to allow an efficient one-pass parser, it could be described by an LR(k) grammar. And that grammar could always be mechanically transformed into an equivalent (but larger) LR(1) grammar. So an LR(1) parsing method was, in theory, powerful enough to handle any reasonable language. In practice, the natural grammars for many programming languages are close to being LR(1). The canonical LR parsers described by Knuth had too many states and very big parse tables that were impractically large for the limited memory of computers of that era. LR parsing became practical when Frank DeRemer invented SLR and LALR parsers with much fewer states.[6][7] For full details on LR theory and how LR parsers are derived from grammars, see [8] if you can find it, otherwise see their current textbook.[9] Earley parsers apply the techniques and notation of LR parsers to the task of generating all possible parses for ambiguous grammars such as for human languages.

Additional Example 1+1


This example of LR parsing uses the following small grammar with goal symbol E: (1) E E * B (2) E E + B (3) E B (4) B 0 (5) B 1 to parse the following input: 1+1
Bottom-up parse of 1+1

Action and goto tables The two LR(0) parsing tables for this grammar look as follows:

LR parser

38

state * 0 1 2 3 4 5 6 7 8 +

action 0 1 $

goto E B 3 4 r4 r5 acc r3 7 8 r1 r2

s1 s2 r4 r4 r4 r4 r5 r5 r5 r5 s5 s6 r3 r3 r3 r3 s1 s2 s1 s2 r1 r1 r1 r1 r2 r2 r2 r2

The action table is indexed by a state of the parser and a terminal (including a special terminal $ that indicates the end of the input stream) and contains three types of actions: shift, which is written as 'sn' and indicates that the next state is n reduce, which is written as 'rm' and indicates that a reduction with grammar rule m should be performed accept, which is written as 'acc' and indicates that the parser accepts the string in the input stream. The goto table is indexed by a state of the parser and a nonterminal and simply indicates what the next state of the parser will be if it has recognized a certain nonterminal. This table is important to find out the next state after every reduction. After a reduction, the next state is found by looking up the goto table entry for top of the stack (i.e. current state) and the reduced rule's LHS (i.e. non-terminal). Parsing Steps The table below illustrates each step in the process. Here the state refers to the element at the top of the stack (the right-most element), and the next action is determined by referring to the action table above. Also note that a $ is appended to the input string to denote the end of the stream.
State Input stream Output stream 0 2 4 3 6 2 8 3 1+1$ +1$ +1$ +1$ 1$ $ $ $ 5 5,3 5,3 5,3 5,3,5 5,3,5,2 Stack [0] [0,2] [0,4] [0,3] [0,3,6] Next action Shift 2 Reduce 5 Reduce 3 Shift 6 Shift 2

[0,3,6,2] Reduce 5 [0,3,6,8] Reduce 2 [0,3] Accept

LR parser Walkthrough The parser starts out with the stack containing just the initial state ('0'): [0] The first symbol from the input string that the parser sees is '1'. In order to find out what the next action is (shift, reduce, accept or error), the action table is indexed with the current state (remember that the "current state" is just whatever is on the top of the stack), which in this case is 0, and the current input symbol, which is '1'. The action table specifies a shift to state 2, and so state 2 is pushed onto the stack (again, remember that all the state information is in the stack, so "shifting to state 2" is the same thing as pushing 2 onto the stack). The resulting stack is [0 '1' 2] where the top of the stack is 2. For the sake of explanation we also show the symbol (e.g., '1', B) that caused the transition to the next state, although strictly speaking it is not part of the stack. In state 2 the action table says that regardless of what terminal we see on the input stream, we should do a reduction with grammar rule 5. If the table is correct, this means that the parser has just recognized the right-hand side of rule 5, which is indeed the case. In this case we write 5 to the output stream, pop one state from the stack (since the right-hand side of the rule has one symbol), and push on the stack the state from the cell in the goto table for state 0 and B, i.e., state 4. The resulting stack is: [0 B 4] However, in state 4 the action table says we should now do a reduction with rule 3. So we write 3 to the output stream, pop one state from the stack, and find the new state in the goto table for state 0 and E, which is state 3. The resulting stack: [0 E 3] The next terminal that the parser sees is a '+' and according to the action table it should then go to state 6: [0 E 3 '+' 6] Note that the resulting stack can be interpreted as the history of a finite state automaton that has just read a nonterminal E followed by a terminal '+'. The transition table of this automaton is defined by the shift actions in the action table and the goto actions in the goto table. The next terminal is now '1' and this means that we perform a shift and go to state 2: [0 E 3 '+' 6 '1' 2] Just as the previous '1' this one is reduced to B giving the following stack: [0 E 3 '+' 6 B 8] Again note that the stack corresponds with a list of states of a finite automaton that has read a nonterminal E, followed by a '+' and then a nonterminal B. In state 8 we always perform a reduce with rule 2. Note that the top 3 states on the stack correspond with the 3 symbols in the right-hand side of rule 2. [0 E 3] Finally, we read a '$' from the input stream which means that according to the action table (the current state is 3) the parser accepts the input string. The rule numbers that will then have been written to the output stream will be [5, 3, 5, 2] which is indeed a rightmost derivation of the string "1 + 1" in reverse.

39

LR parser

40

Constructing LR(0) parsing tables


Items
The construction of these parsing tables is based on the notion of LR(0) items (simply called items here) which are grammar rules with a special dot added somewhere in the right-hand side. For example the rule E E + B has the following four corresponding items: E E+B EE +B EE+ B EE+B Rules of the form A have only a single item A . The item E E + B, for example, indicates that the parser has recognized a string corresponding with E on the input stream and now expects to read a '+' followed by another string corresponding with B.

Item sets
It is usually not possible to characterize the state of the parser with a single item because it may not know in advance which rule it is going to use for reduction. For example if there is also a rule E E * B then the items E E + B and E E * B will both apply after a string corresponding with E has been read. Therefore we will characterize the state of the parser by a set of items, in this case the set { E E + B, E E * B }.

Extension of Item Set by expansion of non-terminals


An item with a dot before a nonterminal, such as E E + B, indicates that the parser expects to parse the nonterminal B next. To ensure the item set contains all possible rules the parser may be in the midst of parsing, it must include all items describing how B itself will be parsed. This means that if there are rules such as B 1 and B 0 then the item set must also include the items B 1 and B 0. In general this can be formulated as follows: If there is an item of the form A v Bw in an item set and in the grammar there is a rule of the form B w' then the item B w' should also be in the item set.

Closure of item sets


Thus, any set of items can be extended by recursively adding all the appropriate items until all nonterminals preceded by dots are accounted for. The minimal extension is called the closure of an item set and written as clos(I) where I is an item set. It is these closed item sets that we will take as the states of the parser, although only the ones that are actually reachable from the begin state will be included in the tables.

Augmented grammar
Before we start determining the transitions between the different states, the grammar is always augmented with an extra rule (0) S E where S is a new start symbol and E the old start symbol. The parser will use this rule for reduction exactly when it has accepted the input string. For our example we will take the same grammar as before and augment it: (0) S E (1) E E * B

LR parser (2) E E + B (3) E B (4) B 0 (5) B 1 It is for this augmented grammar that we will determine the item sets and the transitions between them.

41

Table construction
Finding the reachable item sets and the transitions between them
The first step of constructing the tables consists of determining the transitions between the closed item sets. These transitions will be determined as if we are considering a finite automaton that can read terminals as well as nonterminals. The begin state of this automaton is always the closure of the first item of the added rule: S E: Item set 0 S E +E E*B +E E+B +E B +B 0 +B 1 The boldfaced "+" in front of an item indicates the items that were added for the closure (not to be confused with the mathematical '+' operator which is a terminal). The original items without a "+" are called the kernel of the item set. Starting at the begin state (S0) we will now determine all the states that can be reached from this state. The possible transitions for an item set can be found by looking at the symbols (terminals and nonterminals) we find right after the dots; in the case of item set 0 those symbols are the terminals '0' and '1' and the nonterminals E and B. To find the item set that each symbol x leads to we follow the following procedure for each of the symbols: 1. Take the subset, S, of all items in the current item set where there is a dot in front of the symbol of interest, x. 2. For each item in S, move the dot to the right of x. 3. Close the resulting set of items. For the terminal '0' (i.e. where x = '0') this results in: Item set 1 B0 and for the terminal '1' (i.e. where x = '1') this results in: Item set 2 B1 and for the nonterminal E (i.e. where x = E) this results in: Item set 3 SE EE *B EE +B and for the nonterminal B (i.e. where x = B) this results in: Item set 4

LR parser EB Note that the closure does not add new items in all cases - in the new sets above, for example, there are no nonterminals following the dot. We continue this process until no more new item sets are found. For the item sets 1, 2, and 4 there will be no transitions since the dot is not in front of any symbol. For item set 3 we see that the dot is in front of the terminals '*' and '+'. For '*' the transition goes to: Item set 5 EE* B +B 0 +B 1 and for '+' the transition goes to: Item set 6 EE+ B +B 0 +B 1 For item set 5 we have to consider the terminals '0' and '1' and the nonterminal B. For the terminals we see that the resulting closed item sets are equal to the already found item sets 1 and 2, respectively. For the nonterminal B the transition goes to: Item set 7 EE*B For item set 6 we also have to consider the terminal '0' and '1' and the nonterminal B. As before, the resulting item sets for the terminals are equal to the already found item sets 1 and 2. For the nonterminal B the transition goes to: Item set 8 EE+B These final item sets have no symbols beyond their dots so no more new item sets are added and we are finished. The finite automaton, with item sets as its states is shown below. The transition table for the automaton now looks as follows:
Item Set * + 0 1 E B 0 1 2 3 4 5 6 7 8 1 2 1 2 7 8 5 6 1 2 3 4

42

LR parser

43

Constructing the action and goto tables


From this table and the found item sets we construct the action and goto table as follows: 1. The columns for nonterminals are copied to the goto table. 2. The columns for the terminals are copied to the action table as shift actions. 3. An extra column for '$' (end of input) is added to the action table that contains acc for every item set that contains S E . 4. If an item set i contains an item of the form A w and A w is rule m with m > 0 then the row for state i in the action table is completely filled with the reduce action rm. The reader may verify that this results indeed in the action and goto table that were presented earlier on. A note about LR(0) versus SLR and LALR parsing Note that only step 4 of the above procedure produces reduce actions, and so all reduce actions must occupy an entire table row, causing the reduction to occur regardless of the next symbol in the input stream. This is why these are LR(0) parse tables: they don't do any lookahead (that is, they look ahead zero symbols) before deciding which reduction to perform. A grammar that needs lookahead to disambiguate reductions would require a parse table row containing different reduce actions in different columns, and the above procedure is not capable of creating such rows. Refinements to the LR(0) table construction procedure (such as SLR and LALR) are capable of constructing reduce actions that do not occupy entire rows. Therefore, they are capable of parsing more grammars than LR(0) parsers.

Conflicts in the constructed tables


The automaton is constructed in such a way that it is guaranteed to be deterministic. However, when reduce actions are added to the action table it can happen that the same cell is filled with a reduce action and a shift action (a shift-reduce conflict) or with two different reduce actions (a reduce-reduce conflict). However, it can be shown that when this happens the grammar is not an LR(0) grammar. A small example of a non-LR(0) grammar with a shift-reduce conflict is: (1) E 1 E (2) E 1 One of the item sets we then find is: Item set 1 E1 E E1 +E 1E +E 1 There is a shift-reduce conflict in this item set because in the cell in the action table for this item set and the terminal '1' there will be both a shift action to state 1 and a reduce action with rule 2. A small example of a non-LR(0) grammar with a reduce-reduce conflict is: (1) E A 1 (2) E B 2 (3) A 1 (4) B 1 In this case we obtain the following item set: Item set 1

LR parser A1 B1 There is a reduce-reduce conflict in this item set because in the cells in the action table for this item set there will be both a reduce action for rule 3 and one for rule 4. Both examples above can be solved by letting the parser use the follow set (see LL parser) of a nonterminal A to decide if it is going to use one of As rules for a reduction; it will only use the rule A w for a reduction if the next symbol on the input stream is in the follow set of A. This solution results in so-called Simple LR parsers.

44

References
[1] Knuth, Donald (July 1965). "On the Translation of Languages from Left to Right" (http:/ / www. cs. dartmouth. edu/ ~mckeeman/ cs48/ mxcom/ doc/ knuth65. pdf). Information and Control 8: 707639. . Retrieved 29 May 2011. [2] Language theoretic comparison of LL and LR grammars (http:/ / cs. stackexchange. com/ questions/ 43/ language-theoretic-comparison-of-ll-and-lr-grammars) [3] Engineering a Compiler (2nd edition), by Keith Cooper and Linda Torczon, Morgan Kaufman 2011. [4] Crafting and Compiler, by Charles Fischer, Ron Cytron, and Richard LeBlanc, Addison Wesley 2009. [5] Flex & Bison: Text Processing Tools, by John Levine, O'Reilly Media 2009. [6] Practical Translators for LR(k) Languages, by Frank DeRemer, MIT PhD dissertation 1969. [7] Simple LR(k) Grammars, by Frank DeRemer, Comm. ACM 14:7 1971. [8] The Theory of Parsing, Translation, and Compiling, Volume 1: Parsing, by Alfred Aho and Jeffrey Ullman, Prentice Hall 1972. [9] Compilers: Principles, Techniques, and Tools (2nd Edition), by Alfred Aho, Monica Lam, Ravi Sethi, and Jeffrey Ullman, Prentice Hall 2006.

External links dickgrune.com (http://dickgrune.com/Books/PTAPG_1st_Edition/), Parsing Techniques - A Practical Guide 1st Ed. web page of book includes downloadable pdf. Parsing Simulator (http://www.supereasyfree.com/software/simulators/compilers/ principles-techniques-and-tools/parsing-simulator/parsing-simulator.php) This simulator is used to generate parsing tables LR and to resolve the exercises of the book Internals of an LALR(1) parser generated by GNU Bison (http://www.cs.uic.edu/~spopuri/cparser.html) Implementation issues Course notes on LR parsing (http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2004S/Outlines/outline. 20.html) Shift-reduce and Reduce-reduce conflicts in an LALR parser (http://www.gobosoft.com/eiffel/gobo/geyacc/ algorithm.html) A LR parser example (http://www.cs.uky.edu/~lewis/essays/compilers/bu-parse.html) Practical LR(k) Parser Construction (http://david.tribble.com/text/lrk_parsing.html) The Honalee LR(k) Algorithm (http://david.tribble.com/text/honalee.html) Further offline reading Chapman, Nigel P., LR Parsing: Theory and Practice (http://books.google.com/books?id=nEA9AAAAIAAJ& printsec=frontcover), Cambridge University Press, 1987. ISBN 0-521-30413-X Pager, D., A Practical General Method for Constructing LR(k) Parsers. Acta Informatica 7, 249 - 268 (1977)

Syntax-directed translation

45

Syntax-directed translation
In computer programming, Syntax-directed translation (SDT) is a method of translating a string into a sequence of actions by attaching one such action to each rule of a grammar.[1] Thus, parsing a string of the grammar produces a sequence of rule applications. And SDT provides a simple way to attach semantics to any such syntax.

Overview
Syntax-directed translation fundamentally works by adding actions to the productions in a context-free grammar. Actions are steps or procedures that will be carried out when that production is used in a derivation. A grammar specification embedded with actions to be performed is called a syntax-directed translation scheme[2] (sometimes simply called a 'translation scheme'.) Each symbol in the grammar can have an attribute, which is a value that is to be associated with the symbol. Common attributes could include a variable type, the value of an expression, etc. Given a symbol X, with an attribute t, that attribute is referred to as X.t Thus, given actions and attributes, the grammar can be used for translating strings from its language by applying the actions and carrying information through each symbol's attribute.

References
[1] Gurari, Eitan M. "Syntax-Directed Translation Schemes (SDTS's)." Web. 23 Sept. 2010. <http://www.cse.ohio-state.edu/~gurari/course/cse756/cse756su33.xht>. [2] Aho, Alfred V. Compilers: Principles, Techniques, & Tools. Boston: Pearson/Addison Wesley, 2007. Print.

Context-free grammar
In formal language theory, a context-free grammar (CFG) is a formal grammar in which every production rule is of the form Vw where V is a single nonterminal symbol, and w is a string of terminals and/or nonterminals (w can be empty). The languages generated by context-free grammars are known as the context-free languages. A formal grammar is considered "context free" when its production rules can be applied regardless of the context of a nonterminal. Context-free grammars are important in linguistics for describing the structure of sentences and words in natural language, and in computer science for describing the structure of programming languages and other formal languages. In linguistics, some authors use the term phrase structure grammar to refer to context-free grammars, whereby phrase structure grammars are distinct from dependency grammars. In computer science, a popular notation for context-free grammars is BackusNaur Form, or BNF.

Context-free grammar

46

Background
Since the time of Pini, at least, linguists have described the grammars of languages in terms of their block structure, and described how sentences are recursively built up from smaller phrases, and eventually individual words or word elements. An essential property of these block structures is that logical units never overlap. For example, the sentence: John, whose blue car was in the garage, walked to the grocery store. can be logically parenthesized as follows: (John, ((whose blue car) (was (in the garage))), (walked (to (the grocery store)))). A context-free grammar provides a simple and mathematically precise mechanism for describing the methods by which phrases in some natural language are built from smaller blocks, capturing the "block structure" of sentences in a natural way. Its simplicity makes the formalism amenable to rigorous mathematical study. Important features of natural language syntax such as agreement and reference are not part of the context-free grammar, but the basic recursive structure of sentences, the way in which clauses nest inside other clauses, and the way in which lists of adjectives and adverbs are swallowed by nouns and verbs, is described exactly. The formalism of context-free grammars was developed in the mid-1950s by Noam Chomsky,[1] and also their classification as a special type of formal grammar (which he called phrase-structure grammars).[2] What Chomsky called a phrase structure grammar is also known now as a constituency grammar, whereby constituency grammars stand in contrast to dependency grammars. In Chomsky's generative grammar framework, the syntax of natural language was described by context-free rules combined with transformation rules. Block structure was introduced into computer programming languages by the Algol project (19571960), which, as a consequence, also featured a context-free grammar to describe the resulting Algol syntax. This became a standard feature of computer languages, and the notation for grammars used in concrete descriptions of computer languages came to be known as Backus-Naur Form, after two members of the Algol language design committee.[1] The "block structure" aspect that context-free grammars capture is so fundamental to grammar that the terms syntax and grammar are often identified with context-free grammar rules, especially in computer science. Formal constraints not captured by the grammar are then considered to be part of the "semantics" of the language. Context-free grammars are simple enough to allow the construction of efficient parsing algorithms which, for a given string, determine whether and how it can be generated from the grammar. An Earley parser is an example of such an algorithm, while the widely used LR and LL parsers are simpler algorithms that deal only with more restrictive subsets of context-free grammars.

Formal definitions
A context-free grammar G is defined by the 4-tuple:[3] where 1. is a finite set; each element is called a non-terminal character or a variable. Each variable represents a different type of phrase or clause in the sentence. Variables are also sometimes called syntactic categories. Each variable defines a sub-language of the language defined by . 2. is a finite set of terminals, disjoint from , which make up the actual content of the sentence. The set of terminals is the alphabet of the language defined by the grammar 3. ) 4. of is the start variable (or start symbol), used to represent the whole sentence (or program). It must be an element . is a finite relation from members of to . , where the asterisk represents the Kleene star operation. The

are called the (rewrite) rules or productions of the grammar. (also commonly symbolized by a

Context-free grammar

47

Production rule notation


A production rule in is formalized mathematically as a pair , where is a non-terminal and . is is a string of variables and/or terminals; rather than using ordered pair notation, production rules are usually written using an arrow operator with as its left hand side and as its right hand side: It is allowed for to be the empty string, and in this case it is customary to denote it by . The form called an -production.
[4]

Rule application
For any strings such that to . , we say yields and , written as . Thus, , if with and is the result of applying the rule

Repetitive rule application


For any such that (or in some textbooks), if

Context-free language
The language of a grammar is the set

A language

is said to be a context-free language (CFL), if there exists a CFG

, such that

Proper CFGs
A context-free grammar is said to be proper, if it has no inaccessible symbols: no unproductive symbols: no -productions: (the right-arrow in this case denotes logical "implies" and not grammatical "yields") no cycles:

Example
The grammar S aSa, S bSb, S , is context-free. It is not proper since it includes an -production. A typical derivation in this grammar is S aSa aaSaa aabSbaa aabbaa. This makes it clear that proved that it is not regular. . The language is context-free, however it can be , with productions

Context-free grammar

48

Examples
Well-formed parentheses
The canonical example of a context free grammar is parenthesis matching, which is representative of the general case. There are two terminal symbols "(" and ")" and one nonterminal symbol S. The production rules are S SS S (S) S () The first rule allows Ss to multiply; the second rule allows Ss to become enclosed by matching parentheses; and the third rule terminates the recursion.

Well-formed nested parentheses and square brackets


A second canonical example is two different kinds of matching nested parentheses, described by the productions: S SS S () S (S) S [] S [S] with terminal symbols [ ] ( ) and nonterminal S. The following sequence can be derived in that grammar: ([ [ [ ()() [ ][ ] ] ]([ ]) ]) However, there is no context-free grammar for generating all sequences of two different types of parentheses, each separately balanced disregarding the other, but where the two types need not nest inside one another, for example: [ [ [ [(((( ] ] ] ]))))(([ ))(([ ))([ )( ])( ])( ])

A regular grammar
Every regular grammar is context-free, but not all context-free grammars are regular. The following context-free grammar, however, is also regular. Sa S aS S bS The terminals here are a and b, while the only non-terminal is S. The language described is all nonempty strings of s and s that end in . This grammar is regular: no rule has more than one nonterminal in its right-hand side, and each of these nonterminals is at the same end of the right-hand side. Every regular grammar corresponds directly to a nondeterministic finite automaton, so we know that this is a regular language. It is common to list all right-hand sides for the same left-hand side on the same line, using | (the pipe symbol) to separate them. Hence the grammar above can be described more tersely as follows: S a | aS | bS

Context-free grammar

49

Matching pairs
In a context-free grammar, we can pair up characters the way we do with brackets. The simplest example: S aSb S ab This grammar generates the language , which is not regular (according to the Pumping Lemma

for regular languages). The special character stands for the empty string. By changing the above grammar to S aSb | we obtain a grammar generating the language empty string while the original grammar did not. instead. This differs only in that it contains the

Algebraic expressions
Here is a context-free grammar for syntactically correct infix algebraic expressions in the variables x, y and z: 1. S x 2. S y 3. 4. 5. 6. 7. 8. Sz SS+S SS-S SS*S SS/S S(S)

This grammar can, for example, generate the string (x+y)*x-z*y/(x+x) as follows: S (the start symbol) S - S (by rule 5) S * S - S (by rule 6, applied to the leftmost S) S * S - S / S (by rule 7, applied to the rightmost S) ( S ) * S - S / S (by rule 8, applied to the leftmost S) ( S ) * S - S / ( S ) (by rule 8, applied to the rightmost S) ( S + S ) * S - S / ( S ) (etc.) (S+S)*S-S*S/(S) (S+S)*S-S*S/(S+S) (x+S)*S-S*S/(S+S) (x+y)*S-S*S/(S+S) (x+y)*x-S*y/(S+S) (x+y)*x-S*y/(x+S) (x+y)*x-z*y/(x+S) (x+y)*x-z*y/(x+x) Note that many choices were made underway as to which rewrite was going to be performed next. These choices look quite arbitrary. As a matter of fact, they are, in the sense that the string finally generated is always the same. For

Context-free grammar example, the second and third rewrites S * S - S (by rule 6, applied to the leftmost S) S * S - S / S (by rule 7, applied to the rightmost S) could be done in the opposite order: S - S / S (by rule 7, applied to the rightmost S) S * S - S / S (by rule 6, applied to the leftmost S) Also, many choices were made on which rule to apply to each selected S. Changing the choices made and not only the order they were made in usually affects which terminal string comes out at the end. Let's look at this in more detail. Consider the parse tree of this derivation: S | /|\ S - S / \ /|\ /|\ S * S S / S / | | \ /|\ x /|\ /|\ ( S ) S * S ( S ) / | | \ /|\ z y /|\ S + S S + S | | | | x y x x Starting at the top, step by step, an S in the tree is expanded, until no more unexpanded Ses (non-terminals) remain. Picking a different order of expansion will produce a different derivation, but the same parse tree. The parse tree will only change if we pick a different rule to apply at some position in the tree. But can a different parse tree still produce the same terminal string, which is ( x + y ) * x - z * y / ( x + x ) in this case? Yes, for this particular grammar, this is possible. Grammars with this property are called ambiguous. For example, x + y * z can be produced with these two different parse trees: S | /|\ S * S / /|\ S + S | | x y \ z x / S | /|\ S + S \ /|\ S * S | | y z

50

However, the language described by this grammar is not inherently ambiguous: an alternative, unambiguous grammar can be given for the language, for example: Tx

Context-free grammar Ty Tz SS+T SS-T SS*T SS/T T(S) ST (once again picking S as the start symbol). This alternative grammar will produce x + y * z with a parse tree similar to the left one above, i.e. implicitly assuming the association (x + y) * z, which is not according to standard operator precedence. More elaborate, unambiguous and context-free grammars can be constructed that produce parse trees that obey all desired operator precedence and associativity rules.

51

Further examples
Example 1 A context-free grammar for the language consisting of all strings over {a,b} containing an unequal number of a's and b's: SU|V U TaU | TaT V TbV | TbT T aTbT | bTaT | Here, the nonterminal T can generate all strings with the same number of a's as b's, the nonterminal U generates all strings with more a's than b's and the nonterminal V generates all strings with fewer a's than b's. Example 2 Another example of a non-regular language is generated by the following context-free grammar: S bSbb | A A aA | Other examples The formation rules for the terms and formulas of formal logic fit the definition of context-free grammar, except that the set of symbols may be infinite and there may be more than one start symbol. . It is context-free as it can be

Context-free grammar

52

Derivations and syntax trees


A derivation of a string for a grammar is a sequence of grammar rule applications that transforms the start symbol into the string. A derivation proves that the string belongs to the grammar's language. A derivation is fully determined by giving, for each step: the rule applied in that step the occurrence of its left hand side to which it is applied For clarity, the intermediate string is usually given as well. For instance, with the grammar: (1) (2) (3) S S + S S 1 S a

the string 1 + 1 + a can be derived with the derivation: S (rule 1 on first S) S+S (rule S+S+S (rule S+1+S (rule S+1+a (rule 1+1+a 1 on second S) 2 on second S) 3 on third S) 2 on first S)

Often, a strategy is followed that deterministically determines the next nonterminal to rewrite: in a leftmost derivation, it is always the leftmost nonterminal; in a rightmost derivation, it is always the rightmost nonterminal. Given such a strategy, a derivation is completely determined by the sequence of rules applied. For instance, the leftmost derivation S (rule 1 on first S) S+S (rule 2 on first S) 1+S (rule 1 on first S) 1+S+S (rule 2 on first S) 1+1+S (rule 3 on first S) 1+1+a can be summarized as

Context-free grammar rule 1, rule 2, rule 1, rule 2, rule 3 The distinction between leftmost derivation and rightmost derivation is important because in most parsers the transformation of the input is defined by giving a piece of code for every grammar rule that is executed whenever the rule is applied. Therefore it is important to know whether the parser determines a leftmost or a rightmost derivation because this determines the order in which the pieces of code will be executed. See for an example LL parsers and LR parsers. A derivation also imposes in some sense a hierarchical structure on the string that is derived. For example, if the string "1 + 1 + a" is derived according to the leftmost derivation: S S + S (1) 1 + S (2) 1 + S + S (1) 1 + 1 + S (2) 1 + 1 + a (3) the structure of the string would be: { { 1 }S + { { 1 }S + { a }S }S }S where { ... }S indicates a substring recognized as belonging to S. This hierarchy can also be seen as a tree: S /|\ / | \ / | \ S '+' S | /|\ | / | \ '1' S '+' S | | '1' 'a' This tree is called a concrete syntax tree (see also abstract syntax tree) of the string. In this case the presented leftmost and the rightmost derivations define the same syntax tree; however, there is another (rightmost) derivation of the same string S S + S (1) S + a (3) S + S + a (1) S + 1 + a (2) 1 + 1 + a (2) and this defines the following syntax tree: S /|\ / | \ / | \ S '+' S /|\ | / | \ |

53

Context-free grammar S '+' S | | '1' '1' 'a'

54

If, for certain strings in the language of the grammar, there is more than one parsing tree, then the grammar is said to be an ambiguous grammar. Such grammars are usually hard to parse because the parser cannot always decide which grammar rule it has to apply. Usually, ambiguity is a feature of the grammar, not the language, and an unambiguous grammar can be found that generates the same context-free language. However, there are certain languages that can only be generated by ambiguous grammars; such languages are called inherently ambiguous.

Normal forms
Every context-free grammar that does not generate the empty string can be transformed into one in which no rule has the empty string as a product [a rule with as a product is called an -production]. If it does generate the empty string, it will be necessary to include the rule , but there need be no other -rule. Every context-free grammar with no -production has an equivalent grammar in Chomsky normal form or Greibach normal form. "Equivalent" here means that the two grammars generate the same language. Because of the especially simple form of production rules in Chomsky Normal Form grammars, this normal form has both theoretical and practical implications. For instance, given a context-free grammar, one can use the Chomsky Normal Form to construct a polynomial-time algorithm that decides whether a given string is in the language represented by that grammar or not (the CYK algorithm).

Undecidable problems
Some questions that are undecidable for wider classes of grammars become decidable for context-free grammars; e.g. the emptiness problem (whether the grammar generates any terminal strings at all), is undecidable for context-sensitive grammars, but decidable for context-free grammars. Still, many problems remain undecidable. Examples:

Universality
Given a CFG, does it generate the language of all strings over the alphabet of terminal symbols used in its rules?[5][6] A reduction can be demonstrated to this problem from the well-known undecidable problem of determining whether a Turing machine accepts a particular input (the Halting problem). The reduction uses the concept of a computation history, a string describing an entire computation of a Turing machine. We can construct a CFG that generates all strings that are not accepting computation histories for a particular Turing machine on a particular input, and thus it will accept all strings only if the machine doesn't accept that input.

Context-free grammar

55

Language equality
Given two CFGs, do they generate the same language?[6][7] The undecidability of this problem is a direct consequence of the previous: we cannot even decide whether a CFG is equivalent to the trivial CFG defining the language of all strings.

Language inclusion
Given two CFGs, can the first generate all strings that the second can generate?[6][7] If this problem were decidable, then we could use it to determine whether two CFGs G1 and G2 generate the same language by checking whether L(G1) is a subset of L(G2) and L(G2) is a subset of L(G1).

Being in a lower or higher level of the Chomsky hierarchy


Using Greibach's theorem, it can be shown that the two following problems are undecidable: Given a context-sensitive grammar, does it describe a context-free language? Given a context-free grammar, does it describe a regular language?[6][7]

Extensions
An obvious way to extend the context-free grammar formalism is to allow nonterminals to have arguments, the values of which are passed along within the rules. This allows natural language features such as agreement and reference, and programming language analogs such as the correct use and definition of identifiers, to be expressed in a natural way. E.g. we can now easily express that in English sentences, the subject and verb must agree in number. In computer science, examples of this approach include affix grammars, attribute grammars, indexed grammars, and Van Wijngaarden two-level grammars. Similar extensions exist in linguistics. An extended context-free grammar is one in which the right-hand side of the production rules is allowed to be a regular expression over the grammar's terminals and nonterminals. Extended context-free grammars describe exactly the context-free languages.[8] Another extension is to allow additional terminal symbols to appear at the left hand side of rules, constraining their application. This produces the formalism of context-sensitive grammars.

Subclasses
There are a number of important subclasses of the context-free grammars: LR(k) grammars (also known as deterministic context-free grammars) allow parsing (string recognition) with deterministic pushdown automata, but they can only describe deterministic context-free languages. Simple LR, Look-Ahead LR grammars are subclasses that allow further simplification of parsing. LL(k) and LL(*) grammars allow parsing by direct construction of a leftmost derivation as described above, and describe even fewer languages. Simple grammars are a subclass of the LL(1) grammars mostly interesting for its theoretical property that language equality of simple grammars is decidable, while language inclusion is not. Bracketed grammars have the property that the terminal symbols are divided into left and right bracket pairs that always match up in rules. Linear grammars have no rules with more than one nonterminal in the right hand side. Regular grammars are a subclass of the linear grammars and describe the regular languages, i.e. they correspond to finite automata and regular expressions.

Context-free grammar LR parsing extends LL parsing to support a larger range of grammars; in turn, generalized LR parsing extends LR parsing to support arbitrary context-free grammars. On LL grammars and LR grammars, it essentially performs LL parsing and LR parsing, respectively, while on nondeterministic grammars, it is as efficient as can be expected. Although GLR parsing was developed in the 1980s, many new language definitions and parser generators continue to be based on LL, LALR or LR parsing up to the present day.

56

Linguistic applications
Chomsky initially hoped to overcome the limitations of context-free grammars by adding transformation rules.[2] Such rules are another standard device in traditional linguistics; e.g. passivization in English. Much of generative grammar has been devoted to finding ways of refining the descriptive mechanisms of phrase-structure grammar and transformation rules such that exactly the kinds of things can be expressed that natural language actually allows. Allowing arbitrary transformations doesn't meet that goal: they are much too powerful, being Turing complete unless significant restrictions are added (e.g. no transformations that introduce and then rewrite symbols in a context-free fashion). Chomsky's general position regarding the non-context-freeness of natural language has held up since then,[9] although his specific examples regarding the inadequacy of context-free grammars (CFGs) in terms of their weak generative capacity were later disproved.[10] Gerald Gazdar and Geoffrey Pullum have argued that despite a few context-sensitive constructions in natural language (such as cross-serial dependencies in Swiss German[9] and reduplication in Bambara[11]), the vast majority of forms in natural language are indeed context-free.[10]

Notes
[1] Hopcroft & Ullman (1979), p.106. [2] Chomsky, Noam (Sep 1956), "Three models for the description of language" (http:/ / ieeexplore. ieee. org/ iel5/ 18/ 22738/ 01056813. pdf?isnumber=22738& prod=STD& arnumber=1056813& arnumber=1056813& arSt=+ 113& ared=+ 124& arAuthor=+ Chomsky,+ N. ), Information Theory, IEEE Transactions 2 (3): 113124, doi:10.1109/TIT.1956.1056813, , retrieved 2007-06-18 [3] The notation here is that of Sipser (1997), p.94. Hopcroft & Ullman (1979) (p.79) define context-free grammars as 4-tuples in the same way, but with different variable names. [4] Hopcroft & Ullman (1979), pp. 9092. [5] Sipser (1997), Theorem 5.10, p. 181. [6] Hopcroft & Ullman (1979), p. 281. [7] Hazewinkel, Michiel (1994), Encyclopaedia of mathematics: an updated and annotated translation of the Soviet "Mathematical Encyclopaedia" (http:/ / books. google. com/ books?id=s9F71NJxwzoC& pg=PA56), Springer, Vol. IV, p. 56, ISBN978-1-55608-003-6, . [8] Norvell, Theodore. "A Short Introduction to Regular Expressions and Context-Free Grammars" (http:/ / www. engr. mun. ca/ ~theo/ Courses/ fm/ pub/ context-free. pdf). pp.4. . Retrieved August 24, 2012. [9] Shieber, Stuart (1985), "Evidence against the context-freeness of natural language" (http:/ / www. eecs. harvard. edu/ ~shieber/ Biblio/ Papers/ shieber85. pdf), Linguistics and Philosophy 8 (3): 333343, doi:10.1007/BF00630917, . [10] Pullum, Geoffrey K.; Gerald Gazdar (1982), "Natural languages and context-free languages", Linguistics and Philosophy 4 (4): 471504, doi:10.1007/BF00360802. [11] Culy, Christopher (1985), "The Complexity of the Vocabulary of Bambara", Linguistics and Philosophy 8 (3): 345351, doi:10.1007/BF00630918.

Context-free grammar

57

References
Hopcroft, John E.; Ullman, Jeffrey D. (1979), Introduction to Automata Theory, Languages, and Computation, Addison-Wesley. Chapter 4: Context-Free Grammars, pp.77106; Chapter 6: Properties of Context-Free Languages, pp.125137. Sipser, Michael (1997), Introduction to the Theory of Computation, PWS Publishing, ISBN0-534-94728-X. Chapter 2: Context-Free Grammars, pp.91122; Section 4.1.2: Decidable problems concerning context-free languages, pp.156159; Section 5.1.1: Reductions via computation histories: pp.176183.

Run-time system
A run-time system or runtime environment (also called runtime system or just runtime) is a software component designed to support the execution of computer programs written in some computer language. The run-time system contains implementations of basic low-level commands and may also implement higher-level commands and may support type checking, debugging, and even code generation and optimization. Some services of the run-time system are accessible to the programmer through an application programming interface, but other services (such as task scheduling and resource management) may be inaccessible. A run-time system relieves programmers from the burden of writing code for mundane tasks such as drawing text on the screen or making an Internet connection. It also provides an abstraction layer that hides the complexity or variations in the services offered by the operating system. In the limit, the run-time system may be a P-code machine or virtual machine, that hides even the processor's instruction set. This is the approach followed by many interpreted languages such as AWK, and some languages like Java that are meant to be compiled into some machine-independent pseudocode (bytecode). This arrangement greatly simplifies the task of language implementation and its adaptation to different machines, and allows sophisticated language features such as reflection. It also allows the same program to be executed on any machine without recompiling, a feature that has become very important since the diffusion of the World Wide Web.

History
In vFortran, the run-time system consisted basically of the standard library subroutines, such as SQRT() (square root) and the routines that implemented the FORMAT specs. Notable early examples of run-time systems are the interpreters for BASIC and Lisp. The latter included also a garbage collector. Forth is an early example of a language that was designed to be compiled into pseudocode; its run-time system was a virtual machine that interpreted that pseudocode. Another popular, if theoretical, example is Donald Knuth's MIX computer. In C and later languages that supported dynamic memory allocation, the runtime also included a library that managed the program's memory pool; the minimal startup runtime is crt0 (C RunTime time 0). In the object-oriented programming languages, the run-time system was often responsible also for dynamic type checking and resolving method references.

RSTS/E
An early example of run-time system is that included in the RSTS operating system. The "run-time system" (RTS) was the resident code (that is, code always in memory) which was mapped into the high end of the address space of a process, and could be called to provide services to a program, or used as the command line interpreter (CLI) user interface. The term in RSTS for a command line interpreter was keyboard monitor (KBM). This term should not be confused with the similar term keystroke logging.

Run-time system RSTS did not itself define any standard filename extension for an executable program file; instead, each installed RTS defined its own extension, and the appropriate RTS was loaded depending on the actual name of the program file. If the user issued a command to run a program without specifying the extension, then the system went through all installed RTSs, trying each corresponding extension in turn until it found the name of an existing program file. The responsibility for loading and executing the program was left to the run-time system, so users could run interpreted programs just as easily as those compiled into actual machine code, if the interpreter was appropriately implemented as a run-time system. The standard BASIC-PLUS RTS provided a compiler, interpreter and KBM all packed into just 32KiB of code. And indeed many of the standard system utility programs (including privileged ones like LOGIN and LOGOUT) were written in BASIC-PLUS. Other RTSs provided some emulation facilities, allowing simple programs written for the RT-11 and RSX-11 operating systems to run under RSTS. An interesting note is that not all RTS's were designed as KBMs, however almost all KBMs were implemented as RTSes. Under RSTS, the Text Editor and Corrector (TECO) was implemented as an RTS, though its KBM function was a stub which loaded a program with a predefined name which had to be provided by the user. This allowed the user to implement their own KBM. Even the Space Invaders video arcade game was eventually implemented as a multi-user RTS (driving VT52 video terminals).

58

Code generation (compiler)


In computer science, code generation is the process by which a compiler's code generator converts some intermediate representation of source code into a form (e.g., machine code) that can be readily executed by a machine. Sophisticated compilers typically perform multiple passes over various intermediate forms. This multi-stage process is used because many algorithms for code optimization are easier to apply one at a time, or because the input to one optimization relies on the processing performed by another optimization. This organization also facilitates the creation of a single compiler that can target multiple architectures, as only the last of the code generation stages (the backend) needs to change from target to target. (For more information on compiler design, see Compiler.) The input to the code generator typically consists of a parse tree or an abstract syntax tree. The tree is converted into a linear sequence of instructions, usually in an intermediate language such as three address code. Further stages of compilation may or may not be referred to as "code generation", depending on whether they involve a significant change in the representation of the program. (For example, a peephole optimization pass would not likely be called "code generation", although a code generator might incorporate a peephole optimization pass.)

Major tasks in code generation


In addition to the basic conversion from an intermediate representation into a linear sequence of machine instructions, a typical code generator tries to optimize the generated code in some way. Tasks which are typically part of a sophisticated compiler's "code generation" phase include: Instruction selection: which instructions to use. Instruction scheduling: in which order to put those instructions. Scheduling is a speed optimization that can have a critical effect on pipelined machines. Register allocation: the allocation of variables to processor registers[1] Debug data generation if required so the code can be debugged.

Code generation (compiler) Instruction selection is typically carried out by doing a recursive postorder traversal on the abstract syntax tree, matching particular tree configurations against templates; for example, the tree W := ADD(X,MUL(Y,Z)) might be transformed into a linear sequence of instructions by recursively generating the sequences for t1 := X and t2 := MUL(Y,Z), and then emitting the instruction ADD W, t1, t2. In a compiler that uses an intermediate language, there may be two instruction selection stages one to convert the parse tree into intermediate code, and a second phase much later to convert the intermediate code into instructions from the instruction set of the target machine. This second phase does not require a tree traversal; it can be done linearly, and typically involves a simple replacement of intermediate-language operations with their corresponding opcodes. However, if the compiler is actually a language translator (for example, one that converts Eiffel to C), then the second code-generation phase may involve building a tree from the linear intermediate code.

59

Runtime code generation


When code generation occurs at runtime, as in just-in-time compilation (JIT), it is important that the entire process be efficient with respect to space and time. For example, when regular expressions are interpreted and used to generate code at runtime, a non-determistic finite state machine is often generated instead of a deterministic one, because usually the former can be created more quickly and occupies less memory space than the latter. Despite its generally generating less efficient code, JIT code generation can take advantage of profiling information that is available only at runtime.

Related concepts
The fundamental task of taking input in one language and producing output in a non-trivially different language can be understood in terms of the core transformational operations of formal language theory. Consequently, some techniques that were originally developed for use in compilers have come to be employed in other ways as well. For example, YACC (Yet Another Compiler Compiler) takes input in Backus-Naur form and converts it to a parser in C. Though it was originally created for automatic generation of a parser for a compiler, yacc is also often used to automate writing code that needs to be modified each time specifications are changed. (For example, see [2].) Many integrated development environments (IDEs) support some form of automatic source code generation, often using algorithms in common with compiler code generators, although commonly less complicated. (See also: Program transformation, Data transformation.)

Reflection
In general, a syntax and semantic analyzer tries to retrieve the structure of the program from the source code, while a code generator uses this structural information (e.g., data types) to produce code. In other words, the former adds information while the latter loses some of the information. One consequence of this information loss is that reflection becomes difficult or even impossible. To counter this problem, code generators often embed syntactic and semantic information in addition to the code necessary for execution.

References
[1] Aho, Alfred V.; Ravi Sethi and Jeffrey D. Ullman (1987). Compilers: Principles, Techniques, and Tools. Addison-Wesley. p.15. ISBN0-201-10088-6. [2] http:/ / www. artima. com/ weblogs/ viewpost. jsp?thread=152273

Program optimization

60

Program optimization
In computer science, program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources.[1] In general, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less power.

General
Although the word "optimization" shares the same root as "optimal", it is rare for the process of optimization to produce a truly optimal system. The optimized system will typically only be optimal in one application or for one audience. One might reduce the amount of time that a program takes to perform some task at the price of making it consume more memory. In an application where memory space is at a premium, one might deliberately choose a slower algorithm in order to use less memory. Often there is no "one size fits all" design which works well in all cases, so engineers make trade-offs to optimize the attributes of greatest interest. Additionally, the effort required to make a piece of software completely optimal incapable of any further improvement is almost always more than is reasonable for the benefits that would be accrued; so the process of optimization may be halted before a completely optimal solution has been reached. Fortunately, it is often the case that the greatest improvements come early in the process.

"Levels" of optimization
Optimization can occur at a number of "levels": Design level At the highest level, the design may be optimized to make best use of the available resources. The implementation of this design will benefit from a good choice of efficient algorithms and the implementation of these algorithms will benefit from writing good quality code. The architectural design of a system overwhelmingly affects its performance. The choice of algorithm affects efficiency more than any other item of the design and, since the choice of algorithm usually is the first thing that must be decided, arguments against early or "premature optimization" may be hard to justify. In some cases, however, optimization relies on using more elaborate algorithms, making use of "special cases" and special "tricks" and performing complex trade-offs. A "fully optimized" program might be more difficult to comprehend and hence may contain more faults than unoptimized versions. Source code level Avoiding poor quality coding can also improve performance, by avoiding obvious "slowdowns". After that, however, some optimizations are possible that actually decrease maintainability. Some, but not all, optimizations can nowadays be performed by optimizing compilers. Compile level Use of an optimizing compiler tends to ensure that the executable program is optimized at least as much as the compiler can predict. Assembly level At the lowest level, writing code using an assembly language, designed for a particular hardware platform can produce the most efficient and compact code if the programmer takes advantage of the full repertoire of machine instructions. Many operating systems used on embedded systems have been traditionally written in assembler code for this reason. Programs (other than very small programs) are seldom written from start to finish in assembly due to the time and cost involved. Most are compiled down from a high level language to assembly and hand optimized

Program optimization from there. When efficiency and size are less important large parts may be written in a high-level language. With more modern optimizing compilers and the greater complexity of recent CPUs, it is more difficult to write code that is optimized better than the compiler itself generates, and few projects need resort to this "ultimate" optimization step. However, a large amount of code written today is still compiled with the intent to run on the greatest percentage of machines possible. As a consequence, programmers and compilers don't always take advantage of the more efficient instructions provided by newer CPUs or quirks of older models. Additionally, assembly code tuned for a particular processor without using such instructions might still be suboptimal on a different processor, expecting a different tuning of the code. Run time Just-in-time compilers and assembler programmers may be able to perform run time optimization exceeding the capability of static compilers by dynamically adjusting parameters according to the actual input or other factors.

61

Platform dependent and independent optimizations


Code optimization can be also broadly categorized as platform-dependent and platform-independent techniques. While the latter ones are effective on most or all platforms, platform-dependent techniques use specific properties of one platform, or rely on parameters depending on the single platform or even on the single processor. Writing or producing different versions of the same code for different processors might therefore be needed. For instance, in the case of compile-level optimization, platform-independent techniques are generic techniques (such as loop unrolling, reduction in function calls, memory efficient routines, reduction in conditions, etc.), that impact most CPU architectures in a similar way. Generally, these serve to reduce the total instruction path length required to complete the program and/or reduce total memory usage during the process. On the other hand, platform-dependent techniques involve instruction scheduling, instruction-level parallelism, data-level parallelism, cache optimization techniques (i.e., parameters that differ among various platforms) and the optimal instruction scheduling might be different even on different processors of the same architecture.

Different algorithms
Computational tasks can be performed in several different ways with varying efficiency. For example, consider the following C code snippet whose intention is to obtain the sum of all integers from 1 to N: int i, sum = 0; for (i = 1; i <= N; ++i) { sum += i; } printf("sum: %d\n", sum); This code can (assuming no arithmetic overflow) be rewritten using a mathematical formula like: int sum = N * (1 + N) / 2; printf("sum: %d\n", sum); The optimization, sometimes performed automatically by an optimizing compiler, is to select a method (algorithm) that is more computationally efficient, while retaining the same functionality. See algorithmic efficiency for a discussion of some of these techniques. However, a significant improvement in performance can often be achieved by removing extraneous functionality. Optimization is not always an obvious or intuitive process. In the example above, the "optimized" version might actually be slower than the original version if N were sufficiently small and the particular hardware happens to be

Program optimization much faster at performing addition and looping operations than multiplication and division.

62

Trade-offs
Optimization will generally focus on improving just one or two aspects of performance: execution time, memory usage, disk space, bandwidth, power consumption or some other resource. This will usually require a trade-off where one factor is optimized at the expense of others. For example, increasing the size of cache improves runtime performance, but also increases the memory consumption. Other common trade-offs include code clarity and conciseness. There are instances where the programmer performing the optimization must decide to make the software better for some operations but at the cost of making other operations less efficient. These trade-offs may sometimes be of a non-technical nature such as when a competitor has published a benchmark result that must be beaten in order to improve commercial success but comes perhaps with the burden of making normal usage of the software less efficient. Such changes are sometimes jokingly referred to as pessimizations. In some cases, a snippet of code can become so optimized as to be obfuscated. This can lead to difficulty in maintaining the code.

Bottlenecks
Optimization may include finding a bottleneck, a critical part of the code that is the primary consumer of the needed resource sometimes known as a hot spot. Often, the Pareto principle is applied. i.e., 20% of the code is responsible for 80% of the results. In computer science, the Pareto principle can be applied to resource optimization by observing that 80% of the resources are typically used by 20% of the operations. In software engineering, it is often a better approximation that 90% of the execution time of a computer program is spent executing 10% of the code (known as the 90/10 law in this context). More complex algorithms and data structures perform well with many items, while simple algorithms are more suitable for small amounts of data the setup, initialization time, and constant factors of the more complex algorithm can outweigh the benefit. In some cases, adding more memory can help to make a program run faster. For example, a filtering program will commonly read each line and filter and output that line immediately. This only uses enough memory for one line, but performance is typically poor. Performance can be greatly improved by reading the entire file then writing the filtered result, though this uses much more memory. Caching the result is similarly effective, though also requiring larger memory use.

When to optimize
Optimization can reduce readability and add code that is used only to improve the performance. This may complicate programs or systems, making them harder to maintain and debug. As a result, optimization or performance tuning is often performed at the end of the development stage. Donald Knuth made the following two statements on optimization: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"[2] (He also attributed the quote to Tony Hoare several years later,[3] although this might have been an error as Hoare disclaims having coined the phrase.[4]) "In established engineering disciplines a 12 % improvement, easily obtained, is never considered marginal and I believe the same viewpoint should prevail in software engineering"[2]

Program optimization "Premature optimization" is a phrase used to describe a situation where a programmer lets performance considerations affect the design of a piece of code. This can result in a design that is not as clean as it could have been or code that is incorrect, because the code is complicated by the optimization and the programmer is distracted by optimizing. When deciding whether to optimize a specific part of the program, Amdahl's Law should always be considered: the impact on the overall program depends very much on how much time is actually spent in that specific part, which is not always clear from looking at the code without a performance analysis. A better approach is therefore to design first, code from the design and then profile/benchmark the resulting code to see which parts should be optimized. A simple and elegant design is often easier to optimize at this stage, and profiling may reveal unexpected performance problems that would not have been addressed by premature optimization. In practice, it is often necessary to keep performance goals in mind when first designing software, but the programmer balances the goals of design and optimization.

63

Macros
Optimization during code development using macros takes on different forms in different languages. In some procedural languages, such as C and C++, macros are implemented using token substitution. Nowadays, inline functions can be used as a type safe alternative in many cases. In both cases, the inlined function body can then undergo further compile-time optimizations by the compiler, including constant folding, which may move some computations to compile time. In many functional programming languages macros are implemented using parse-time substitution of parse trees/abstract syntax trees, which it is claimed makes them safer to use. Since in many cases interpretation is used, that is one way to ensure that such computations are only performed at parse-time, and sometimes the only way. Lisp originated this style of macro, and such macros are often called "Lisp-like macros." A similar effect can be achieved by using template metaprogramming in C++. In both cases, work is moved to compile-time. The difference between C macros on one side, and Lisp-like macros and C++ template metaprogramming on the other side, is that the latter tools allow performing arbitrary computations at compile-time/parse-time, while expansion of C macros does not perform any computation, and relies on the optimizer ability to perform it. Additionally, C macros do not directly support recursion or iteration, so are not Turing complete. As with any optimization, however, it is often difficult to predict where such tools will have the most impact before a project is complete.

Automated and manual optimization


See also Category:Compiler optimizations Optimization can be automated by compilers or performed by programmers. Gains are usually limited for local optimization, and larger for global optimizations. Usually, the most powerful optimization is to find a superior algorithm. Optimizing a whole system is usually undertaken by programmers because it is too complex for automated optimizers. In this situation, programmers or system administrators explicitly change code so that the overall system performs better. Although it can produce better efficiency, it is far more expensive than automated optimizations. Use a profiler (or performance analyzer) to find the sections of the program that are taking the most resources the bottleneck. Programmers sometimes believe they have a clear idea of where the bottleneck is, but intuition is frequently wrong. Optimizing an unimportant piece of code will typically do little to help the overall performance.

Program optimization When the bottleneck is localized, optimization usually starts with a rethinking of the algorithm used in the program. More often than not, a particular algorithm can be specifically tailored to a particular problem, yielding better performance than a generic algorithm. For example, the task of sorting a huge list of items is usually done with a quicksort routine, which is one of the most efficient generic algorithms. But if some characteristic of the items is exploitable (for example, they are already arranged in some particular order), a different method can be used, or even a custom-made sort routine. After the programmer is reasonably sure that the best algorithm is selected, code optimization can start. Loops can be unrolled (for lower loop overhead, although this can often lead to lower speed if it overloads the CPU cache), data types as small as possible can be used, integer arithmetic can be used instead of floating-point, and so on. (See algorithmic efficiency article for these and other techniques.) Performance bottlenecks can be due to language limitations rather than algorithms or data structures used in the program. Sometimes, a critical part of the program can be re-written in a different programming language that gives more direct access to the underlying machine. For example, it is common for very high-level languages like Python to have modules written in C for greater speed. Programs already written in C can have modules written in assembly. Programs written in D can use the inline assembler. Rewriting sections "pays off" in these circumstances because of a general "rule of thumb" known as the 90/10 law, which states that 90% of the time is spent in 10% of the code, and only 10% of the time in the remaining 90% of the code. So, putting intellectual effort into optimizing just a small part of the program can have a huge effect on the overall speed if the correct part(s) can be located. Manual optimization sometimes has the side effect of undermining readability. Thus code optimizations should be carefully documented (preferably using in-line comments), and their effect on future development evaluated. The program that performs an automated optimization is called an optimizer. Most optimizers are embedded in compilers and operate during compilation. Optimizers can often tailor the generated code to specific processors. Today, automated optimizations are almost exclusively limited to compiler optimization. However, because compiler optimizations are usually limited to a fixed set of rather general optimizations, there is considerable demand for optimizers which can accept descriptions of problem and language-specific optimizations, allowing an engineer to specify custom optimizations. Tools that accept descriptions of optimizations are called program transformation systems and are beginning to be applied to real software systems such as C++. Some high-level languages (Eiffel, Esterel) optimize their programs by using an intermediate language. Grid computing or distributed computing aims to optimize the whole system, by moving tasks from computers with high usage to computers with idle time.

64

Time taken for optimization


Sometimes, the time taken to undertake optimization in itself may be an issue. Optimizing existing code usually does not add new features, and worse, it might add new bugs in previously working code (as any change might). Because manually optimized code might sometimes have less "readability" than unoptimized code, optimization might impact maintainability of it as well. Optimization comes at a price and it is important to be sure that the investment is worthwhile. An automatic optimizer (or optimizing compiler, a program that performs code optimization) may itself have to be optimized, either to further improve the efficiency of its target programs or else speed up its own operation. A compilation performed with optimization "turned on" usually takes longer, although this is usually only a problem when programs are quite large. In particular, for just-in-time compilers the performance of the run time compile component, executing together with its target code, is the key to improving overall execution speed.

Program optimization

65

Quotes
"The order in which the operations shall be performed in every particular case is a very interesting and curious question, on which our space does not permit us fully to enter. In almost every computation a great variety of arrangements for the succession of the processes is possible, and various considerations must influence the selection amongst them for the purposes of a Calculating Engine. One essential object is to choose that arrangement which shall tend to reduce to a minimum the time necessary for completing the calculation." Ada Byron's notes on the analytical engine 1842. "More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason including blind stupidity." W.A. Wulf "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified"[5] Donald Knuth "Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you have proven that's where the bottleneck is." Rob Pike "The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." Michael A. Jackson

References
Jon Bentley: Writing Efficient Programs, ISBN 0-13-970251-2. Donald Knuth: The Art of Computer Programming
[1] Robert Sedgewick, Algorithms, 1984, p. 84 [2] Knuth, Donald (December 1974). "Structured Programming with go to Statements". ACM Journal Computing Surveys 6 (4): 268. CiteSeerX: 10.1.1.103.6084 (http:/ / citeseerx. ist. psu. edu/ viewdoc/ summary?doi=10. 1. 1. 103. 6084). [3] The Errors of Tex, in SoftwarePractice & Experience, Volume 19, Issue 7 (July 1989), pp. 607685, reprinted in his book Literate Programming (p. 276) [4] Tony Hoare, a 2004 email (http:/ / hans. gerwitz. com/ 2004/ 08/ 12/ premature-optimization-is-the-root-of-all-evil. html) [5] Knuth, Donald: Structured Programming with Goto Statements (http:/ / pplab. snu. ac. kr/ courses/ adv_pl05/ papers/ p261-knuth. pdf). Computing Surveys 6:4 (1974), 261301.

External links
Optimizing software in C++ (http://www.agner.org/optimize/optimizing_cpp.pdf) C optimization tutorial (http://web.archive.org/web/20080729033434/http://www.abarnett.demon.co.uk/ tutorial.html) Memory optimization (http://www.research.scea.com/research/pdfs/ GDC2003_Memory_Optimization_18Mar03.pdf) by Christer Ericson Optimization manuals for the x86 and x86-64 family microprocessors (http://www.agner.org/optimize/) How To Write Fast Numerical Code: A Small Introduction (http://www.ece.cmu.edu/~franzf/papers/gttse07. pdf) Software Optimization at Link-time And Run-time (http://www.cs.arizona.edu/solar/) Article " A Plea for Lean Software (http://doi.ieeecomputersociety.org/10.1109/2.348001)" by Niklaus Wirth Description from the Portland Pattern Repository (http://c2.com/cgi/wiki?CategoryOptimization) Performance tuning of Computer Networks (http://www.daemon.be/maarten/ipperf.html) An article describing high-level optimization (http://www.thinkingparallel.com/2006/08/07/ my-views-on-high-level-optimization/) "What Every Programmer Should Know About Memory" (http://people.redhat.com/drepper/cpumemory.pdf) by Ulrich Drepper explains the structure of modern memory subsystems and suggests how to utilize them

Program optimization efficiently "Linux Multicore Performance Analysis and Optimization in a Nutshell" (http://icl.cs.utk.edu/~mucci/latest/ pubs/Notur2009-new.pdf), presentation slides by Philip Mucci.

66

Optimizing compiler
In computing and computer science, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied. The growth of portable computers has created a market for minimizing the power consumed by a program. Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources. It has been shown that some code optimization problems are NP-complete, or even undecidable. In practice, factors such as the programmer's willingness to wait for the compiler to complete its task place upper limits on the optimizations that a compiler implementor might provide. (Optimization is generally a very CPU- and memory-intensive process.) In the past, computer memory limitations were also a major factor in limiting which optimizations could be performed. Because of all these factors, optimization rarely produces "optimal" output in any sense, and in fact an "optimization" may impede performance in some cases; rather, they are heuristic methods for improving resource usage in typical programs.

Types of optimizations
Techniques used in optimization can be broken up among various scopes which can affect anything from a single statement to the entire program. Generally speaking, locally scoped techniques are easier to implement than global ones but result in smaller gains. Some examples of scopes include: Peephole optimizations Usually performed late in the compilation process after machine code has been generated. This form of optimization examines a few adjacent instructions (like "looking through a peephole" at the code) to see whether they can be replaced by a single instruction or a shorter sequence of instructions. For instance, a multiplication of a value by2 might be more efficiently executed by left-shifting the value or by adding the value to itself. (This example is also an instance of strength reduction.) Local optimizations These only consider information local to a basic block.[1] Since basic blocks have no control flow, these optimizations need very little analysis (saving time and reducing storage requirements), but this also means that no information is preserved across jumps. Global optimizations These are also called "intraprocedural methods" and act on whole functions.[2] This gives them more information to work with but often makes expensive computations necessary. Worst case assumptions have to be made when function calls occur or global variables are accessed (because little information about them is available). Loop optimizations These act on the statements which make up a loop, such as a for loop (e.g., loop-invariant code motion). Loop optimizations can have a significant impact because many programs spend a large percentage of their time inside loops. Interprocedural, whole-program or link-time optimization

Optimizing compiler These analyze all of a program's source code. The greater quantity of information extracted means that optimizations can be more effective compared to when they only have access to local information (i.e., within a single function). This kind of optimization can also allow new techniques to be performed. For instance function inlining, where a call to a function is replaced by a copy of the function body. Machine code optimization These analyze the executable task image of the program after all of a executable machine code has been linked. Some of the techniques that can be applied in a more limited scope, such as macro compression (which saves space by collapsing common sequences of instructions), are more effective when the entire executable task image is available for analysis.[3] In addition to scoped optimizations there are two further general categories of optimization: Programming languageindependent vs language-dependent Most high-level languages share common programming constructs and abstractions: decision (if, switch, case), looping (for, while, repeat.. until, do.. while), and encapsulation (structures, objects). Thus similar optimization techniques can be used across languages. However, certain language features make some kinds of optimizations difficult. For instance, the existence of pointers in C and C++ makes it difficult to optimize array accesses (see alias analysis). However, languages such as PL/1 (that also supports pointers) nevertheless have available sophisticated optimizing compilers to achieve better performance in various other ways. Conversely, some language features make certain optimizations easier. For example, in some languages functions are not permitted to have side effects. Therefore, if a program makes several calls to the same function with the same arguments, the compiler can immediately infer that the function's result need be computed only once. In languages where functions are allowed to have side effects, another strategy is possible. The optimizer can determine which function has no side effects, and restrict such optimizations to side effect free functions. This optimization is only possible when the optimizer has access to the called function. Machine independent vs machine dependent Many optimizations that operate on abstract programming concepts (loops, objects, structures) are independent of the machine targeted by the compiler, but many of the most effective optimizations are those that best exploit special features of the target platform. E.g.: Instructions which do several things at once, such as decrement register and branch if not zero. The following is an instance of a local machine dependent optimization. To set a register to 0, the obvious way is to use the constant '0' in an instruction that sets a register value to a constant. A less obvious way is to XOR a register with itself. It is up to the compiler to know which instruction variant to use. On many RISC machines, both instructions would be equally appropriate, since they would both be the same length and take the same time. On many other microprocessors such as the Intel x86 family, it turns out that the XOR variant is shorter and probably faster, as there will be no need to decode an immediate operand, nor use the internal "immediate operand register". (A potential problem with this is that XOR may introduce a data dependency on the previous value of the register, causing a pipeline stall. However, processors often have XOR of a register with itself as a special case that doesn't cause stalls.)

67

Factors affecting optimization


The machine itself Many of the choices about which optimizations can and should be done depend on the characteristics of the target machine. It is sometimes possible to parameterize some of these machine dependent factors, so that a single piece of compiler code can be used to optimize different machines just by altering the machine description parameters. GCC is a compiler which exemplifies this approach. The architecture of the target CPU

Optimizing compiler Number of CPU registers: To a certain extent, the more registers, the easier it is to optimize for performance. Local variables can be allocated in the registers and not on the stack. Temporary/intermediate results can be left in registers without writing to and reading back from memory. RISC vs CISC: CISC instruction sets often have variable instruction lengths, often have a larger number of possible instructions that can be used, and each instruction could take differing amounts of time. RISC instruction sets attempt to limit the variability in each of these: instruction sets are usually constant length, with few exceptions, there are usually fewer combinations of registers and memory operations, and the instruction issue rate (the number of instructions completed per time period, usually an integer multiple of the clock cycle) is usually constant in cases where memory latency is not a factor. There may be several ways of carrying out a certain task, with CISC usually offering more alternatives than RISC. Compilers have to know the relative costs among the various instructions and choose the best instruction sequence (see instruction selection). Pipelines: A pipeline is essentially a CPU broken up into an assembly line. It allows use of parts of the CPU for different instructions by breaking up the execution of instructions into various stages: instruction decode, address decode, memory fetch, register fetch, compute, register store, etc. One instruction could be in the register store stage, while another could be in the register fetch stage. Pipeline conflicts occur when an instruction in one stage of the pipeline depends on the result of another instruction ahead of it in the pipeline but not yet completed. Pipeline conflicts can lead to pipeline stalls: where the CPU wastes cycles waiting for a conflict to resolve. Compilers can schedule, or reorder, instructions so that pipeline stalls occur less frequently. Number of functional units: Some CPUs have several ALUs and FPUs. This allows them to execute multiple instructions simultaneously. There may be restrictions on which instructions can pair with which other instructions ("pairing" is the simultaneous execution of two or more instructions), and which functional unit can execute which instruction. They also have issues similar to pipeline conflicts. Here again, instructions have to be scheduled so that the various functional units are fully fed with instructions to execute. The architecture of the machine Cache size (256kiB12MiB) and type (direct mapped, 2-/4-/8-/16-way associative, fully associative): Techniques such as inline expansion and loop unrolling may increase the size of the generated code and reduce code locality. The program may slow down drastically if a highly utilized section of code (like inner loops in various algorithms) suddenly cannot fit in the cache. Also, caches which are not fully associative have higher chances of cache collisions even in an unfilled cache. Cache/Memory transfer rates: These give the compiler an indication of the penalty for cache misses. This is used mainly in specialized applications. Intended use of the generated code Debugging While writing an application, a programmer will recompile and test often, and so compilation must be fast. This is one reason most optimizations are deliberately avoided during the test/debugging phase. Also, program code is usually "stepped through" (see Program animation) using a symbolic debugger, and optimizing transformations, particularly those that reorder code, can make it difficult to relate the output code with the line numbers in the original source code. This can confuse both the debugging tools and the programmers using them. General purpose use Prepackaged software is very often expected to be executed on a variety of machines and CPUs that may share the same instruction set, but have different timing, cache or memory characteristics. So, the code may not be tuned to any particular CPU, or may be tuned to work best on the most popular CPU and yet still work acceptably well on other CPUs.

68

Optimizing compiler Special-purpose use If the software is compiled to be used on one or a few very similar machines, with known characteristics, then the compiler can heavily tune the generated code to those specific machines (if such options are available). Important special cases include code designed for parallel and vector processors, for which special parallelizing compilers are employed. Embedded systems These are a common case of special-purpose use. Embedded software can be tightly tuned to an exact CPU and memory size. Also, system cost or reliability may be more important than the code's speed. So, for example, compilers for embedded software usually offer options that reduce code size at the expense of speed, because memory is the main cost of an embedded computer. The code's timing may need to be predictable, rather than as fast as possible, so code caching might be disabled, along with compiler optimizations that require it.

69

Common themes
To a large extent, compiler optimization techniques have the following themes, which sometimes conflict. Optimize the common case The common case may have unique properties that allow a fast path at the expense of a slow path. If the fast path is taken most often, the result is better overall performance. Avoid redundancy Reuse results that are already computed and store them for later use, instead of recomputing them. Less code Remove unnecessary computations and intermediate values. Less work for the CPU, cache, and memory usually results in faster execution. Alternatively, in embedded systems, less code brings a lower product cost. Fewer jumps by using straight line code, also called branch-free code Less complicated code. Jumps (conditional or unconditional branches) interfere with the prefetching of instructions, thus slowing down code. Using inlining or loop unrolling can reduce branching, at the cost of increasing binary file size by the length of the repeated code. This tends to merge several basic blocks into one. Locality Code and data that are accessed closely together in time should be placed close together in memory to increase spatial locality of reference. Exploit the memory hierarchy Accesses to memory are increasingly more expensive for each level of the memory hierarchy, so place the most commonly used items in registers first, then caches, then main memory, before going to disk. Parallelize Reorder operations to allow multiple computations to happen in parallel, either at the instruction, memory, or thread level. More precise information is better The more precise the information the compiler has, the better it can employ any or all of these optimization techniques. Runtime metrics can help Information gathered during a test run can be used in profile-guided optimization. Information gathered at runtime (ideally with minimal overhead) can be used by a JIT compiler to dynamically improve optimization.

Optimizing compiler Strength reduction Replace complex or difficult or expensive operations with simpler ones. For example, replacing division by a constant with multiplication by its reciprocal, or using induction variable analysis to replace multiplication by a loop index with addition.

70

Specific techniques
Loop optimizations
Some optimization techniques primarily designed to operate on loops include: Induction variable analysis Roughly, if a variable in a loop is a simple linear function of the index variable, such as j := 4*i + 1, it can be updated appropriately each time the loop variable is changed. This is a strength reduction, and also may allow the index variable's definitions to become dead code. This information is also useful for bounds-checking elimination and dependence analysis, among other things. Loop fission or loop distribution Loop fission attempts to break a loop into multiple loops over the same index range but each taking only a part of the loop's body. This can improve locality of reference, both of the data being accessed in the loop and the code in the loop's body. Loop fusion or loop combining or loop ramming or loop jamming Another technique which attempts to reduce loop overhead. When two adjacent loops would iterate the same number of times (whether or not that number is known at compile time), their bodies can be combined as long as they make no reference to each other's data. Loop inversion This technique changes a standard while loop into a do/while (also known as repeat/until) loop wrapped in an if conditional, reducing the number of jumps by two, for cases when the loop is executed. Doing so duplicates the condition check (increasing the size of the code) but is more efficient because jumps usually cause a pipeline stall. Additionally, if the initial condition is known at compile-time and is known to be side-effect-free, the if guard can be skipped. Loop interchange These optimizations exchange inner loops with outer loops. When the loop variables index into an array, such a transformation can improve locality of reference, depending on the array's layout. Loop-invariant code motion If a quantity is computed inside a loop during every iteration, and its value is the same for each iteration, it can vastly improve efficiency to hoist it outside the loop and compute its value just once before the loop begins. This is particularly important with the address-calculation expressions generated by loops over arrays. For correct implementation, this technique must be used with loop inversion, because not all code is safe to be hoisted outside the loop. Loop nest optimization Some pervasive algorithms such as matrix multiplication have very poor cache behavior and excessive memory accesses. Loop nest optimization increases the number of cache hits by performing the operation over small blocks and by using a loop interchange. Loop reversal

Optimizing compiler Loop reversal reverses the order in which values are assigned to the index variable. This is a subtle optimization which can help eliminate dependencies and thus enable other optimizations. Loop unrolling Unrolling duplicates the body of the loop multiple times, in order to decrease the number of times the loop condition is tested and the number of jumps, which hurt performance by impairing the instruction pipeline. A "fewer jumps" optimization. Completely unrolling a loop eliminates all overhead, but requires that the number of iterations be known at compile time. Loop splitting Loop splitting attempts to simplify a loop or eliminate dependencies by breaking it into multiple loops which have the same bodies but iterate over different contiguous portions of the index range. A useful special case is loop peeling, which can simplify a loop with a problematic first iteration by performing that iteration separately before entering the loop. Loop unswitching Unswitching moves a conditional from inside a loop to outside the loop by duplicating the loop's body inside each of the if and else clauses of the conditional. Software pipelining The loop is restructured in such a way that work done in an iteration is split into several parts and done over several iterations. In a tight loop this technique hides the latency between loading and using values. Automatic parallelization A loop is converted into multi-threaded or vectorized (or even both) code in order to utilize multiple processors simultaneously in a shared-memory multiprocessor (SMP) machine, including multi-core machines.

71

Data-flow optimizations
Data-flow optimizations, based on data-flow analysis, primarily depend on how certain properties of data are propagated by control edges in the control flow graph. Some of these include: Common subexpression elimination In the expression (a + b) - (a + b)/4, "common subexpression" refers to the duplicated (a + b). Compilers implementing this technique realize that (a + b) won't change, and as such, only calculate its value once. Constant folding and propagation replacing expressions consisting of constants (e.g., 3 + 5) with their final value (8) at compile time, rather than doing the calculation in run-time. Used in most modern languages. Induction variable recognition and elimination see discussion above about induction variable analysis. Alias classification and pointer analysis in the presence of pointers, it is difficult to make any optimizations at all, since potentially any variable can have been changed when a memory location is assigned to. By specifying which pointers can alias which variables, unrelated pointers can be ignored. Dead store elimination removal of assignments to variables that are not subsequently read, either because the lifetime of the variable ends or because of a subsequent assignment that will overwrite the first value.

Optimizing compiler

72

SSA-based optimizations
These optimizations are intended to be done after transforming the program into a special form called static single assignment (see SSA form), in which every variable is assigned in only one place. Although some function without SSA, they are most effective with SSA. Many optimizations listed in other sections also benefit with no special changes, such as register allocation. Global value numbering GVN eliminates redundancy by constructing a value graph of the program, and then determining which values are computed by equivalent expressions. GVN is able to identify some redundancy that common subexpression elimination cannot, and vice versa. Sparse conditional constant propagation Effectively equivalent to iteratively performing constant propagation, constant folding, and dead code elimination until there is no change, but is much more efficient. This optimization symbolically executes the program, simultaneously propagating constant values and eliminating portions of the control flow graph that this makes unreachable.

Code generator optimizations


Register allocation The most frequently used variables should be kept in processor registers for fastest access. To find which variables to put in registers an interference-graph is created. Each variable is a vertex and when two variables are used at the same time (have an intersecting liverange) they have an edge between them. This graph is colored using for example Chaitin's algorithm using the same number of colors as there are registers. If the coloring fails one variable is "spilled" to memory and the coloring is retried. Instruction selection Most architectures, particularly CISC architectures and those with many addressing modes, offer several different ways of performing a particular operation, using entirely different sequences of instructions. The job of the instruction selector is to do a good job overall of choosing which instructions to implement which operators in the low-level intermediate representation with. For example, on many processors in the 68000 family and on the x86 architecture, complex addressing modes can be used in statements like "lea 25(a1,d5*4), a0", allowing a single instruction to perform a significant amount of arithmetic with less storage. Instruction scheduling Instruction scheduling is an important optimization for modern pipelined processors, which avoids stalls or bubbles in the pipeline by clustering instructions with no dependencies together, while being careful to preserve the original semantics. Rematerialization Rematerialization recalculates a value instead of loading it from memory, preventing a memory access. This is performed in tandem with register allocation to avoid spills. Code factoring If several sequences of code are identical, or can be parameterized or reordered to be identical, they can be replaced with calls to a shared subroutine. This can often share code for subroutine set-up and sometimes tail-recursion.[4] Trampolines Many CPUs have smaller subroutine call instructions to access low memory. A compiler can save space by using these small calls in the main body of code. Jump instructions in low memory can access the routines at any address. This multiplies space savings from code factoring.[4]

Optimizing compiler Reordering computations Based on integer linear programming, restructuring compilers enhance data locality and expose more parallelism by reordering computations. Space-optimizing compilers may reorder code to lengthen sequences that can be factored into subroutines.

73

Functional language optimizations


Although many of these also apply to non-functional languages, they either originate in, are most easily implemented in, or are particularly critical in functional languages such as Lisp and ML. Removing recursion Recursion is often expensive, as a function call consumes stack space and involves some overhead related to parameter passing and flushing the instruction cache. Tail recursive algorithms can be converted to iteration, which does not have call overhead and uses a constant amount of stack space, through a process called tail recursion elimination or tail call optimization. Some functional languages (e.g., Scheme and Erlang) mandate that tail calls be optimized by a conforming implementation, due to their prevalence in these languages. Deforestation (data structure fusion) Because of the high level nature by which data structures are specified in functional languages such as Haskell, it is possible to combine several recursive functions which produce and consume some temporary data structure so that the data is passed directly without wasting time constructing the data structure.

Other optimizations
Please help separate and categorize these further and create detailed pages for them, especially the more complex ones, or link to one where one exists. Bounds-checking elimination Many languages, for example Java, enforce bounds-checking of all array accesses. This is a severe performance bottleneck on certain applications such as scientific code. Bounds-checking elimination allows the compiler to safely remove bounds-checking in many situations where it can determine that the index must fall within valid bounds, for example if it is a simple loop variable. Branch offset optimization (machine independent) Choose the shortest branch displacement that reaches target Code-block reordering Code-block reordering alters the order of the basic blocks in a program in order to reduce conditional branches and improve locality of reference. Dead code elimination Removes instructions that will not affect the behaviour of the program, for example definitions which have no uses, called dead code. This reduces code size and eliminates unnecessary computation. Factoring out of invariants If an expression is carried out both when a condition is met and is not met, it can be written just once outside of the conditional statement. Similarly, if certain types of expressions (e.g., the assignment of a constant into a variable) appear inside a loop, they can be moved out of it because their effect will be the same no matter if they're executed many times or just once. Also known as total redundancy elimination. A more powerful optimization is partial redundancy elimination (PRE). Inline expansion or macro expansion

Optimizing compiler When some code invokes a procedure, it is possible to directly insert the body of the procedure inside the calling code rather than transferring control to it. This saves the overhead related to procedure calls, as well as providing great opportunity for many different parameter-specific optimizations, but comes at the cost of space; the procedure body is duplicated each time the procedure is called inline. Generally, inlining is useful in performance-critical code that makes a large number of calls to small procedures. A "fewer jumps" optimization. The statements of imperative programming languages are also an example of such an optimization. Although statements could be implemented with function calls they are almost always implemented with code inlining. Jump threading In this pass, consecutive conditional jumps predicated entirely or partially on the same condition are merged. E.g., if (c) { foo; } if (c) { bar; } to if (c) { foo; bar; }, and if (c) { foo; } if (!c) { bar; } to if (c) { foo; } else { bar; }. Macro Compression A space optimization that recognizes common sequences of code, creates subprograms ("code macros") that contain the common code, and replaces the occurrences of the common code sequences with calls to the corresponding subprograms.[3] This is most effectively done as a machine code optimization, when all the code is present. The technique was first used to conserve space in an interpretive byte stream used in an implementation of Macro Spitbol on microcomputers.[5] The problem of determining an optimal set of macros that minimizes the space required by a given code segment is known to be NP-complete,[3] but efficient heuristics attain near-optimal results.[6] Reduction of cache collisions (e.g., by disrupting alignment within a page) Stack height reduction Rearrange expression tree to minimize resources needed for expression evaluation. Test reordering If we have two tests that are the condition for something, we can first deal with the simpler tests (e.g. comparing a variable to something) and only then with the complex tests (e.g., those that require a function call). This technique complements lazy evaluation, but can be used only when the tests are not dependent on one another. Short-circuiting semantics can make this difficult.

74

Interprocedural optimizations
Interprocedural optimization works on the entire program, across procedure and file boundaries. It works tightly with intraprocedural counterparts, carried out with the cooperation of a local part and global part. Typical interprocedural optimizations are: procedure inlining, interprocedural dead code elimination, interprocedural constant propagation, and procedure reordering. As usual, the compiler needs to perform interprocedural analysis before its actual optimizations. Interprocedural analyses include alias analysis, array access analysis, and the construction of a call graph. Interprocedural optimization is common in modern commercial compilers from SGI, Intel, Microsoft, and Sun Microsystems. For a long time the open source GCC was criticized for a lack of powerful interprocedural analysis and optimizations, though this is now improving. Another good open source compiler with full analysis and optimization infrastructure is Open64, which is used by many organizations for research and for commercial purposes. Due to the extra time and space required by interprocedural analysis, most compilers do not perform it by default. Users must use compiler options explicitly to tell the compiler to enable interprocedural analysis and other expensive optimizations.

Optimizing compiler

75

Problems with optimization


Early in the history of compilers, compiler optimizations were not as good as hand-written ones. As compiler technologies have improved, good compilers can often generate better code than human programmers, and good post pass optimizers can improve highly hand-optimized code even further. For RISC CPU architectures, and even more so for VLIW hardware, compiler optimization is the key for obtaining efficient code, because RISC instruction sets are so compact that it is hard for a human to manually schedule or combine small instructions to get efficient results. Indeed, these architectures were designed to rely on compiler writers for adequate performance. However, optimizing compilers are by no means perfect. There is no way that a compiler can guarantee that, for all program source code, the fastest (or smallest) possible equivalent compiled program is output; such a compiler is fundamentally impossible because it would solve the halting problem. This may be proven by considering a call to a function, foo(). This function returns nothing and does not have side effects (no I/O, does not modify global variables and "live" data structures, etc.). The fastest possible equivalent program would be simply to eliminate the function call. However, if the function foo() in fact does not return, then the program with the call to foo() would be different from the program without the call; the optimizing compiler will then have to determine this by solving the halting problem. Additionally, there are a number of other more practical issues with optimizing compiler technology: Optimizing compilers focus on relatively shallow constant-factor performance improvements and do not typically improve the algorithmic complexity of a solution. For example, a compiler will not change an implementation of bubble sort to use mergesort instead. Compilers usually have to support a variety of conflicting objectives, such as cost of implementation, compilation speed and quality of generated code. A compiler typically only deals with a part of a program at a time, often the code contained within a single file or module; the result is that it is unable to consider contextual information that can only be obtained by processing the other files. The overhead of compiler optimization: Any extra work takes time; whole-program optimization is time consuming for large programs. The often complex interaction between optimization phases makes it difficult to find an optimal sequence in which to execute the different optimization phases. Work to improve optimization technology continues. One approach is the use of so-called post-pass optimizers (some commercial versions of which date back to mainframe software of the late 1970s[7]). These tools take the executable output by an "optimizing" compiler and optimize it even further. Post pass optimizers usually work on the assembly language or machine code level (contrast with compilers that optimize intermediate representations of programs). The performance of post pass compilers are limited by the fact that much of the information available in the original source code is not always available to them. As processor performance continues to improve at a rapid pace, while memory bandwidth improves more slowly, optimizations that reduce memory bandwidth requirements (even at the cost of making the processor execute relatively more instructions) will become more useful. Examples of this, already mentioned above, include loop nest optimization and rematerialization.

Optimizing compiler

76

List of static code analyses


Alias analysis Pointer analysis Shape analysis Escape analysis Array access analysis Dependence analysis Control flow analysis Data flow analysis Use-define chain analysis Live variable analysis Available expression analysis

References
[1] Cooper, Keith D., and Torczon, Linda, Engineering a Compiler, Morgan Kaufmann, 2004, ISBN 1-55860-699-8 page 404 [2] Cooper, Keith D., and Torczon, Linda, Engineering a Compiler, Morgan Kaufmann, 2004, ISBN 1-55860-699-8 page 407 [3] Clinton F. Goss (June 1986). Machine Code Optimization - Improving Executable Object Code (http:/ / www. ClintGoss. com/ mco/ ) (Ph.D.). New York University. . Retrieved 24-Mar-2011. [4] Cx51 Compiler Manual, version 09.2001, p155, Keil Software Inc. [5] Robert B. K. Dewar; Martin Charles Golumbic; Clinton F. Goss (October 1979). MICRO SPITBOL. Computer Science Department Technical Report. No. 11. Courant Institute of Mathematical Sciences. [6] Martin Charles Golumbic; Robert B. K. Dewar; Clinton F. Goss (1980). "Macro Substitutions in MICRO SPITBOL - a Combinatorial Analysis". Proc. 11th Southeastern Conference on Combinatorics, Graph Theory and Computing, Congressus Numerantium, Utilitas Math., Winnipeg, Canada 29: 485495. [7] http:/ / portal. acm. org/ citation. cfm?id=358728. 358732

External links
Citations from CiteSeer (http://citeseer.ist.psu.edu/Programming/CompilerOptimization/) Optimization manuals (http://www.agner.org/optimize/#manuals) by Agner Fog - documentation about x86 processor architecture and low-level code optimization

Compiler-compiler

77

Compiler-compiler
In computer science, a compiler-compiler or compiler generator is a tool that creates a parser, interpreter, or compiler from some form of formal description of a language and machine. The earliest and still most common form of compiler-compiler is a parser generator, whose input is a grammar (usually in BNF) of a programming language, and whose generated output is the source code of a parser often used as a component of a compiler. Similarly, code generator-generators (such as JBurg [1]) exist, but such tools have not yet reached maturity. The ideal compiler-compiler takes a description of a programming language and a target instruction set architecture, and automatically generates a usable compiler from them. In practice, the state of the art has yet to reach this degree of sophistication and most compiler generators are not capable of handling semantic or target architecture information.

Variants
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 grammars and code generation rules, 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.[2] 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 [1]) 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).

History
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. However it was rather different from modern compiler-compilers, and today would probably be described as being somewhere between a highly customisable generic compiler and an extensible-syntax language. The name 'compiler-compiler' was far more appropriate for Brooker's system than it is for most modern compiler-compilers, which are more accurately described as parser generators. It is almost certain that the "Compiler Compiler" name has entered common use due to Yacc rather than Brooker's work being remembered. Other examples of parser generators in the yacc vein are ANTLR, Coco/R, CUP, GNU bison, Eli, FSL, SableCC and JavaCC.

Compiler-compiler

78

Several compiler-compilers
ANTLR Bison Coco/R DMS Software Reengineering Toolkit, a program transformation system with parser generators ELI [3], an integrated toolset for compiler construction. Lemon parboiled, a Java library for building parsers. Packrat parser PQCC, a compiler-compiler that is more than a parser generator. SYNTAX [4], an integrated toolset for compiler construction VisualLangLab, a visual parser generator for JVM languages. Yacc

Other related topics


Parsing expression grammar LL parser LR parser Simple LR parser LALR parser GLR parser Metacompiler

Notes
[1] http:/ / jburg. sourceforge. net/ [2] Peter Mosses, "SIS: A Compiler-Generator System Using Denotational Semantics," Report 78-4-3, Dept. of Computer Science, University of Aarhus, Denmark, June 1978 [3] http:/ / eli-project. sourceforge. net/ [4] https:/ / gforge. inria. fr/ projects/ syntax

References
This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.

Further reading
Historical
Brooker, R. A., et al., The Compiler-Compiler, Annual Review in Automatic Programming, Vol. 3, p. 229. (1963). Brooker, R. A., Morris, D. and Rohl, J. S., Experience with the Compiler Compiler (http://comjnl. oxfordjournals.org/cgi/content/abstract/9/4/345), Computer Journal, Vol. 9, p. 350. (February 1967). Johnson, Stephen C., Yaccyet another compiler-compiler, Computer Science Technical Report 32, Bell Laboratories, Murray Hill, NJ, July 1975 McKeeman, William Marshall; Horning, James J.; Wortman, David B., CS.toronto.edu (http://www.cs.toronto. edu/XPL/), A Compiler Generator, Englewood Cliffs, N.J.: Prentice-Hall, 1970. ISBN 0-13-155077-2

Compiler-compiler

79

External links
Computer50.org (http://www.computer50.org/mark1/gethomas/manchester_autocodes.html), Brooker Autocodes Catalog.compilertools.net (http://catalog.compilertools.net/), The Catalog of Compiler Construction Tools Labraj.uni-mb.si (http://labraj.uni-mb.si/lisa), Lisa Skenz.it (http://www.skenz.it/traduttori/), Jflex and Cup resources (Italian) Gentle.compilertools.net (http://gentle.compilertools.net/index.html), The Gentile Compiler Construction System Accent.compilertools.net (http://accent.compilertools.net/), Accent: a Compiler for the Entire Class of Context-Free Languages Grammatica.percederberg.net (http://grammatica.percederberg.net), an open-source parser-generator for .NET and Java

Article Sources and Contributors

80

Article Sources and Contributors


Compiler Source: http://en.wikipedia.org/w/index.php?oldid=513737612 Contributors: 169, 16@r, 1exec1, 213.20.128.xxx, 213.253.39.xxx, A Quest For Knowledge, APH, Abdull, Adrignola, Akihabara, Alfa.walsh, Ali@gwc.org.uk, AlyM, Ancheta Wis, Andre Engels, Andreas Kaufmann, Andrij Kursetsky, Annish33, Ans-mo, Arbitrarily0, Atanamir, Atlan, Avhohlov, Avono, Axem Titanium, BACbKA, BEDEWIN, BMF81, Babbage, Badsaha, BenLTitzer, Bevo, BlueNovember, Bluetulip, Bobblewik, Bongwarrior, Bookandcoffee, Brat32, Brest, Buraq, CYD, Cacycle, CanOfWorms, CanisRufus, Captain Wingo, CatherineMunro, Charles Merriam, Chocolateboy, Christian75, Christopher Thomas, Climber22, Closedmouth, Conversion script, CooYip, Corti, Culix, Cwolfsheep, D6, DVdm, Daio, Damdemi, DanielPharos, Daveatwork11, Daverocks, David-Sarah Hopwood, DavidCary, Dcoetzee, Dcstimm, Deanos, Deepak D'Souza, Derek Ross, Derek farn, Dfletter, Diego Moya, Dina, Dmcq, DmitTrix, Doradus, Doramjan, Dorchard, Dreftymac, Drhex, Dwo, Dysprosia, ESkog, Edcolins, Edwindreillyjr, EmielMols, Emperorbma, Enchanter, Encryptio, Epbr123, Espertus, Excirial, Expertjohn, Fabartus, Faradayplank, Fishnet37222, FlashSheridan, Flewis, Fred Bauder, FredT67, Fubar Obfusco, Furrykef, G0gogcsc300, General Disarray (usurped), Giftlite, Glane23, Glaucus, Glenn, GliderMaven, Graibeard, Gregsinclair, Grottenolm42, Grunt, Gurch, Haeleth, Hans Bauer, Hans Dunkelberg, Hari, Hereticam, History2007, Hmains, Hobbes, Holizz, Holme215, Hooperbloob, Horizons 2009, Hubertus, Iain.mcclatchie, Ida Shaw, Imran, Imroy, Incnis Mrsi, Indeterminate, Indon, Interiot, IntrigueBlue, Ioopooi, J.delanoy, JHandrop, JLaTondre, JaGa, James Foster, Jauhienij, Javalenok, Jay, Jdsalat, Jens Troeger, Jerryobject, Jiddisch, Jkl, Joakim, JonHarder, Jonatan Swift, Jonathan de Boyne Pollard, Jpbowen, Jpk, Jung dalglish, Jwpurple, Jna runn, K.lee, KATANAGOD, KGasso, Kbdank71, Kenny sh, Khaled hosny, Khendon, KingDoorknob, Kissall, Knutux, Kragen, Kurtbeyer, LeaveSleaves, Lee.crabtree, Leibniz, Lemontea, Liao, Ligulem, LittleOldMe, LjL, Llywrch, Looxix, Loren.wilton, LorenzoB, LrdChaos, Lseongjoo, Luna Santin, M4gnum0n, Mac, Makeemlighter, Mariuz, Marudubshinki, Matt73, Matthiaspaul, Maximum bobby, Mbessey, Mdg149, Methree69, Mgreenbe, Michael Dring, Michael Hardy, Michael miceli, Mike Rosoft, Minesweeper, MisterPhyrePhox, Miym, Mkehrt, Mormegil, Moskvax, Motley Crue Rocks, Mrwojo, Msjaiswal, Muijz, Mukkakukaku, Murray Langton, Mxn, NSH001, Nanshu, Napi, Natkeeran, NawlinWiki, Nbudden, Neilc, Neo85, Nex O-Slash, Nharipra, Nich148 9, Norm, OSP Editor, Obradovic Goran, Officiallyover, Parrothead1983, Pattermeister, Paul Mackay, Pdcook, Peterdjones, Pgr94, Phoenix314, Pi is 3.14159, PierreAbbat, Pinethicket, Pjvpjv, Project2501a, Prolog, Quuxplusone, Qwertyus, R Lowry, R. S. Shaw, RTC, Radagast83, RainbowOfLight, Raise exception, Rajb245, Raul654, Raw nerve, Raysonho, Rchrd, Rcorcs, ReadyKnit, Reaverdrop, RedWolf, Reelrt, Reimei, Rene Mas, RexNL, Rfc1394, Rhe br, Rich Farmbrough, Richard W.M. Jones, RichardMills65, Rjwilmsi, Rrelf, Ruud Koot, Rwwww, Ryan032, Sam Korn, SamSim, Scarroll, SchuminWeb, Shareddreams, Shawnhath, Shipmaster, Sietse Snel, Simetrical, Skizzik, Smaffy, Sonett72, Sources said, Spadge47, Spencer, Stainless steel, Stan Shebs, SteinbDJ, Steve2011, StradivariusTV, Sunnylalit, Surachit, Suruena, Szajd, TakuyaMurata, Tedoja, Tenbaset, TestPilot, Tgeairn, Th1rt3en, Thalter, The Firewall, The Mysterious Gamer, TheAMmollusc, TheCoffee, Theme97, ThomasStrohmann, Thumperward, Tide rolls, Tobias Bergemann, Tom.Reding, Tompsci, Tony Sidaway, Trescott2000, VampWillow, Vizzer, WMarsh, WRK, Wavelength, Wbm1058, Wbrito, Wernher, Whitepaw, Wikiklrsc, WimdeValk, Wjaguar, Wolfkeeper, X42bn6, Y0u, Yadu123yadu, Yonkie, Yoric, Yworo, Zzuuzz, , , 434 anonymous edits Lexical analysis Source: http://en.wikipedia.org/w/index.php?oldid=507470614 Contributors: 0majortom0, Acyclopedic, Agoralys, Alkamins, Armenbadal, Atr2006, Bananasoup, BiT, Biasoli, Blazar, Borislav, Borsotti, Brion VIBBER, Bunyk, CameronHarris, CharlotteWebb, Chris 73, Christoph hausner, Cool3, Danakil, Daxx wp, Denispir, Derek farn, Diza, Dzhim, Ebraminio, Edward Z. Yang, EdwardSamson, FatalError, Frap, Fschaef, Glenn, Gwleibniz, Hello71, HenkeB, Hyperyl, Indil, Interiot, Itai, Jeff G., Jeremysolt, Jeroen74, Jfriedl, Jogloran, Johnot, K.lee, Kantorovitz, Karol Langner, Kingpin13, Knightwhosayni, Knudvaneeden, Leycec, Liao, Liberatus, Lotje, MDfoo, MarSch, Marc van Leeuwen, Materialscientist, Matt Crypto, Max Terry, Maxwellterry, Mdd4696, Merphant, Monty845, Mrwojo, Nate1481, Natkeeran, Nneonneo, Ojigiri, Omicronpersei8, OwenX, Paul Foxworthy, Paulbmann, Pcvpcvpcv, Philip Trueman, Pooryorick, Prari, Quuxplusone, RMFan1, Rajakhr, Redgolpe, Rich Farmbrough, Robthebob, Rsrikanth05, Ruud Koot, Saeed Jahed, Salix alba, Sderose, Simoneau, Skyfire, Stan Shebs, Storabled, SwisterTwister, Sym zo, Techauthor, Techtonik, Tedickey, Teles, The Anome, Theodore.norvell, Tinku99, Ulrich.b, Updatebjarni, Vegard, Wafitzge, WojPob, Wulfila, XP1, Ybungalobill, Zr40, 199 anonymous edits Parsing Source: http://en.wikipedia.org/w/index.php?oldid=517854386 Contributors: 16@r, 218, Aakin, Agentilini, Ahoerstemeier, Ahy1, AlanUS, Alansohn, AlmostReadytoFly, Andrei Stroe, AngelHaf, Architectual, Ashowen1701, BD2412, Beland, Benot Sagot, Billposer, Bobo192, Borsotti, BradBeattie, Bubba hotep, Cadillac, Callanecc, CapnPrep, CardinalDan, Catapult, CesarB, Chris the speller, Chrisjohnson, Clarityfiend, Claytoncarney, Cometstyles, DBSand, Damian Yerrick, Dantek, Derek farn, DevinCook, Diego Grez, Dmb000006, Dougbast, Downwards, E-boy, Eekerz, Epolk, Farzaneh, FatalError, Fchaumartin, FedericoMP, FghIJklm, Flyguy649, Furrykef, Gabilaw, Gadfium, Garfieldnate, Gavia immer, Giftlite, Glenn, GreyCat, Gurch, HVS, Hairy Dude, HenkeB, Hermel, Hpvpp, IronGargoyle, J04n, JEBrown87544, Jeana7, Jeroendv, Jimworm, John, Jonsafari, Jstrater, K.lee, Kjmjds, Knuckles, Knudvaneeden, Larry V, Leonxlin, Liao, Libcub, Liberatus, Lotje, LucidFool, MER-C, Mac, Macrakis, Magicjazz, MarSch, MarkSweep, MarsRover, Martin Hampl, Mccready, Michael Hardy, Mikeblas, Mmh, Modify, MrDolomite, Mulukhiyya, Natelewis, Nathanfunk, Neilc, Nekobasu, NickyMcLean, Nicsterr, Nikolasmorton, Nixeagle, Obradovic Goran, OlEnglish, Oliverlyc, Ourai, Paul Foxworthy, Paulbmann, Pekinensis, Pelzi, Pietdesomere, Pps, Pulsar.co.nr, QTJ, Quuxplusone, RekishiEJ, Richard W.M. Jones, Rkmlai, RobertG, Robinlrandall, Romek1, Ruebencampbell, Ruud Koot, Salix alba, Scruffy323, Seans Potato Business, Seaphoto, Sebbes333, Sebesta, Sephiroth BCR, Shoesfullofdust, SilkTork, Sirthias, Slike2, Slomo, Sommers, Spakin, Spayrard, Sylvia.wei, TakuyaMurata, Technopat, The Anome, Thisisnotapipe, Timhowardriley, Tobias Bergemann, Tomjenkins52, Usien6, Valodzka, Van der Hoorn, Vicki Rosenzweig, Vt-aoe, W3stfa11, Wikiklrsc, Xtremejames183, YoCmos, Yrithinnd, Zalgo, Zeman, Zetawoof, ZxxZxxZ, , 219 , anonymous edits Top-down parsing Source: http://en.wikipedia.org/w/index.php?oldid=502241897 Contributors: Adam Zbransk, AngelHaf, Astronautics, Blonkm, Brad101, Crystallina, Dysprosia, Edward, FatalError, Jamelan, MCiura, Msnicki, NotAbel, Obradovic Goran, Paul Foxworthy, Pearle, Pesto, Peter Grey, Qeny, Qsebas, R'n'B, Rjwilmsi, Ruud Koot, Shooshpanchick, Spoon!, Temporarily Insane, Tfl, Tobias Bergemann, ToolmakerSteve, Wavelength, Wuzzeb, 41 anonymous edits Bottom-up parsing Source: http://en.wikipedia.org/w/index.php?oldid=508801689 Contributors: A-giau, Angrytoast, BenC, BradBeattie, CBM, Canwolf, ChrisGualtieri, Cmbay, Cmc0626, Cpl Syx, DBSand, DRogers, DabMachine, Dominus, DyingSphynx, Edward, ElliottRosenthal, FatalError, HopeSeekr of xMule, Jacj, Jamelan, Jmabel, Jos Mara Sola, Macrakis, Mrwojo, Neutrality, Nickj, Obradovic Goran, Owl3638, PeepP, Perey, Peter Grey, Pikantrop, Project2501a, Qeny, Qsebas, RJFJR, Ruud Koot, TakuyaMurata, Tobias Bergemann, Tomdee, ToolmakerSteve, UKoch, WatchAndObserve, 52 anonymous edits Recursive descent parser Source: http://en.wikipedia.org/w/index.php?oldid=515634182 Contributors: Abednigo, Alvestrand, Arto B, BeardWand, Bobo192, Brentsmith101, Brynosaurus, C. A. Russell, Cadr, Crazycomputers, Cybercobra, Dcoetzee, Dominus, Drak, Elendal, Emperorbma, Foolip, Gbwiki2008, HHBones, Hirak 99, Jafet, Jan Hidders, Jerryobject, Jfultz, JonHarder, K3rb, Loadmaster, Luqui, LutzL, Michael Hardy, Mikeblas, Modify, MrOllie, Mrwojo, Msnicki, Nowhere man, PGSONIC, Paul Foxworthy, QTJ, R'n'B, Raise exception, RepeatUntil, Rich Farmbrough, Ruud Koot, Shelbymoore3, Sirthias, TimeOfDei, Tobias Bergemann, Trapmoth, UKoch, VictorAnyakin, Wavelength, WilliamDClinger, 97 anonymous edits LR parser Source: http://en.wikipedia.org/w/index.php?oldid=518414565 Contributors: Akerbos, Albanaco, Albertnetymk, Annem jyothsna, AugPi, AxelBoldt, B6s, Bernard Franois, Bkell, Brouhaha, Bryan Derksen, Brynosaurus, C. A. Russell, CRGreathouse, Cadr, Chip Zero, Chris the speller, CommonsDelinker, Conversion script, DBSand, Dadajan, Damian Yerrick, Damien Cassou, Dead Horsey, Deathanatos, Delirium, Derek farn, Diberri, Djspiewak, Dodek, Doradus, Drj, Edward321, Espadrine, Euyyn, FatalError, Goeie, Hdante, HerrEkberg, Jan Hidders, Jeff3000, JoeKearney, JoeRiel, Jonsafari, Jorend, JulesH, Kellen`, Kenyon, Lilac Soul, Loadmaster, Lorenzarius, Lotje, Macrakis, MarSch, MattGiuca, Mecanismo, Michael miceli, Mikeblas, Moonside, Neilc, Nxavar, Nystrxz, Ootachi, Orderud, Paulbmann, Pfagerburg, Pmanderson, Pmetzger, Poco a poco, Qpratt, Reaper Eternal, Rholton, Rp, Ruud Koot, Sandrarossi, Saxbryn, Sjlver, Smjg, Srikanth40, Steven Weston, StuartBrady, Szaba, TakuyaMurata, Tarbo, Tempest natures, Timwi, Tomhunter, Urod, Vi2, Wavelength, Wguynes, Wik, Wikiklrsc, Wunderdude, YGingras, Yellowdesk, Yurivict, Zyzstar, 158 anonymous edits Syntax-directed translation Source: http://en.wikipedia.org/w/index.php?oldid=517401949 Contributors: BiT, Brookie, Emersoni, Fabrictramp, Fryed-peach, Malcolma, MrRadioGuy, Peripitus, Ruud Koot, Sadads, Starwiz, Tobias Bergemann, Truthanado, Winkels, 9 anonymous edits Context-free grammar Source: http://en.wikipedia.org/w/index.php?oldid=518476655 Contributors: .:Ajvol:., 1&only, 2001:660:330F:CC:226:B9FF:FE77:C243, 62.31.64.xxx, A3 nm, Adrian.benko, AlanUS, Alaudo, Algebran, AnAj, Arialblack, Arnavchaudhary, Arthena, Arvindn, Augur, AxelBoldt, Azndragon1987, BAxelrod, Babajobu, Bedelato, Ben Standeven, Boleslav Bobcik, Brion VIBBER, Brynosaurus, CBKAtTopsails, Cadr, Caesura, Camrn86, Chip Zero, ConradPino, Conversion script, Crasshopper, DARTH SIDIOUS 2, Daniel.Cardenas, DavePeixotto, David Eppstein, David Latapie, Dcoetzee, Delepster, Dominique.devriese, Doradus, Dreftymac, Ebraminio, Ehamberg, Erwinrcat, Eugenwpg, Flamingspinach, Florian D., FrancisTyers, FreplySpang, Future Perfect at Sunrise, Fuzheado, Gelingvistoj, Giftlite, Givicencio, Gogo Dodo, Grafen, Gran, HenkeB, Hephaestos, Hermel, Iltseng, Inimino, J.H, Jafet, Jamesdady, Jan Hidders, Jaredwf, Jayc, Jni, JoanneB, John Fader, JohnOwens, Johnflan, Jokl, Jonsafari, JordanDeLong, Josh Triplett, Julesd, K.lee, Kaihsu, Karl Stroetmann, Kenyon, Khazar, Kishany444, Kmetla, KoenDelaere, Kostmo, Kronoss, Kwamikagami, Kwertii, LC, Lejatorn, LeonardoGregianin, Likebox, Luqui, Mararo, Marcelkcs, Marchash, Mastermind3k, Mcld, Merutak, Miaow Miaow, Minesweeper, Mouhaned99, Mountain, Muu-karhu, N3bulous, Naddy, Nanshu, Nxavar, Obradovic Goran, Oliphaunt, P0lyglut, Paul Foxworthy, Peak, Pete142, Phil Boswell, Phylake, QTJ, Qsebas, Quuxplusone, RaulMiller, Rbrewer42, Ringesherre, Rizome, Rotem Dan, Rp, Ruakh, Ruds, Ruud Koot, Saccade, Sae1962, Saforrest, Saratchandraprasad, Sdornan, Shikhil pict, Sibi antony, Simeon, SixWingedSeraph, Smmurphy, Spencer.matthew, SpikeToronto, Sundar, Susfele, Svick, TakuyaMurata, The Stingray, TheLibrarian, Tim Starling, Timwi, Tjo3ya, TreasuryTag, Tristanreid, Tyler McHenry, UKoch, Universityuser, Urhixidur, VegKilla, Victor Yus, Vsion, Weixifan, William Avery, Wwwwolf, Yoderj, Yoshigev, Zahnradzacken, Zero sharp, ZeroOne, Zeus-chu, Zundark, 299 anonymous edits Run-time system Source: http://en.wikipedia.org/w/index.php?oldid=510585627 Contributors: Abdull, Alansohn, AllenDowney, Asymmetric, Atlant, Bbump, Chris Chittleborough, Cybercobra, DexDor, Giftlite, Grossenhayn, Isnow, JLaTondre, Jdthood, JeromeJerome, Jerryobject, Jorge Stolfi, Koavf, Kubanczyk, Ldo, Methossant, Nbarth, Nickptar, Ninly, Public Menace, Ron Ritzman, Ruud Koot, SimonTrew, Sol Blue, SparsityProblem, Tktktk, Tonank, UnitedStatesian, William Avery, Woohookitty, 13 anonymous edits

Article Sources and Contributors


Code generation (compiler) Source: http://en.wikipedia.org/w/index.php?oldid=517965281 Contributors: Abdull, Ahoerstemeier, Ahy1, AlyM, AndrewHowse, CorpITGuy, Cybercobra, D6, Decltype, Derek farn, Dmcq, Doug Pardee, DrStr4ng3Lov, Dreftymac, Ds13, Glass Tomato, Haakon, Happysailor, Iwilcox, Jim1138, JonHarder, Lee.crabtree, Leibniz, Liao, Lprichar, MDE, Materialscientist, Michael Hardy, Nibuod, Paul Mackay, Phil Boswell, Quarl, Quinobi, Quuxplusone, R'n'B, RucasHost, Shadowdrak, SoftwareDeveloper, Spayrard, Spikeballs, Statkit1, Stepheng3, TakuyaMurata, The Anome, Thv, Tinsoul, TittoAssini, Tobias Bergemann, TonyMath, Valodzka, Victoryoalli, Vrenator, Wardbekker, 61 anonymous edits Program optimization Source: http://en.wikipedia.org/w/index.php?oldid=517167395 Contributors: Aaronbrick, Alfio, Andreas Kaufmann, AnthonyQBachler, Apoc2400, Arch dude, Arneth, Arvindn, Aydee, Bbatsell, Beetstra, Blaisorblade, Bluechimera0, CRGreathouse, Carre, CharlesGillingham, Chinju, Chrisahn, ChristopherCreutzig, Colonies Chris, Conor H., Cybercobra, Cyc, Damian Yerrick, David Gerard, Demonkoryu, Denmartines, Derek farn, Diego Moya, Dominus, Doug Bell, Dwheeler, Ed Poor, EncMstr, Eric119, ErikHaugen, Farooqn, Fredrik, Furrykef, Gaius Cornelius, Garas, GeoffWozniak, Gesslein, Giftlite, Gracefool, Gritzko, Griwiki, Gronky, Haeleth, Haineux, Handcuffed, Hans Bauer, HarisM, Harmil, Harryboyles, Hayk, Hede2000, Hydrargyrum, Iain.mcclatchie, Idont Havaname, JFlin5, JLaTondre, Jacobko, Jagged, Jamelan, Jesse V., John Vandenberg, John of Reading, Kaldosh, Kdakin, Kragen, LodeRunner, Lost.goblin, Lourakis, Lpenz, LukeShu, Lussmu, Lysdexia, MartinHarper, Marudubshinki, Masky2, Mdsam2, Mean as custard, Michael Hardy, Michael Suess, MikeDunlavey, Mikeblas, Mikez302, Mindmatrix, Minghong, Minimac, MrOllie, Murray Langton, N90p, Naddy, Nae'blis, Nageh, NickHodges, Nikai, Oli Filth, Ossworks, PGWG, Pablodiazgutierrez, Paul G, Pcap, Peter.thejackos, Phil Boswell, Pnm, Prawn, Programmer865, Quuxplusone, RFST, RJHall, Radagast83, Rafmar, RamenFueled, RawlinsJ, Riedel, Ripper234, Robert Merkel, Ronaldklen, Ronz, Sakurambo, Sesse, Sethwoodworth, Shlomif, Shreevatsa, SimonTrew, Sliders06, Sligocki, Sopoforic, Soulkeeper, Spankthecrumpet, Spoom, SteveBaker, SudoGhost, TakuyaMurata, Tenbaset, The Anome, TheMandarin, Thorncrag, Tjktyler, Tobias Bergemann, Venkatarun95, Wavelength, Wbm1058, WhatamIdoing, WikiNing, WinMend, Wlievens, Wonko, Yym, ZettaBlade, 169 anonymous edits Optimizing compiler Source: http://en.wikipedia.org/w/index.php?oldid=517963837 Contributors: ABCD, Adrianwn, Afaviram, Alexkon, AlyM, Andykitchen, Apoc2400, Arnold90, Arrenlex, B4hand, BenLTitzer, Bender235, Blaisorblade, Blow, Bluemoose, Bookandcoffee, Boud, CRGreathouse, CanisRufus, Capi, Cfbolz, Chester Markel, Cic, ClintGoss, Cooperised, CryptoDerk, Cybercobra, Cyberia, Damian Yerrick, David-Sarah Hopwood, DavidCary, Dcoetzee, Denmartines, Derek Ross, Derek farn, DouglasHeld, Drak, Drangon, Drj, EdC, Edward, El C, Elektron, Elf, EncMstr, Eyrian, Fry-kun, Furrykef, Georg Peter, Griwiki, Hans Bauer, Hao2lian, Hari, Hiiiiiiiiiiiiiiiiiiiii, Howardzzh, IGeMiNix, JLD, JLaTondre, Jamelan, Jimothytrotter, Jkominek, Jmnandakumar, Jruderman, K.lee, Kaisenl, Kate, Kdakin, Kelly Martin, Lee Daniel Crocker, Lexi Marie, Liao, Mabdul, Mauritsmaartendejong, Mike Van Emmerik, Mladifilozof, Modify, Moosa 27, MrOllie, Mskfisher, Murray Langton, Mux, NJM, Naddy, Neilc, Niceguyedc, Nneonneo, Oicumayberight, Oli Filth, Orderud, Parabolis, Possession, Ptheoch, QTCaptain, Quuxplusone, R'n'B, R. S. Shaw, RedWolf, ReidSpencer, Rgvandewalker, Robert Merkel, Roberto.larcher, Ruakh, Ruud Koot, Sam Hocevar, Sander123, Sbose7890, Scgtrp, SimonKagstrom, Sliders06, Stan Shebs, Steven ganz, TakuyaMurata, Tarka, Tbackstr, Tedickey, Tobias Bergemann, Tony Sidaway, TonyMath, Ultimus, Uriyan, Walk&check, Widefox, Wik, Wlievens, Wmahan, Woohookitty, Ww, Yaidabbtoo, Yumf, Zundark, 136 anonymous edits Compiler-compiler Source: http://en.wikipedia.org/w/index.php?oldid=513064372 Contributors: AVB, Biasoli, Borislav, CRGreathouse, Dark Bane, Derek Ross, Derek farn, Dfletter, E-boy, Ecb29, Ejrh, Elendal, Emperorbma, Enchanter, FoughtKnox, Georgesawyer, Gogo Dodo, Guy Harris, Haikupoet, Hmains, Horus the falcon, IZAK, Iulian.serbanoiu, Jan Hidders, Jeroendv, LesmanaZimmer, Lotje, Lumingz, Macrakis, Marudubshinki, Matiash, MetaNest, Michael B. Trausch, Michael Hardy, Mirror Vax, Neilc, Ngocminh.oss, Night Gyr, Parrt, Paul Foxworthy, Paulbmann, Peak, Pi zero, PierreAbbat, Pohl, Project2501a, Psychonaut, Qndel, Radiant!, RedWolf, Rjstott, Royas, Ruud Koot, Shmert, Sirthias, SonicAD, Spayrard, Spl, Square87, Suruena, TakuyaMurata, Timwi, Tobias Bergemann, Tony Sidaway, Tyomitch, Utcuong3010, Vasywriter, Wikiklrsc, Yahya Abdal-Aziz, Yintan, 93 anonymous edits

81

Image Sources, Licenses and Contributors

82

Image Sources, Licenses and Contributors


Image:Compiler.svg Source: http://en.wikipedia.org/w/index.php?title=File:Compiler.svg License: Creative Commons Attribution-ShareAlike 3.0 Unported Contributors: Surachit Image:Parser Flow.gif Source: http://en.wikipedia.org/w/index.php?title=File:Parser_Flow.gif License: Public Domain Contributors: A3 nm, DevinCook File:Parse Tree Example.svg Source: http://en.wikipedia.org/w/index.php?title=File:Parse_Tree_Example.svg License: GNU Free Documentation License Contributors: DBSand File:Bottom-up Parse Tree Order.svg Source: http://en.wikipedia.org/w/index.php?title=File:Bottom-up_Parse_Tree_Order.svg License: GNU Free Documentation License Contributors: DBSand File:Top-down Parse Tree Order.svg Source: http://en.wikipedia.org/w/index.php?title=File:Top-down_Parse_Tree_Order.svg License: GNU Free Documentation License Contributors: DBSand File:Shift-Reduce Parse Steps for A*2+1.svg Source: http://en.wikipedia.org/w/index.php?title=File:Shift-Reduce_Parse_Steps_for_A*2+1.svg License: GNU Free Documentation License Contributors: DBSand File:Bottom-Up Parser.svg Source: http://en.wikipedia.org/w/index.php?title=File:Bottom-Up_Parser.svg License: GNU Free Documentation License Contributors: DBSand Image:rule.svg Source: http://en.wikipedia.org/w/index.php?title=File:Rule.svg License: Public Domain Contributors: Stevertigo Image:LR Parser.png Source: http://en.wikipedia.org/w/index.php?title=File:LR_Parser.png License: GNU Free Documentation License Contributors: Bayo, Flappiefh, Ilia Kr., Mhaesen, TommyBee, Wku2m5rr

License

83

License
Creative Commons Attribution-Share Alike 3.0 Unported //creativecommons.org/licenses/by-sa/3.0/

Potrebbero piacerti anche