Compiler Construction Flashcards
Left recursion
Top-down parsers cannot handle left-recursion in a grammar
-> transform the grammar to fix this cf sl. 40/41
Context-free grammars
s
syntax is context-free
semantics is context-sensitive
intermediate representation
aka parse tree
-> a notation that is not tied to any particular source language or target machine
Choosing the “right” IR is not an easy task: source code is too
high level for performing many analyses, and machine code is
too low level. Sometimes an AST is used (i.e., closer to source
code), and sometimes a special kind of abstract instruction set
(i.e., closer to machine code).
derivation
s
left-recursion
s
Bytecode
The “machine-code” of the virtual machine
What is the difference between a compiler and an
interpreter?
s
What are important qualities of compilers?
s
Why are compilers commonly split into multiple passes?
s
What are the typical responsibilities of the different parts of a modern compiler?
s
How are context-free grammars specified?
s
What is “abstract” about an abstract syntax tree?
s
What is intermediate representation and what is it for?
s
Why is optimization a separate activity?
s
Is Java compiled or interpreted? What about Smalltalk?
Ruby? PHP? Are you sure?
s
What are the key differences between modern compilers and compilers written in the 1970s?
s
Why is it hard for compilers to generate good error
messages?
s
What is “context-free” about a context-free grammar?
s
Traditional two pass compiler
A classical compiler consists of a front end that parses the source
code into an intermediate representation, and a back end that
generates executable code.
front end
parses the source code into an intermediate representation
- the front end should deal with all things to do with the language and source code
- consists of scanner (tokens) and parser (IR)
back end
generates executable code
- the back end should deal with all things to do with the
target architecture
- translate IR into target machine code
scanner
is responsible for converting the source code into a stream of tokens of the source language
parser
is responsible for recognizing structure in the stream of tokens - job of the parser is to recognize structure in the stream of tokens produced by the scanner - Typically it builds a parse tree representing this structure, but it may also produce another kind of IR (intermediate representation) more suitable for the backend
IR
e.g. parse tree