Purdue University
CS 352
1. Lexical Analysis (Scanning)
2. Syntax Analysis (Parsing)
3. Semantic Analysis (Type Checking and Variable Binding)
4. Optimization
5. Code Generation
CS 352: Compilers: Principles And Practice Changhee Jung
Frontend Structure
Lexical Analysis
Syntactic Analysis
Semantic Analysis
Errors
Abstract
Syntax Tree
Sou
...[Show More]
1. Lexical Analysis (Scanning)
2. Syntax Analysis (Parsing)
3. Semantic Analysis (Type Checking and Variable Binding)
4. Optimization
5. Code Generation
CS 352: Compilers: Principles And Practice Changhee Jung
Frontend Structure
Lexical Analysis
Syntactic Analysis
Semantic Analysis
Errors
Abstract
Syntax Tree
Source Code
Language
Preprocessor Trivial errors
Processing of
#include, #defines
#ifdef, etc
Preprocessed source code (foo.i)
Frontend Structure
Lexical Analysis
Syntax Analysis
Semantic Analysis
Errors
Abstract
Syntax Tree
Source Code
Language
Preprocessor Trivial errors
Processing of
#include, #defines
#ifdef, etc
Preprocessed source code (foo.i)
Note: gcc –E foo.c –o foo.i
to invoke just the
preprocessor
Lexical Analysis Process
Lexical Analysis
(Scanner)
if (b == 0) a = b; Preprocessed source code
(read char by char)
if ( b == 0 ) a = b ;
• Lexical analysis
- Transform multi-character input stream to token stream
- Reduce length of program representation (remove spaces)
Tokens
• Identifiers: x y11 elsex
• Keywords: if else while for break
• Integers: 2 1000 -20
• Floating-point: 2.0 -0.0010 .021e5
• Symbols: + * { } ++ << < <= [ ]
• Strings: “x” “He said, \”I love CS 352\””
• Comments: /* Purdue Boilermakers */
How to Describe Tokens
• Use regular expressions to describe programming
language tokens!
• A regular expression (RE) is defined inductively
– a ordinary character stands for itself
– e empty string
– R|S either R or S (alteration), where R and S are regular expressions
– RS R followed by S (concatenation)
– R* concatenation of R zero or more times
– (R|S)* concatenation of either R or S zero or more times; parentheses
are notational symbols---as in | ---for grouping
Regular Language
• Regular expression describe regular language L
• A regular expression R describes a set of strings of characters
denoted L(R)
• L(R) = the language defined by R
– L(abc) = { abc }
– L(hello|goodbye) = { hello, goodbye }
– L(1(0|1)*) = all binary numbers that start with a 1
• Each token can be defined using a regular expression
Regular Expression Notationa
[Show Less]