L2-Lexical1 - Introduction to Compiler Design Lexical...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Compiler Design Lexical Analysis I Professor Yi-Ping You Department of Computer Science http://www.cs.nctu.edu.tw/~ypyou/ Page 1 Introduction to Compiler Design, Spring 2010
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Supplements to Text Book http://dragonbook.stanford.edu/ Errata sheet http://infolab.stanford.edu/~ullman/dragon/ rrata1.html errata1.html http://infolab.stanford.edu/~ullman/dragon/ rrata html errata.html Page 2 Introduction to Compiler Design, Spring 2010
Background image of page 2
Outline Lexical Analysis and Tokens Regular Expressions Interaction between Scanner & Parser Implementation of Lexical Analysis Transition Diagram Page 3 Introduction to Compiler Design, Spring 2010
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Structure of a Compiler Lexical Analyzer Source Code y (scanner) Syntax Analyzer (parser) Semantic Analyzer Intermediate Code enerator Symbol Table Error Handler Code Optimizer Generator Code Generator Page 4 Introduction to Compiler Design, Spring 2010 Target Code
Background image of page 4
Lexical Analysis Process if (b == 0) a = b; Preprocessed source code, read char by char Lexical Analysis/Scanner if ( b == 0 ) a = b ; Transform multi-character input stream to token stream KWif ( ID == NUM ) ID = ID SEMI p Reduce length of program representation (remove spaces) Page 5 Introduction to Compiler Design, Spring 2010
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Examples of Tokens Token allest logically cohesive sequence of characters smallest logically cohesive sequence of characters of interest in source program Token Lexeme Single-character operators = + - > <> Multi-character operators := == <> -> Keywords if else while break Identifiers my_variable flag1 Numeric constants/literals 123 45.67 8.9e+05 ’ ‘’ ‘ Character literals ‘a’ ‘~’ ‘\’ String literals “abcd” Page 6 Introduction to Compiler Design, Spring 2010
Background image of page 6
Tokens, Patterns and Lexemes A token is a pair a token name and an optional ttribute value attribute value E.g., identifier , keyword , operator attern a description of the form that the A pattern is a description of the form that the lexemes of a token may take egular expression Regular expression E.g., [A Za z_][A Za z0 9_]* A lexeme is a sequence of characters in the source program that matches the pattern for a token E.g., x , y ; if , else , while ; + , - , < Page 7 Introduction to Compiler Design, Spring 2010
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Attributes for Tokens: Example E = M * C ** 2 < id , pointer to symbol table entry for E > < assign-op > < id , pointer to symbol table entry for M > < mult-op > < id , pointer to symbol table entry for C > < exp-op > < number , integer value 2 > Page 8 Introduction to Compiler Design, Spring 2010
Background image of page 8
Lexical Errors Some errors are out of power of lexical analyzer recognize: to recognize: fi (a == f(x)) … = 2r; d 2r; However it may be able to recognize errors like: d = “abc; Such errors are recognized when no pattern for tokens matches a character sequence Page 9 Introduction to Compiler Design, Spring 2010
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Error Recovery Why error recovery?
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/25/2010 for the course ALL 0204 taught by Professor 79979 during the Spring '10 term at National Chiao Tung University.

Page1 / 34

L2-Lexical1 - Introduction to Compiler Design Lexical...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online