Lecture 2 Flashcards
What are the phases of a compiler
It consists of the analysis and synthesis phases:
Analysis phase:
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
Synthesis Analysis
1. Intermediate Code Generation
2. Code Optimization
3. Target Code Generation
Tasks of lexical analysis
- Read input characters of the source program
- Group them into lexemes
- Produce as output a sequence of tokens for each output
- Stripping off comments and whitespaces
- Correlate error messages with line number in the source program
Master the diagram of the lexical analysis
What is the job of the syntax analyser
The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream.
What is the job of semantic analysis?
- The semantic analyzer uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition.
- It also gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation.
An example of intermediate representation
Syntax trees are a form of intermediate representation; they are commonly used during syntax and semantic analysis.
three-address code, which consists of a sequence of assembly-like instructions with three operands per instruction.
Mention the two properties of intermediate representation
- it should be easy to produce
- it should be easy to translate into the target machine.
What happens in the code optimization phase
The machine-independent code-optimization phase attempts to improve the intermediate code so that a better target code will result.
Usually better means faster, but other objectives may be desired, such as shorter code, or target code that consumes less power
What is the job of the code generator
The code generator takes as input an intermediate representation of the source program and maps it into the target language.
If the target language is machine code, registers or memory locations are selected for each of the variables used by the program.
Describe the job of the analysis part of the compiler
If the analysis part detects that the source program is either syntactically ill formed or semantically unsound, then it must provide informative messages, so the user can take corrective action.
The analysis part also collects information about the source program and stores it in a data structure called a symbol table, which is passed along with the intermediate representation to the synthesis part.
Describe the job of the synthesis part
The synthesis part constructs the desired target program from the intermediate representation and the information in the symbol table. It is commonly called the back end of the compiler
The front end is split into two parts
Scanner: Responsible for converting character stream to token stream
—Also strips out white space, comments
Parser: Reads token stream; generates Intermediate Representation
Give examples of token stream
Operators & Punctuation: {}[]!+-=*;: …
Keywords: if, while, return
Identifiers: id & actual name
Constants: int, floating-point character, string, …