IAE Academy

The whole Compiler Design formula sheet, to print

20 formulas in 5 topics, each with when to use it. Each is explained in the topic notes.

Parsing: top-down and bottom-up parsers

Read the notes

REMOVING IMMEDIATE LEFT RECURSION

Conditions: no starts with , and no is . is a new nonterminal. The new grammar has alternatives in total: for and for .

LEFT FACTORING

Applies when two or more alternatives of share a nonempty prefix (take the longest one); is a new nonterminal, and an empty becomes . Repeat until no two alternatives of any nonterminal share a prefix.

LL(1) CONDITION

Also at most one alternative of may derive . The condition holds for every nonterminal exactly when no cell of the LL(1) table has two entries.

MOVES OF A DETERMINISTIC PARSER

Applies to LL(1) and LR parsers on an accepted string of tokens whose parse tree has internal nodes, not counting the augmented start . In LL, counts the matches and the expansions; in LR, counts the shifts and the reductions. A node for an -production counts in . Accept, and the final match of $, are not counted unless the question says so.

LR(1) CLOSURE

If is empty or nullable, itself is among the new lookaheads. The initial item is with lookahead $. Goto works exactly as in LR(0), carrying the lookaheads along unchanged. Reduce by only on the item's own lookahead.

GRAMMAR-CLASS HIERARCHY

Each inclusion is proper. A grammar class here means the set of grammars the method parses without conflict. LL(1) is not contained in LALR(1), SLR(1) or LR(0), and none of those is contained in LL(1): LL(1) is incomparable with each of them. More generally every LL(k) grammar is LR(k).

Syntax-directed translation

Read the notes

FORM OF A SYNTHESIZED RULE

The attribute being defined belongs to the head ; its arguments are attributes of the body symbols (or of itself).

FORM OF AN INHERITED RULE

The attribute being defined belongs to a body symbol ; the arguments may be attributes of the head and of the body symbols.

L-ATTRIBUTED CONDITION

Also allowed: dependence on other attributes of itself, provided that creates no cycle. Not allowed: dependence on a synthesized attribute of the head . Synthesized attributes of may use any attribute in the production, including 's inherited attributes, provided no cycle results.

Runtime environments: activation records and storage allocation

Read the notes

LIVE ACTIVATIONS

Holds under stack allocation, where every live activation has exactly one record on the stack. Counts the records on the stack at one instant; the maximum over the run is the peak stack depth. Includes the main program's record.

ACCESS-LINK HOPS FOR A VARIABLE

Static scoping with access links. A procedure at nesting depth uses a variable whose nearest enclosing declaration is at depth . Start at the current record and follow access links this many times; 0 means the variable is local.

ACCESS LINK OF A CALLEE

Procedure (depth ) calls procedure (depth ), which is visible from , so . Follow this many access links from 's record; the record reached is the most recent activation of 's lexical parent, and 's access link is set to it. Cases: ( declared inside ) gives 0, so the link points to itself; gives 1, so the link equals 's own access link; gives links.

Intermediate code generation

Read the notes

INSTRUCTION AND TEMPORARY COUNT FOR AN ASSIGNMENT

For where has operator occurrences and nothing is shared. The scheme gives each operator a new temporary (temporaries are never reused) and then copies the last one to . With a DAG, replace by the number of operator nodes in the DAG.

ONE-DIMENSIONAL ARRAY ADDRESS

is the width of one element in bytes and is the lower bound of the index. For a 0-based array the term is just .

TWO-DIMENSIONAL ARRAY ADDRESS, ROW-MAJOR

is the number of columns, and are the lower bounds. Row-major is the default for C-style arrays unless stated otherwise.

TWO-DIMENSIONAL ARRAY ADDRESS, COLUMN-MAJOR

is the number of rows. Use this only when the question says the layout is column-major.

ASSIGNMENTS IN SSA FORM

Each original assignment gets exactly one new name, and each is one more assignment. Since each SSA name is assigned once, this is also the number of names created by assignments and -functions. Variables that are only read and never assigned (inputs) keep their names and are not counted.

Local optimisation and data-flow analysis

Read the notes

LIVE VARIABLES (BACKWARD, UNION)

A variable is live at a point if its current value may be read later before being overwritten. Initialise IN[B] = ∅ for all B; OUT of an exit block is ∅ unless the question names live-out variables.

REACHING DEFINITIONS (FORWARD, UNION)

A definition reaches a point if some path from it to that point does not redefine the variable. Initialise OUT[B] = ∅; IN[entry] = ∅.

AVAILABLE EXPRESSIONS (FORWARD, INTERSECTION)

An expression is available at a point if on every path to it the expression has been computed and its operands not changed since. IN[entry] = ∅; initialise OUT[B] to the set U of all expressions for every other block.