02-Lexical Analysis
Total Page:16
File Type:pdf, Size:1020Kb
Lexical Analysis April 3, 2013 Wednesday, April 3, 13 Previously on CSE 131b... Structure of a modern compiler The Structure of a Modern Compiler Source Lexical Analysis Code Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code Wednesday, April 3, 13 Where are we? Where We Are Source Lexical Analysis Code Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code Wednesday, April 3, 13 Goal of Lexical Analysis Breaking the program down into words or “tokens” Input: code (character stream) w h i l e ( i p < z ) \n \t + + i p ; while (ip < z) ++ip; Wednesday, April 3, 13 Goal of Lexical Analysis Output: Token Stream T_While ( T_Ident < T_Ident ) ++ T_Ident ip z ip w h i l e ( i p < z ) \n \t + + i p ; while (ip < z) ++ip; Wednesday, April 3, 13 The Token Stream is then used as input for Parser (syntax analysis) While < ++ Ident Ident Ident ip z ip T_While ( T_Ident < T_Ident ) ++ T_Ident • ip z ip w h i l e ( i p < z ) \n \t + + i p ; while (ip < z) ++ip; Wednesday, April 3, 13 What’s a token? • What’s a lexical unit of code? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; What is my name ? Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while • Punctuation: ( ) { } ; Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while • Punctuation: ( ) { } ; • Operand: + - ++ Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while • Punctuation: ( ) { } ; • Operand: + - ++ • Relation: < > = Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while • Punctuation: ( ) { } ; • Operand: + - ++ • Relation: < > = • Identifier: (variable name,function name) foo foo_2 Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while • Punctuation: ( ) { } ; • Operand: + - ++ • Relation: < > = • Identifier: (variable name,function name) foo foo_2 • Integer, float point, string: 2345 2.0 “hello world” Wednesday, April 3, 13 ScanningToken a Source Type File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; • Keyword: for int if else while • Punctuation: ( ) { } ; • Operand: + - ++ • Relation: < > = • Identifier: (variable name,function name) foo foo_2 • Integer, float point, string: 2345 2.0 “hello world” • Whitespace, comment /* this code is awesome */ Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( i1 p3 7 < < z )i \n) \t\n \t+ + i+ pi ; Wednesday, April 3, 13 Scanning a Source File w h i l e ( 1 3 7 < i ) \n \t + + i ; T_While Wednesday, April 3, 13 Scanning a Source File w h i l e ( 1 3 7 < i ) \n \t + + i ; T_While Token Wednesday, April 3, 13 Scanning a Source File w h i l e ( 1 3 7 < i ) \n \t + + i ; Lexeme: the piece of the original program from which we made the token T_While Token Wednesday, April 3, 13 Scanning a Source File w h i l e ( 1 3 7 < i ) \n \t + + i ; T_While ( T_IntConst 137 Wednesday, April 3, 13 Scanning a Source File w h i l e ( 1 3 7 < i ) \n \t + + i ; SomeSome tokenstokens cancan havehave attributesattributes thatthat storestore T_While ( T_IntConst extraextra informationinformation aboutabout thethe token.token. HereHere wewe 137 storestore whichwhich integerinteger isis represented.represented. Wednesday, April 3, 13 Lexical Analyzer • Recognize substrings that correspond to tokens: lexemes • Lexeme: actual text of the token • For each lexeme, identify token type • < Token type, attribute> • attribute: optional, extra information, often numeric value Wednesday, April 3, 13 Why is this process hard? Wednesday, April 3, 13 Scanning is Hard ● FORTRAN: Whitespace is irrelevant DO 5 I = 1,25 DO5I = 1.25 Thanks to Prof. Alex Aiken Wednesday, April 3, 13 Scanning is Hard ● C++: Nested template declarations vector<vector<int>> myVector Thanks to Prof. Alex Aiken Wednesday, April 3, 13 Scanning is Hard ● C++: Nested template declarations vector < vector < int >> myVector Thanks to Prof. Alex Aiken Wednesday, April 3, 13 Scanning is Hard ● C++: Nested template declarations (vector < (vector < (int >> myVector))) ● Again, can be difficult to determine where to split. Thanks to Prof. Alex Aiken Wednesday, April 3, 13 Challenges for Lexical Analyzer • How do we determine which lexemes are associated with each token? • When there are multiple ways we could scan the input, how do we know which one to pick? • if if1 • How do we address these concerns efficiently? Wednesday, April 3, 13 Associate Lexemes to Tokens • Tokens: categorize lexemes by what information they provide. • Associate lexemes to token: Pattern matching • How to describe patterns?? Wednesday, April 3, 13 Token: Lexemes • Keyword: for int if else while • Punctuation: ( ) { } ; • Operand: + - ++ • Relation: < > = • Identifier: (variable name,function name) foo foo_2 • Integer, float point, string: 2345 2.0 “hello world” • Whitespace, comment /* this code is awesome */ Wednesday, April 3, 13 Token: Lexemes • Keyword: for int if else while Finite possible lexemes • Punctuation: ( ) { } ; • Operand: + - ++ • Relation: < > = • Identifier: (variable name,function name) foo foo_2 • Integer, float point, string: 2345 2.0 “hello world” • Whitespace, comment /* this code is awesome */ Wednesday, April 3, 13 Token: Lexemes • Keyword: for int if else while Finite possible lexemes • Punctuation: ( ) { } ; • Operand: + - ++ Infinite • Relation: < > = possible lexemes • Identifier: (variable name,function name) foo foo_2 • Integer, float point, string: 2345 2.0 “hello world” • Whitespace, comment /* this code is awesome */ Wednesday, April 3, 13 • How do we describe which (potentially infinite) set of lexemes is associated with each token type? Wednesday, April 3, 13 Formal Languages ● A formal language is a set of strings.