
15-411 Compiler Design, Fall 2020
Lab 4
Seth and co.
Test Programs Due: 11:59 pm, Tuesday, November 3, 2020
Checkpoint Due: 11:59 pm, Thursday, November 5, 2020
Compilers Due: 11:59 pm, Thursday, November 12, 2020
1 Introduction
The goal of the lab is to implement a complete compiler for the language L4. This language extends
L3 with pointers, arrays, and structs. With the ability to store global state, you should be able to
write a wide variety of interesting programs. As always, correctness is paramount, but you should
take care to make sure your compiler runs in reasonable time.
2 L4 Syntax
The lexical specification of L4 is changed by adding ’[’, ’]’, ’.’, and ’->’ as lexical tokens; see
Figure 1. Whitespace, token delimiting, and comments are unchanged from languages L1, L2, and
L3.
The syntax of L4 is defined by the (no longer context-free!) grammar in Figure 2. Ambiguities
in this grammar are resolved according to the operator precedence table in Figure 3and the rule
that an else provides the alternative for the most recent eligible if. Note that according to this
precedence table, *x++ parses as *(x++), which isn’t valid syntax. While we could allow *x++
because *x is unambiguously an lvalue and (*x)++ is unambiguously a statement, we disallow it.
This is both to match the precedence rules and also to avoid confusion with the different semantics
that statement has in C.
L4 syntax is not context-free
As noted above, the grammar presented for L4 is no longer context free. Consider, for example,
the statement
foo * bar;
If foo is a type name, then this is a declaration of a foo pointer named bar. If, however, foo is
not a type name, then this is a multiplication expression used as a statement.
For those of you using parser combinator libraries, you will be able to backtrack from a parse
decision based on whether an identifier is a type name, so this case should not be a problem.
1