1. Home
  2. Explore
  3. Computer Science
  4. Computational Thinking and Compilers

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Computational Thinking and Compilers

Speaker 1Speaker 2

Computational Thinking and Compilers

18:49
0:00-18:49
  1. Maria

    Welcome back. Today we're exploring Alfred V. Aho's central idea: abstractions and algorithms transformed compiler design from an art into a science. He connects that history to computational thinking, which became especially prominent after Jeannette Wing argued in 2006 that it should be taught alongside reading, writing, and arithmetic.

  2. Simon

    And computational thinking is not simply knowing how to program. Aho's preferred definition focuses on the thought processes involved in formulating problems with abstractions, so that their solutions can be represented as computational steps and algorithms. In other words, we simplify a problem in a useful way, then describe a method that a computer can carry out.

  3. Maria

    There is still no single standard definition of computational thinking in education. But compiler design is a particularly good illustration because compilers take a complicated human-written program and systematically transform it into another, equivalent program.

  4. Simon

    Before we discuss compilers, we need to understand abstraction. That word is difficult to define universally because every field develops its own abstractions. The abstractions used in chemistry are not the same as those used in music, so a model that is meaningful to a musician may mean nothing to a chemist.

  5. Maria

    In their Turing Award paper, Aho and Jeffrey Ullman define an abstraction as a mathematical model with two components: a data model and a set of operations. The second part is important. An abstraction does not merely describe what data looks like; it also provides operations for manipulating that data. Those operations can be viewed as a small programming language, although that language does not have to be Turing-complete.

  6. Simon

    A dictionary is a simple example. Its data model consists of a universal set of possible elements, called U, and a subset S, initially empty. Its operations are to insert an element into S, delete an element from S, and check whether a particular element is in S. That is exactly the sort of abstraction a compiler can use for a symbol table.

  7. Maria

    An algorithm, informally, is a recipe for doing something. More formally, Aho uses the definition of a Turing machine that halts on every input. A machine that does not necessarily halt corresponds instead to a partially computable function. The halting requirement matters because an algorithm is expected to produce an answer rather than run forever.

  8. Simon

    Aho and Ullman also proposed a taxonomy of computer science abstractions. Their four initial categories are fundamental abstractions, abstract implementations, declarative abstractions, and computational abstractions. The categories are useful because they distinguish everyday data structures from higher-level specifications and from complete models of computation.

  9. Maria

    Fundamental abstractions are commonly introduced in a first computer science course: dictionaries, stacks, queues, graphs, trees, and classes with methods. Abstract implementations are data structures used to implement those fundamentals, such as hash tables and different kinds of search trees.

  10. Simon

    Declarative abstractions specify what is wanted without specifying how to achieve it. Full graphs are one example, and they do not need to be Turing-complete. Computational abstractions, by contrast, model programming languages more closely and are Turing-complete; examples include Turing machines, Boolean circuits, and quantum circuits.

  11. Maria

    Abstractions can also be layered. One abstraction can be implemented by an algorithm or in terms of another abstraction. For example, a dictionary could be implemented using a linked-list abstraction. This layering is useful because it lets us reason about the running time of a higher-level abstraction in terms of the implementation underneath it.

  12. Simon

    Now let's apply these ideas to compilers. Here, a compiler means a program that translates a source program into a semantically equivalent target program. Since the 1960s and 1970s, compiler design has commonly been viewed as a sequence of phases, each translating one representation into another equivalent but lower-level representation.

  13. Maria

    The early phases are called the front end, and the later phases are called the back end. In a modern framework such as LLVM, different front ends translate different programming languages into one intermediate representation, LLVM IR. An optimizer transforms that representation so that a better target program can be produced, and one of several target-language code generators then completes the back end.

  14. Simon

    This modular structure is a major practical benefit. A new language can use an existing LLVM back end simply by supplying a new front end that produces LLVM IR. Conversely, supporting an existing language on a new machine mainly requires a new generator that translates LLVM IR into that machine's code.

  15. Maria

    The first major declarative abstraction in the compiler pipeline is the regular expression, used for lexical analysis. Its data model is a set of strings over an alphabet, and its operations include union, concatenation, and Kleene closure. Regular expressions became widely used in tools and languages such as Lex and Perl.

  16. Simon

    Different algorithms can implement regular-expression matching with very different running times. Ken Thompson's late-1960s approach simulated a nondeterministic finite automaton in time linear in the product of the regular-expression length and the input length. Aho later developed a Unix search program using lazy, dynamic construction of DFA transitions, with observed time linear in the sum of those two lengths.

  17. Maria

    Lex made these ideas accessible. A user writes a specification describing token patterns, and Lex generates a lexical analyzer without requiring the user to learn the pattern-matching algorithm. For example, a rule recognizing one or more digits can store the digit sequence in a table and return a NUM token together with a pointer to that stored text.

  18. Simon

    Lex and descendants such as Flex are still used to build lexers. A lexer turns a character stream into tokens, which gives the next compiler phase a cleaner input. The next phase is syntax analysis, or parsing.

  19. Maria

    Parsing commonly uses the declarative abstraction of a context-free grammar. Context-free grammars describe the grammatical structure of programming languages, and the idea is much older than modern computing: it is attributed to the Indian scholar Panini, who used such grammatical ideas for Sanskrit between roughly 400 and 200 BC.

  20. Simon

    At the first POPL conference in 1973, Aho, Jeffrey Ullman, and Steve Johnson introduced a syntax-directed formalism. They attached rules to grammar productions so that a parser could translate a token stream into an output, usually a syntax tree. Johnson also created YACC at Bell Labs, a tool that builds syntax analyzers from these specifications.

  21. Maria

    YACC stands for 'Yet Another Compiler Compiler,' a name coined by Jeffrey Ullman. Its descendants, including Bison, remain widely used. Combined with Lex, YACC allows designers to specify a language's tokens and grammar and obtain a working parser rather than implementing every parsing detail by hand.

  22. Simon

    A simple example is a desk calculator. Lex can recognize integer numbers, while a YACC specification can describe addition and multiplication. Compiling the two specifications together produces a calculator, and the combination became especially popular in compiler courses because students could experiment with language syntax.

  23. Maria

    Another important abstraction is the flow graph, used especially in code optimization. A flow graph models control flow: its nodes represent basic blocks of an intermediate representation, and its directed edges indicate which blocks can follow which others.

  24. Simon

    Consider a three-address statement such as U equals V plus W. That statement generates a definition of U. The definition reaches a later statement if there is an execution path from the first statement to the later one on which U is not redefined, or 'killed.' The reaching-definitions problem asks which definitions may reach which program points.

  25. Maria

    Reaching definitions support optimizations such as constant propagation and loop-invariant code motion. In the flow-graph abstraction, the data-flow values form a semilattice of sets of variable definitions. Each node has a value for definitions reaching its beginning and another for definitions reaching its end.

  26. Simon

    There are two key operations. A transfer function associated with a node computes the output value at the end of that node from the input value. A confluence operator combines the values arriving from all predecessor nodes to determine the value at the beginning. Because these values form a semilattice, the confluence operator is commutative and associative.

  27. Maria

    The power of this formulation is that the abstraction separates the problem from the particular algorithm used to solve it. Different algorithms can compute the data-flow values depending on the optimization being performed. This is a recurring theme: a good abstraction lets us change an implementation without changing the problem specification.

  28. Simon

    Aho and his coauthors documented these abstractions and algorithms in three major compiler textbooks. The first appeared in 1977, the second in 1986 with Ravi Sethi, and the third in 2007 with Monica Lam. Their covers featured fierce dragons, symbolizing the complexity of compiler design, so the books became known as the Dragon Books.

  29. Maria

    The books had a substantial impact. IBM's first FORTRAN compiler in the 1950s required eighteen experienced staff-years to build. From the mid-1970s onward, languages such as Brian Kernighan's EQN for typesetting equations could be created in weeks using the tools and abstractions developed in compiler research.

  30. Simon

    Aho also described a teaching example from his compiler course at Columbia. Teams of five students designed an original programming language and built a compiler for it over one semester. One memorable language, Upbeat, connected streaming input patterns with user-selected music.

  31. Maria

    The Upbeat team demonstrated the idea with New York Stock Exchange ticker data. When the market rose, the compiled program played lively music; when the market fell, it played gloomy music. Aho noted that during twenty-five years of teaching the course, no student team failed to deliver a working compiler, which he attributed to the abstractions, algorithms, and software-engineering practices they used.

  32. Simon

    The same compiler-design framework can also be applied to quantum programming languages, even though quantum abstractions differ sharply from classical ones. To see why, we need a short review of the four postulates of quantum mechanics.

  33. Maria

    The first postulate says that the state of a physical system is represented by a unit vector in a Hilbert space, a complex vector space with an inner product. A qubit is represented by a unit vector, written as alpha times ket zero plus beta times ket one, where the squared absolute values of alpha and beta add up to one.

  34. Simon

    Unlike a classical bit, a qubit can be in a superposition: a complex linear combination of the basis states ket zero and ket one. The state of a closed quantum system evolves through a unitary operator, which can be viewed as a complex square matrix whose rows and columns are orthonormal. Unitary evolution is reversible, even though an ordinary programming language does not have to be reversible.

  35. Maria

    The Hadamard operator illustrates superposition. Applied to ket zero, it produces ket zero plus ket one, divided by the square root of two. Applied to ket one, it produces ket zero minus ket one, divided by the square root of two. Applying the Hadamard operator twice gives the identity operation.

  36. Simon

    The two-qubit CNOT gate has a control qubit and a target qubit. If the control is zero, the target passes through unchanged. If the control is one, the target flips. So an input with control ket one and target ket zero produces control ket one and target ket one.

  37. Maria

    The third postulate concerns measurement. Measurement is not unitary and it collapses a quantum state. For a qubit in the state alpha ket zero plus beta ket one, the result is the classical bit zero with probability equal to the absolute value of alpha squared, or one with probability equal to the absolute value of beta squared. Afterward, the state has collapsed to the corresponding basis state.

  38. Simon

    The fourth postulate describes composite systems. Their state spaces are tensor products of the component state spaces. Combining two two-dimensional qubit spaces gives a four-dimensional space; combining three gives an eight-dimensional space. In general, n qubits have a state space of dimension two to the n, which explains why simulating large quantum systems on classical computers is so difficult.

  39. Maria

    Quantum circuits are a key abstraction for quantum computation, sometimes called the gate model. A circuit is a graph of lines connecting unitary operators and measurement gates. Unlike Boolean circuits, it cannot freely fan out, because the no-cloning theorem says that an unknown quantum state cannot simply be copied.

  40. Simon

    A circuit containing a Hadamard gate followed by a CNOT gate can create a Bell state. Starting with ket zero, zero, the output is ket zero, zero plus ket one, one, divided by the square root of two. This state cannot be expressed as a tensor product of two separate states, so it is entangled.

  41. Maria

    Quantum computing became especially important for cryptography after Peter Shor showed in 1994 that a quantum computer, combined with classical computation, could factor integers using an order of n cubed operations. If sufficiently large, error-corrected quantum computers became available, Shor's algorithm could threaten RSA and other encryption systems that rely on factoring being computationally difficult.

  42. Simon

    For now, RSA remains safe in practice because current quantum computers are far too small. Companies such as Google and IBM have built machines with around a hundred qubits, while running Shor's algorithm against practical RSA sizes would require a million or more qubits. Still, cryptographers are developing systems designed to resist both classical and quantum attacks.

  43. Maria

    Quantum programming languages can still use the traditional compiler framework. Aho described work showing that classical compiler techniques could support quantum compilers, and Microsoft later developed Q sharp as part of its quantum development kit. The language is one example of the growing collection of quantum programming languages.

  44. Simon

    A question from the discussion concerned LL versus LR parsing. LL parsing is a top-down, recursive-descent style, while LR parsing is bottom-up. In the early days of compiler design, there were many competing parsing strategies, but Donald Knuth's 1965 work on LR(k) grammars characterized a powerful class of grammars that could be parsed deterministically.

  45. Maria

    Aho explained why YACC and LR-style tools became attractive. Compiler writers often had to distort a natural grammar to make it fit a restricted LL or LR form, and many were not specialists in parsing theory. YACC let them write more natural specifications and use rules to resolve conflicts, such as favoring shift in a shift-reduce conflict.

  46. Simon

    Precedence is another example. C has operators at sixteen precedence levels. An unambiguous grammar might require sixteen different syntactic categories, but an ambiguous expression rule can be paired with declarations specifying precedence and associativity. YACC also reports conflicts, helping developers discover ambiguities that they might otherwise miss.

  47. Maria

    That does not mean parser generators are always the right choice. Experienced developers may choose another strategy, and complex languages such as C++ can require specialized parser construction. Aho's broader point is that theory and practice support each other: theory is motivated by practice, and practice is often improved when it is guided by theory.

  48. Simon

    Compiler construction has also become more like software engineering because of frameworks such as GCC and LLVM. And researchers are exploring machine learning and natural-language techniques that can generate programs or even simple compilers from examples or spoken descriptions. Whether such methods can handle the complexity of a language like C++ remains an open question.

  49. Maria

    Natural language presents a particular challenge because it is inherently ambiguous. If a programmer and a compiler disagree about what a sentence means, the resulting program may not match the programmer's intention. That is one reason formal languages and carefully defined abstractions remain so valuable.

  50. Simon

    Another discussion question asked whether language translators can teach computational thinking to people who are not computer scientists. Aho's answer was cautiously positive. He emphasized that thinking before programming is better than programming first and only afterward wondering what went wrong.

  51. Maria

    He also pointed to games and puzzles as promising teaching tools. Students may enjoy learning by solving problems in a game more than by reading explanations alone. Since learners have different styles, the same concept may need to be presented through words, pictures, videos, or repeated explanations in different forms.

  52. Simon

    Finally, quantum compilers differ from classical compilers in an important way: quantum programs cannot freely copy variable values because of the no-cloning theorem. Also, quantum computers are generally used as components in hybrid systems. A classical computer prepares the input, the quantum processor performs quantum operations and measurements, and the classical computer processes the resulting classical bits.

  53. Maria

    Shor's algorithm illustrates that hybrid arrangement. Classical preprocessing reduces factoring to a period-finding problem, the quantum computer computes the period, and classical post-processing turns the result into useful information. Current quantum machines are controlled by classical computers and are kept at extremely low temperatures, so they are not yet devices we can simply carry in a phone.

  54. Simon

    The big picture is that abstractions let us state problems clearly, while algorithms provide executable solutions. From dictionaries and regular expressions to grammars, flow graphs, and quantum circuits, these tools make complex systems understandable and implementable.

  55. Maria

    And that brings us back to the thesis: decades of abstractions and algorithms transformed compiler design from an art into a science. They made compilers modular, analyzable, teachable, and reusable—and they continue to provide a foundation even as computing moves toward quantum systems and machine-assisted programming.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent