Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic...
Transcript of Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic...
![Page 1: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/1.jpg)
Midterm ReviewApril 28th
Monday, April 29, 13
![Page 2: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/2.jpg)
Monday, April 29, 13
![Page 3: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/3.jpg)
Midterm
• Close Book
• Close Note
• 80 mins
• 20%
Monday, April 29, 13
![Page 4: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/4.jpg)
Previously on CSE 131b...
The Structure of a Modern Compiler
Lexical Analysis
Syntax Analysis
Semantic Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
Machine
Code
Structure of a modern compiler
Wednesday, April 3, 13
Midterm
Monday, April 29, 13
![Page 5: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/5.jpg)
• Basic compiler concepts
• Lexical Analysis
• Goal, Input, Output
• Formal Language: regular expression/DFA/NFA
• Conflict Resolution
• Syntax Analysis
• Goal, input, output
• Formal Language: context free grammar, leftmost/rightmost derivation, ambiguity
• Parsing: parse tree construction, top down parsing (bfs,dfs, predictive, LL(1))
• Semantic Analysis
• Goal
• Scope: static scope, dynamic scope, symbol table
• Type checking: type inference, function overloading, function overriding
Monday, April 29, 13
![Page 6: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/6.jpg)
Basic Concepts
• What’s a compiler?
• What’s an interpreter?
• What are the main phases of a compiler?
Monday, April 29, 13
![Page 7: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/7.jpg)
• Compiler
Compilerprogram in source language
program in target language
Error message
Input
Output
Monday, April 29, 13
![Page 8: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/8.jpg)
• Compiler
Compilerprogram in source language
program in target language
Error message
Input
Output
• Translates
Monday, April 29, 13
![Page 9: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/9.jpg)
• Compiler
Compilerprogram in source language
program in target language
Error message
Input
Output
• Translates
• Typically lower the level of abstraction of the program
Monday, April 29, 13
![Page 10: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/10.jpg)
• Compiler
Compilerprogram in source language
program in target language
Error message
Input
Output
• Translates
• Typically lower the level of abstraction of the program • High-level programming language -> machine code (C/C++ -> X86, etc)
Monday, April 29, 13
![Page 11: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/11.jpg)
• Compiler
Compilerprogram in source language
program in target language
Error message
Input
Output
• Translates
• Typically lower the level of abstraction of the program • High-level programming language -> machine code (C/C++ -> X86, etc)
• We expect the program produced by the compiler to be better, (faster, consumes less memory, etc) than the original program
Monday, April 29, 13
![Page 12: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/12.jpg)
• Compiler
Interpreter
Compilerprogram in source language
program in target language
Error message
ProgramInput
output
Input
Output
• Interpreter
Static: before runtime
Dynamic: during runtime
Examples: python, shell, javascript, PHP
Monday, April 29, 13
![Page 13: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/13.jpg)
Previously on CSE 131b...
The Structure of a Modern Compiler
Lexical Analysis
Syntax Analysis
Semantic Analysis
IR Generation
IR Optimization
Code Generation
Optimization
SourceCode
Machine
Code
Structure of a modern compiler
Wednesday, April 3, 13
Monday, April 29, 13
![Page 14: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/14.jpg)
Lexical Analysis• Goal, Input, Output
• Formal Language
• regular expression (need to know what language a regular expression means, and vice versa)
• DFA/NFA
• Conflict Resolution
• Maximal Munch
Monday, April 29, 13
![Page 15: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/15.jpg)
w h i l e ( i < z ) \n \t + i p ;
while (ip < z) ++ip;
p + +
T_While ( T_Ident < T_Ident ) ++ T_Ident
ip z ip
Goal of Lexical AnalysisOutput: Token Stream
• For each lexeme, identify token type
• < Token type, attribute>Monday, April 29, 13
![Page 16: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/16.jpg)
Scanning a Source File
w h i l e ( i < z ) \n \t + i p ;p + +( 1 < i ) \n \t + i ;3 + +7
Token Type
• 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 */Monday, April 29, 13
![Page 17: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/17.jpg)
• How do we describe which (potentially infinite) set of lexemes is associated with each token type?
• What type of formal language should we use to describe tokens?
Monday, April 29, 13
![Page 18: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/18.jpg)
Regular Expressions
● Regular expressions are a family of descriptions that can be used to capture certain languages (the regular languages).
● Often provide a compact and human-readable description of the language.
● Used as the basis for numerous software systems, including the flex tool we will use in this course.
Monday, April 29, 13
![Page 19: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/19.jpg)
Simple Regular Expressions
● Suppose the only characters are 0 and 1.
● Here is a regular expression for strings containing 00 as a substring:
(0 | 1)*00(0 | 1)*
Monday, April 29, 13
![Page 20: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/20.jpg)
• More examples
• Whitespace: [ \t\n]+
• Integers: [+\-]?[0-9]+
• Hex numbers: 0x[0-9a-f]+
• identifier
• [A-Za-z]([A-Za-z]|[0-9])*
Wednesday, April 3, 13
Monday, April 29, 13
![Page 21: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/21.jpg)
Recognizing Regular Language
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 22: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/22.jpg)
Recognizing Regular Language
• Finite Automata
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 23: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/23.jpg)
Recognizing Regular Language
• Finite Automata
• DFA (Deterministic Finite Automata)
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 24: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/24.jpg)
Recognizing Regular Language
• Finite Automata
• DFA (Deterministic Finite Automata)
• NFA (Non-deterministic Finite Automata)
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 25: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/25.jpg)
Recognizing Regular Language
• Finite Automata
• DFA (Deterministic Finite Automata)
• NFA (Non-deterministic Finite Automata)
• DFA and NFA have equal power
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 26: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/26.jpg)
Recognizing Regular Language
• Finite Automata
• DFA (Deterministic Finite Automata)
• NFA (Non-deterministic Finite Automata)
• DFA and NFA have equal power
• Need to be able to construct DFA/NFA for given regular expressions
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 27: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/27.jpg)
Recognizing Regular Language
• Finite Automata
• DFA (Deterministic Finite Automata)
• NFA (Non-deterministic Finite Automata)
• DFA and NFA have equal power
• Need to be able to construct DFA/NFA for given regular expressions
• Need to understand what language a DFA/NFA recoganize
What is the machine that recognize regular language??
Monday, April 29, 13
![Page 28: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/28.jpg)
Lexing Ambiguities
T_For forT_Identifier [A-Za-z_][A-Za-z0-9_]*
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
Monday, April 29, 13
![Page 29: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/29.jpg)
Lexing Ambiguities
T_For forT_Identifier [A-Za-z_][A-Za-z0-9_]*
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
f o tr
Monday, April 29, 13
![Page 30: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/30.jpg)
Conflict Resolution
● Assume all tokens are specified as regular expressions.
● Algorithm: Left-to-right scan.
● Tiebreaking rule one: Maximal munch.
● Always match the longest possible prefix of the remaining text.
Monday, April 29, 13
![Page 31: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/31.jpg)
Implementing Maximal Munch
● Given a set of regular expressions, how can we use them to implement maximum munch?
● Idea:
● Convert expressions to NFAs.
● Run all NFAs in parallel, keeping track of the last match.
● When all automata get stuck, report the last match and restart the search at that point.
Monday, April 29, 13
![Page 32: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/32.jpg)
Implementing Maximal Munch
● Given a set of regular expressions, how can we use them to implement maximum munch?
● Idea:
● Convert expressions to NFAs.
● Run all NFAs in parallel, keeping track of the last match.
● When all automata get stuck, report the last match and restart the search at that point.
Monday, April 29, 13
![Page 33: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/33.jpg)
• Example
Monday, April 29, 13
![Page 34: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/34.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
Monday, April 29, 13
![Page 35: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/35.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
Monday, April 29, 13
![Page 36: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/36.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 37: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/37.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 38: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/38.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 39: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/39.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 40: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/40.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 41: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/41.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 42: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/42.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 43: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/43.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 44: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/44.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 45: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/45.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 46: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/46.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 47: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/47.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 48: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/48.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 49: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/49.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 50: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/50.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 51: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/51.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 52: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/52.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 53: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/53.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 54: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/54.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 55: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/55.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 56: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/56.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 57: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/57.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 58: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/58.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 59: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/59.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 60: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/60.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 61: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/61.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 62: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/62.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 63: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/63.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 64: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/64.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 65: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/65.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 66: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/66.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 67: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/67.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 68: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/68.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 69: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/69.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 70: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/70.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 71: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/71.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 72: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/72.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 73: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/73.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 74: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/74.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 75: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/75.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 76: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/76.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 77: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/77.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 78: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/78.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 79: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/79.jpg)
T_Do doT_Double doubleT_Mystery [A-Za-z]
Implementing Maximal Munch
start d o
start d o u b l e
start Σ
D O U B L ED O U B
Monday, April 29, 13
![Page 80: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/80.jpg)
A Minor Simplification
d o
d o u b l e
Σ
ε
ε
εstart
Monday, April 29, 13
![Page 81: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/81.jpg)
Syntax Analysis
• Goal, input, output
• Formal Language
• context free grammar
• leftmost/rightmost derivation
• Ambiguity
• Parsing
• parse tree construction
• Abstract Syntax Tree
• Top down parsing (bfs,dfs,predictive, LL(1))
Monday, April 29, 13
![Page 82: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/82.jpg)
w h i l e ( i < z ) \n \t + i p ;
while (ip < z) ++ip;
p + +
T_While ( T_Ident < T_Ident ) ++ T_Ident
ip z ip
While
++
Ident
<
Ident Ident
ip z ip
Monday, April 29, 13
![Page 83: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/83.jpg)
Overview of Syntax Analysis
• Input: stream of tokens from the lexer
• Output: Abstract Syntax Tree (AST)
if
==
b 0
=
a �Hi�
;
Source code: if (b==0) a = “Hi”;
AST:
Monday, April 29, 13
![Page 84: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/84.jpg)
Goal of Syntax Analysis
Monday, April 29, 13
![Page 85: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/85.jpg)
Goal of Syntax Analysis
• Recovered the structure described by the token stream
• Report errors if the tokens do not properly encode a structure
• Differentiate parsing errors and semantic errors
Monday, April 29, 13
![Page 86: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/86.jpg)
What%Parsing%Doesn�t%Do%• %Doesn�t%check:%type%agreement,%variable%declara;on,%
variables%ini;aliza;on,%etc.%
• int x = true; • int y; • z = f(y);
• %Deferred%un;l%seman;c%analysis%
Monday, April 29, 13
![Page 87: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/87.jpg)
Formal Language for Syntax Analysis
Monday, April 29, 13
![Page 88: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/88.jpg)
Formal Language for Syntax Analysis
Monday, April 29, 13
![Page 89: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/89.jpg)
Formal Language for Syntax Analysis
• Can we use regular expressions (on tokens) to specify programming language syntax?
Monday, April 29, 13
![Page 90: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/90.jpg)
• Language of all strings that contain balanced parentheses?
• () (()) ()()() (())()((()()))
• (( )( ()) (()()
• Construct a Finite Automaton for this...?
Monday, April 29, 13
![Page 91: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/91.jpg)
• Language of all strings that contain balanced parentheses?
• () (()) ()()() (())()((()()))
• (( )( ()) (()()
• Construct a Finite Automaton for this...?
) ) ) ) )
( ( ( ( (
Monday, April 29, 13
![Page 92: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/92.jpg)
• Language of all strings that contain balanced parentheses?
• () (()) ()()() (())()((()()))
• (( )( ()) (()()
• Construct a Finite Automaton for this...?
) ) ) ) )
( ( ( ( (
• Limits of regular language: DFA has only finite number of states; cannot perform unbounded counting
Monday, April 29, 13
![Page 93: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/93.jpg)
• Language of all strings that contain balanced parentheses?
• () (()) ()()() (())()((()()))
• (( )( ()) (()()
• Construct a Finite Automaton for this...?
) ) ) ) )
( ( ( ( (
• Limits of regular language: DFA has only finite number of states; cannot perform unbounded counting
• Need a More Powerful Representa3on: Context Free Grammar
Monday, April 29, 13
![Page 94: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/94.jpg)
CFG Terminology• Terminals
• Token or ε
• Non-terminals
• variables
• Start symbol
• Begins the derivation
• Productions
• Specify how non-terminals may be expanded to form strings (replacement rules)
• LHS: single non-terminal, RHS: string of terminals (including ε) or non-terminals
• S → ( S ) S
• S → ε
Monday, April 29, 13
![Page 95: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/95.jpg)
• Need to understand CFG grammar
CFGs for Programming Languages
BLOCK → STMT | { STMTS }
STMTS → ε
| STMT STMTS
STMT → EXPR; | if (EXPR) BLOCK
| while (EXPR) BLOCK | do BLOCK while (EXPR); | BLOCK | …
EXPR → identifier | constant
| EXPR + EXPR | EXPR – EXPR | EXPR * EXPR | ...
Monday, April 29, 13
![Page 96: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/96.jpg)
Leftmost Derivations
→ STMTS
⇒ STMT STMTS
⇒ EXPR; STMTS
⇒ EXPR = EXPR; STMTS
⇒ id = EXPR; STMTS
⇒ id = EXPR + EXPR; STMTS
⇒ id = id + EXPR; STMTS
⇒ id = id + constant; STMTS
⇒ id = id + constant;
BLOCK → STMT | { STMTS }
STMTS → ε
| STMT STMTS
STMT → EXPR; | if (EXPR) BLOCK | while (EXPR) BLOCK
| do BLOCK while (EXPR); | BLOCK | …
EXPR → identifier | constant | EXPR + EXPR | EXPR – EXPR | EXPR * EXPR | EXPR = EXPR | ...
Productions Derivations
Monday, April 29, 13
![Page 97: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/97.jpg)
Leftmost Derivations
→ STMTS
⇒ STMT STMTS
⇒ EXPR; STMTS
⇒ EXPR = EXPR; STMTS
⇒ id = EXPR; STMTS
⇒ id = EXPR + EXPR; STMTS
⇒ id = id + EXPR; STMTS
⇒ id = id + constant; STMTS
⇒ id = id + constant;
BLOCK → STMT | { STMTS }
STMTS → ε
| STMT STMTS
STMT → EXPR; | if (EXPR) BLOCK | while (EXPR) BLOCK
| do BLOCK while (EXPR); | BLOCK | …
EXPR → identifier | constant | EXPR + EXPR | EXPR – EXPR | EXPR * EXPR | EXPR = EXPR | ...
Productions Derivations
• Need to be able to conduct derivations
• What are the two important types of derivations that we talked about?
Monday, April 29, 13
![Page 98: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/98.jpg)
Leftmost Derivations
● A leftmost derivation is a derivation in which each step expands the leftmost nonterminal.
● A rightmost derivation is a derivation in which each step expands the rightmost nonterminal.
● These will be of great importance when we talk about parsing next week.
Monday, April 29, 13
![Page 99: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/99.jpg)
Related Derivations
⇒ E
⇒ E Op E
⇒ int Op E
⇒ int * E
⇒ int * (E)
⇒ int * (E Op E)
⇒ int * (int Op E)
⇒ int * (int + E)
⇒ int * (int + int)
⇒ E
⇒ E Op E
⇒ E Op (E)
⇒ E Op (E Op E)
⇒ E Op (E Op int)
⇒ E Op (E + int)
⇒ E Op (int + int)
⇒ E * (int + int)
⇒ int * (int + int)
Monday, April 29, 13
![Page 100: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/100.jpg)
• Do leftmost and rightmost derivations generate different parsing trees?
Monday, April 29, 13
![Page 101: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/101.jpg)
For ComparisonE
E EOp
int *
E
( int + int )
E EOp
E
E EOp
int *
E
( int + int )
E EOp
A Notational Shorthand
E → int | E Op E | (E)
Op → + | - | * | /
Monday, April 29, 13
![Page 102: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/102.jpg)
For ComparisonE
E EOp
int *
E
( int + int )
E EOp
E
E EOp
int *
E
( int + int )
E EOp
Left-most derivation and right-most derivation generate the same parse tree
A Notational Shorthand
E → int | E Op E | (E)
Op → + | - | * | /
Monday, April 29, 13
![Page 103: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/103.jpg)
For ComparisonE
E EOp
int *
E
( int + int )
E EOp
E
E EOp
int *
E
( int + int )
E EOp
Left-most derivation and right-most derivation generate the same parse tree
The order of the construction is different
A Notational Shorthand
E → int | E Op E | (E)
Op → + | - | * | /
Monday, April 29, 13
![Page 104: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/104.jpg)
• Need to be able to construct the parsing tree
For ComparisonE
E EOp
int *
E
( int + int )
E EOp
E
E EOp
int *
E
( int + int )
E EOp
Left-most derivation and right-most derivation generate the same parse tree
The order of the construction is different
A Notational Shorthand
E → int | E Op E | (E)
Op → + | - | * | /
Monday, April 29, 13
![Page 105: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/105.jpg)
E
E EOp
int * int + int
E EOp
E
E EOp
int * int
E EOp
+ int
A Serious Problem
int * (int + int) (int * int) + int
Ambiguity
• Need to be able to tell if CFG is ambiguous
Monday, April 29, 13
![Page 106: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/106.jpg)
Parse&Tree&vs.&AST&• Parse&Tree,&aka&
concrete&syntax&S
E S +
S E ) (
E S + 5
1 E S +
2 E
S ) (
E S +
3 E
4
+
+ 5
1 +
2 +
3 4
Abstract Syntax Tree
Discards/abstracts unneeded information
• Need to be able to construct AST
Monday, April 29, 13
![Page 107: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/107.jpg)
Parsing Algorithms
• Top Down ( will be tested)
• Beginning with the start symbol, try to guess the productions to apply to end up at the user’s program
• Bottom up (will not be tested)
• Beginning with the user's program, try to apply productions in reverse to convert the program back into the start symbol.
Monday, April 29, 13
![Page 108: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/108.jpg)
• Topdown parsing
• parsing as a search
• breath first search (using what data structure?)
• leftmost depth first search (using what data structure?)
• problem with leftmost depth-first search?
• how to eliminate the problem
• Predictive Parsing
• LL(1)
• Algorithm, parsing table
• Limitation
Monday, April 29, 13
![Page 109: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/109.jpg)
Parsing as a SearchE → TE → T + ET → intT → (E)
E
T
T + E
int
int + E
T + T
T + T + E
(E)
int + T
T + (E)
int + (E)
(T)
(T + E)
…
…
…
…int + T + E
int + int
…
…
……
…
Monday, April 29, 13
![Page 110: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/110.jpg)
Our First Top-Down Algorithm
● Breadth-First Search
● Maintain a worklist of sentential forms, initially just the start symbol S.
● While the worklist isn't empty:
● Remove an element from the worklist.
● If it matches the target string, you're done.
● Otherwise, for each possible string that can be derived in one step, add that string to the worklist.
● Can recover a parse tree by tracking what productions we applied at each step.
Monday, April 29, 13
![Page 111: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/111.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 112: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/112.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
E
Monday, April 29, 13
![Page 113: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/113.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
E
Monday, April 29, 13
![Page 114: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/114.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
E
T T + E
Monday, April 29, 13
![Page 115: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/115.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
E
T T + E
T T + E
Monday, April 29, 13
![Page 116: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/116.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
T T + E
Monday, April 29, 13
![Page 117: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/117.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
T + E
T
Monday, April 29, 13
![Page 118: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/118.jpg)
Breadth-First Search Parsing
Worklist
int + int
E → TE → T + ET → intT → (E)
T + E
T
int (E)
Monday, April 29, 13
![Page 119: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/119.jpg)
• ...
Monday, April 29, 13
![Page 120: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/120.jpg)
Leftmost DFS
● Idea: Use depth-first search.
● Advantages:
● Lower memory usage: Only considers one branch at a time.
● High performance: On many grammars, runs very quickly.
● Easy to implement: Can be written as a set of mutually recursive functions.
Monday, April 29, 13
![Page 121: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/121.jpg)
Leftmost DFS
int + int
E → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 122: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/122.jpg)
Leftmost DFS
int + int
E
E → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 123: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/123.jpg)
Leftmost DFS
int + int
E
TE → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 124: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/124.jpg)
Leftmost DFS
int + int
E
T
intE → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 125: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/125.jpg)
Leftmost DFS
int + int
E
T
intE → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 126: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/126.jpg)
Leftmost DFS
int + int
E
TE → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 127: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/127.jpg)
Leftmost DFS
int + int
E
T
(E)E → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 128: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/128.jpg)
Leftmost DFS
int + int
E
TE → TE → T + ET → intT → (E)
Monday, April 29, 13
![Page 129: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/129.jpg)
• ...
Monday, April 29, 13
![Page 130: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/130.jpg)
Problems with Leftmost DFS
A → Aa | c
c
A
Aa
Aaa
Aaaa
Aaaaa
Monday, April 29, 13
![Page 131: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/131.jpg)
Left Recursion
● A nonterminal A is said to be left-recursive iff
A ⇒* Aω
for some string ω.
● Leftmost DFS may fail on left-recursive grammars.
● Fortunately, in many cases it is possible to eliminate left recursion (see Handout 08 for details).
Saturday, April 13, 13
Monday, April 29, 13
![Page 132: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/132.jpg)
Left Recursion
● A nonterminal A is said to be left-recursive iff
A ⇒* Aω
for some string ω.
● Leftmost DFS may fail on left-recursive grammars.
● Fortunately, in many cases it is possible to eliminate left recursion (see Handout 08 for details).
Saturday, April 13, 13
• need to know what left recursion is
• and why it causes problems for leftmost DFS
Monday, April 29, 13
![Page 133: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/133.jpg)
Eliminating Left Recursion
● In general, left recursion can be converted into right recursion by a mechanical transformation.
● Consider the grammar
A → Aω | α
● This will produce α followed by some number of ω's.
● Can rewrite the grammar as
A → αB
B → ε | ωB
Monday, April 29, 13
![Page 134: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/134.jpg)
Summary of Leftmost BFS/DFS
● Leftmost BFS works on all grammars.
● Worst-case runtime is exponential.
● Worst-case memory usage is exponential.
● Rarely used in practice.
● Leftmost DFS works on grammars without left recursion.
● Worst-case runtime is exponential.
● Worst-case memory usage is linear.
● Often used in a limited form as recursive descent.
Monday, April 29, 13
![Page 135: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/135.jpg)
Predictive Parsing
● The leftmost DFS/BFS algorithms are backtracking algorithms.
● Guess which production to use, then back up if it doesn't work.
● Try to match a prefix by sheer dumb luck.
● There is another class of parsing algorithms called predictive algorithms.
● Based on remaining input, predict (without backtracking) which production to use.
Monday, April 29, 13
![Page 136: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/136.jpg)
A Simple Predictive Parser: LL(1)
● Top-down, predictive parsing:
● L: Left-to-right scan of the tokens
● L: Leftmost derivation.
● (1): One token of lookahead
● Construct a leftmost derivation for the sequence of tokens.
● When expanding a nonterminal, we predict the production to use by looking at the next token of the input. The decision is forced.
Monday, April 29, 13
![Page 137: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/137.jpg)
A Simple Predictive Parser: LL(1)
● Top-down, predictive parsing:
● L: Left-to-right scan of the tokens
● L: Leftmost derivation.
● (1): One token of lookahead
● Construct a leftmost derivation for the sequence of tokens.
● When expanding a nonterminal, we predict the production to use by looking at the next token of the input. The decision is forced.
Monday, April 29, 13
![Page 138: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/138.jpg)
• LL (1)
• how does it work
• how to construct the parsing table for LL(1)
• limitations
Monday, April 29, 13
![Page 139: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/139.jpg)
LL(1) Parse Tables
Monday, April 29, 13
![Page 140: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/140.jpg)
LL(1) Parse Tables
E → intE → (E Op E)Op → +Op → *
Monday, April 29, 13
![Page 141: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/141.jpg)
LL(1) Parse Tables
E → intE → (E Op E)Op → +Op → *
int ( ) + *
E
Op
int (E Op E)
*+
Monday, April 29, 13
![Page 142: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/142.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))
LL(1) Parsing
Monday, April 29, 13
![Page 143: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/143.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))E
LL(1) Parsing
Monday, April 29, 13
![Page 144: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/144.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))E
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 145: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/145.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$E$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 146: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/146.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$E$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
The $ symbol is the end-of-input
marker and is used by the parser
to detect when we have reached
the end of the input. It is not
a part of the grammar.
The $ symbol is the end-of-input
marker and is used by the parser
to detect when we have reached
the end of the input. It is not
a part of the grammar.
Monday, April 29, 13
![Page 147: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/147.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$E$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 148: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/148.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$E$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
The first symbol of our guess is a
nonterminal. We then look at our
parsing table to see what
production to use.
This is called a predict step.
The first symbol of our guess is a
nonterminal. We then look at our
parsing table to see what
production to use.
This is called a predict step.
Monday, April 29, 13
![Page 149: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/149.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$E$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
The first symbol of our guess is a
nonterminal. We then look at our
parsing table to see what
production to use.
This is called a predict step.
The first symbol of our guess is a
nonterminal. We then look at our
parsing table to see what
production to use.
This is called a predict step.
Monday, April 29, 13
![Page 150: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/150.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$E$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 151: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/151.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
E$
(E Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 152: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/152.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
E$
(E Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
The first symbol of our guess is
now a terminal symbol. We thus
match it against the first symbol
of the string to parse.
This is called a match step.
The first symbol of our guess is
now a terminal symbol. We thus
match it against the first symbol
of the string to parse.
This is called a match step.
Monday, April 29, 13
![Page 153: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/153.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
int + (int * int))$
E$
(E Op E)$
E Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 154: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/154.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
int + (int * int))$
E$
(E Op E)$
E Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 155: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/155.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
int + (int * int))$
int + (int * int))$
E$
(E Op E)$
E Op E)$
int Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 156: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/156.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
int + (int * int))$
int + (int * int))$
+ (int * int))$
E$
(E Op E)$
E Op E)$
int Op E)$
Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 157: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/157.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
int + (int * int))$
int + (int * int))$
+ (int * int))$
E$
(E Op E)$
E Op E)$
int Op E)$
Op E)$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 158: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/158.jpg)
• ......
Monday, April 29, 13
![Page 159: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/159.jpg)
(1) E → int(2) E → (E Op E)(3) Op → +(4) Op → *
(int + (int * int))$
(int + (int * int))$
int + (int * int))$
int + (int * int))$
+ (int * int))$
+ (int * int))$
(int * int))$
(int * int))$
int * int))$
int * int))$int * int))$
* int))$
* int))$
int))$
int))$
))$
)$
$
E$
(E Op E)$
E Op E)$
int Op E)$
Op E)$
+ E)$
E)$
(E Op E))$
E Op E))$
int Op E))$
Op E))$
* E))$
E))$int))$
))$
)$
$
int ( ) + *
E
Op
1 2
43
LL(1) Parsing
Monday, April 29, 13
![Page 160: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/160.jpg)
• Need to know how LL(1) works
• How to construct the parsing table for LL(1) ( language that does not have epsilon transition)
• Limitations of LL(1)
Monday, April 29, 13
![Page 161: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/161.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=
1 3 3 3 3 3 3
5 6 7 8 4 4
9 10
→ ;while
2
do
Monday, April 29, 13
![Page 162: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/162.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=→ ;while do
Monday, April 29, 13
![Page 163: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/163.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=→ ;while do
Monday, April 29, 13
![Page 164: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/164.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=
9 10
→ ;while do
Monday, April 29, 13
![Page 165: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/165.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=
9 10
→ ;while do
Monday, April 29, 13
![Page 166: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/166.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=
5 6 7 8
9 10
→ ;while do
Monday, April 29, 13
![Page 167: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/167.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=
5 6 7 8
9 10
→ ;while do
Monday, April 29, 13
![Page 168: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/168.jpg)
STMT → if EXPR then STMT (1)
| while EXPR do STMT (2)
| EXPR ; (3)
EXPR → TERM -> id (4)
| zero? TERM (5)
| not EXPR (6)
| ++ id (7)
| -- id (8)
TERM → id (9)
| constant (10)
Constructing LL(1) Parse Tables
STMT
EXPR
TERM
if then while zero? not ++ -- id const=
5 6 7 8
9 10
→ ;while do
Monday, April 29, 13
![Page 169: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/169.jpg)
• ...
Monday, April 29, 13
![Page 170: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/170.jpg)
Semantic Analysis
• Goal
• Scope
• static scope, dynamic scope
• symbol table
• Type checking
• calculate the type of expressions and statements
• function overloading/overriding
Monday, April 29, 13
![Page 171: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/171.jpg)
Why$Separate$Seman-c$Analysis?$• Parsing$cannot$catch$some$errors$• Why?$
• Some$language$constructs$are$not$context9free$– Example:$All$used$variables$must$have$been$declared$(scoping)$
– Example:$A$method$must$be$invoked$with$arguments$of$proper$type$(typing)$
Monday, April 22, 13Monday, April 29, 13
![Page 172: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/172.jpg)
What%Does%Seman-c%Analysis%Do?%• Checks%of%many%kinds:%
1. All%iden-fiers%are%declared%2. Types%%3. Inheritance%rela-onships%4. Classes%defined%only%once%5. Methods%in%a%class%defined%only%once%6. Reserved%iden-fiers%are%not%misused%And%others%.%.%.%
• The%requirements%depend%on%the%language%
Monday, April 22, 13
Need to be able to identify semantic errors in sample code
Monday, April 29, 13
![Page 173: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/173.jpg)
Scoping:)General)Rules)• The)scope)rules)of)a)language:)
– Determine)which)declara:on)of)a)named)object)corresponds)to)each)use)of)the)object)
– Scoping)rules)map)uses)of)objects)to)their)declara:ons)
• C++)and)Java)use)sta$c&scoping:)– Mapping)from)uses)to)declara:ons)at)compile):me)– C++)uses)the)"most)closely)nested")rule)
• a)use)of)variable)x)matches)the)declara:on)in)the)most)closely)enclosing)scope))
• such)that)the)declara:on)precedes)the)use)
Monday, April 22, 13Monday, April 29, 13
![Page 174: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/174.jpg)
Dynamic(Scoping(Revisited(• Each(func8on(has(an(environment(of(defini8ons(• If(a(name(that(occurs(in(a(func8on(is(not(found(in(its(environment,(its(caller�s(environment(is(searched(
• And(if(not(found(there,(the(search(con8nues(back(through(the(chain(of(callers((
• Now,(with(that(cleared(up…(– Assuming(that(dynamic(scoping(is(used,(what(is(output(by(the(
following(program?((
• void main() { int x = 0; f1(); g(); f2(); } • void f1() { int x = 10; g(); } • void f2() { int x = 20; f1(); g(); } • void g() { print(x); }
Monday, April 22, 13
“most recently called still active” Monday, April 29, 13
![Page 175: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/175.jpg)
Dynamic(Scoping(Revisited(• Each(func8on(has(an(environment(of(defini8ons(• If(a(name(that(occurs(in(a(func8on(is(not(found(in(its(environment,(its(caller�s(environment(is(searched(
• And(if(not(found(there,(the(search(con8nues(back(through(the(chain(of(callers((
• Now,(with(that(cleared(up…(– Assuming(that(dynamic(scoping(is(used,(what(is(output(by(the(
following(program?((
• void main() { int x = 0; f1(); g(); f2(); } • void f1() { int x = 10; g(); } • void f2() { int x = 20; f1(); g(); } • void g() { print(x); }
Monday, April 22, 13
“most recently called still active”
Need to be able to write the output for
sample programs
Monday, April 29, 13
![Page 176: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/176.jpg)
Symbol Tables: The Intuition
0: int x = 137;
1: int z = 42;
2: int MyFunction(int x, int y) {
3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: {
5: int x, z;
6: z@5 = y@2; 7: x@5 = z@5; 8: {
9: int y = x@5;10: {
11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }
13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }
15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }
Symbol Table
x 0
z 1
x 2
y 2
x 5
z 5
Monday, April 29, 13
![Page 177: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/177.jpg)
Symbol Tables: The Intuition
0: int x = 137;
1: int z = 42;
2: int MyFunction(int x, int y) {
3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: {
5: int x, z;
6: z@5 = y@2; 7: x@5 = z@5; 8: {
9: int y = x@5;10: {
11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }
13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }
15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }
Symbol Table
x 0
z 1
x 2
y 2
x 5
z 5
Need to be able to construct the symbol table specifying the decls in each scope, and
the current scopeMonday, April 29, 13
![Page 178: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/178.jpg)
Scoping with Inheritance
public class Base {
public int publicBaseInt = 1;
protected int baseInt = 2;
}
public class Derived extends Base {
public int derivedInt = 3;
public int publicBaseInt = 4;
public void doSomething() {
System.out.println(publicBaseInt);
System.out.println(baseInt);
System.out.println(derivedInt);
int publicBaseInt = 6;
System.out.println(publicBaseInt);
}
}
Root Scope
Base
publicBaseInt 1
baseInt 2
Derived
derivedInt 3
publicBaseInt 4
> 4
2
3
6
doSomething
publicBaseInt 6
Monday, April 29, 13
![Page 179: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/179.jpg)
Scoping with Inheritance
public class Base {
public int publicBaseInt = 1;
protected int baseInt = 2;
}
public class Derived extends Base {
public int derivedInt = 3;
public int publicBaseInt = 4;
public void doSomething() {
System.out.println(publicBaseInt);
System.out.println(baseInt);
System.out.println(derivedInt);
int publicBaseInt = 6;
System.out.println(publicBaseInt);
}
}
Root Scope
Base
publicBaseInt 1
baseInt 2
Derived
derivedInt 3
publicBaseInt 4
> 4
2
3
6
doSomething
publicBaseInt 6
Same for scoping with inheritance
Monday, April 29, 13
![Page 180: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/180.jpg)
• Type inference
• infer the type for expression, statement,
• check “well-formed”
Monday, April 29, 13
![Page 181: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/181.jpg)
• Function overloading/overriding
Monday, April 29, 13
![Page 182: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/182.jpg)
Overloading with Inheritancevoid Function();
void Function(int x);
void Function(double x);
void Function(Base b);
void Function(Derived d);
Function(new Derived);
() (int x) (double x) (Base b) (Derived d)
Monday, April 29, 13
![Page 183: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/183.jpg)
Overloading with Inheritancevoid Function();
void Function(int x);
void Function(double x);
void Function(Base b);
void Function(Derived d);
Function(new Derived);
() (int x) (double x) (Base b) (Derived d)
Need to know how overloading decides what function to call (best
match, hierarchical function overloads for ambiguity, etc)
Monday, April 29, 13
![Page 184: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/184.jpg)
Relaxing our Restrictions
class Base {
Base clone() {
return new Base;
}
}
class Derived extends Base {
Derived clone() {
return new Derived;
}
}Is this safe?
Monday, April 29, 13
![Page 185: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/185.jpg)
Is this safe?
Relaxing our Restrictions (Again)
class Base {
bool equalTo(Base B) {
/* … */
}
}
class Derived extends Base {
bool equalTo(Derived D) {
/* … */
}
}
Monday, April 29, 13
![Page 186: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/186.jpg)
A Concrete Exampleclass Fine {
void nothingFancy(Fine f) {
/* … do nothing … */
}
}
class Borken extends Fine {
int missingFn() {
return 137;
}
void nothingFancy(Borken b) {
Print(b.missingFn());
}
}
int main() {
Fine f = new Borken;
f.nothingFancy(new Fine);
}
(That calls this
one)
Monday, April 29, 13
![Page 187: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/187.jpg)
Monday, April 29, 13
![Page 188: Midterm Review€¦ · The Structure of a Modern Compiler Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code](https://reader033.fdocuments.in/reader033/viewer/2022050107/5f44e192c84a0374f3022b48/html5/thumbnails/188.jpg)
• Good luck!
Monday, April 29, 13