A compiler performs two major tasks:
•
•
Analysis of the source program being compiled Synthesis of a target program
Almost all modern compilers are syntax-directed: The compilation process is driven by the syntactic structure of the source program. A parser builds semantic structure out of tokens, the elementary symbols of programming language syntax. Recognition of syntactic structure is a major part of the analysis task.
Semantic analysis examines the meaning (semantics) of the program. Semantic analysis plays a dual role. It finishes the analysis task by performing a variety of correctness checks (for example, enforcing type and scope rules). Semantic analysis also begins the synthesis phase. The synthesis phase may translate source programs into some intermediate representation (IR) or it may directly generate target code.
CS 536 Spring 2008
©
29
CS 536 Spring 2008
©
30
If an IR is generated, it then serves as input to a code generator component that produces the desired machinelanguage program. The IR may optionally be transformed by an optimizer so that a more efficient program may be generated.
Source Program
Tokens
Scanner
(Character Stream)
Parser
Abstract Syntax Tree (AST)
Type Checker
Decorated AST
Translator
Intermediate Representation Symbol Tables (IR)
Optimizer
IR Code Generator
Target Machine Code
The Structure of a Syntax-Directed Compiler
CS 536 Spring 2008
©
31
CS 536 Spring 2008
©
32
Scanner
The scanner reads the source program, character by character. It groups individual characters into tokens (identifiers, integers, reserved words, delimiters, and so on). When necessary, the actual character string comprising the token is also passed along for use by the semantic phases. The scanner:
• •
example, to register the presence of a particular label or identifier). Optionally formats and lists the source program
•
•