Home Page Title Page CS432F/CSL 728: Compiler Design July 2004 JJ II J I Page 1 of 100 S. Arun-Kumar sak@cse.iitd.ernet.in Department of Computer Science and Engineering I. I. T. Delhi, Hauz Khas, New Delhi 110 016. Go Back Full Screen Close July 30, 2004 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Contents Home Page • first The Bigpicture last Title Page • first Lexical Analysis last JJ II J I • first Syntax Analysis last – – – – Context-free Grammars last first Ambiguity last first Shift-Reduce Parsing last first Parse Trees last first Page 2 of 100 Go Back • first Semantic Analysis last Full Screen • first Synthesized Attributes last • first Inherited Attributes last Close Contd . . . Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Contents . . . Contd Title Page JJ II J I • first Abstract Syntax Trees last • first Symbol Tables last • first Intermediate Representation last – IR: Properties – Typical Instruction Set – IR: Generation Page 3 of 100 Go Back Full Screen • first Runtime Structure last Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 4 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 1 stream of characters SCANNER stream of tokens Home Page Title Page JJ II J I Page 5 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 2 stream of characters SCANNER stream of tokens Home Page Title Page JJ II J I PARSER parse tree Page 6 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 3 stream of characters SCANNER stream of tokens Home Page Title Page JJ II J I PARSER parse tree SEMANTIC ANALYZER abstract syntax tree Page 7 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 4 stream of characters SCANNER stream of tokens Home Page Title Page JJ II J I PARSER parse tree SEMANTIC ANALYZER abstract syntax tree Page 8 of 100 I.R. CODE GENERATOR intermediate representation Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 5 stream of characters SCANNER stream of tokens Home Page Title Page JJ II J I PARSER parse tree SEMANTIC ANALYZER abstract syntax tree Page 9 of 100 I.R. CODE GENERATOR intermediate representation Go Back OPTIMIZER optimized intermediate representation Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 6 stream of characters SCANNER stream of tokens Home Page Title Page JJ II J I PARSER parse tree SEMANTIC ANALYZER abstract syntax tree Page 10 of 100 I.R. CODE GENERATOR intermediate representation Go Back OPTIMIZER optimized intermediate representation CODE GENERATOR Full Screen Close target code Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 7 stream of characters SCANNER Home Page Title Page stream of tokens JJ II J I PARSER parse tree ERROR− HANDLER SEMANTIC ANALYZER abstract syntax tree SYMBOL TABLE Page 11 of 100 MANAGER I.R. CODE GENERATOR intermediate representation Go Back OPTIMIZER optimized intermediate representation CODE GENERATOR Full Screen Close target code Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit The Big Picture: 7 Home Page Title Page stream of characters SCANNER JJ II J I stream of tokens PARSER parse tree ERROR− HANDLER SEMANTIC ANALYZER abstract syntax tree SYMBOL TABLE MANAGER Page 12 of 100 I.R. CODE GENERATOR intermediate representation OPTIMIZER optimized intermediate representation Go Back CODE GENERATOR target code Full Screen Scanner Parser Semantic Analysis Symbol Table IR Optimization Contents Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 13 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Scanning: 1 Title Page JJ II J I • Takes a stream of characters and identifies tokens from the lexemes. Page 14 of 100 • Eliminates comments and redundant whitepace. Go Back • Keeps track of line numbers and column numbers and passes them as parameters to the other phases to enable error-reporting to the user. Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Scanning: 2 Title Page • Whitespace: JJ II • Lexeme: A sequence of non-whitespace characters de- J I A sequence of space, tab, newline, carriage-return, form-feed characters etc. limited by whitespace or special characters (e.g. operators like +, -, *). Page 15 of 100 • Examples of lexemes. Go Back – reserved words, keywords, identifiers etc. – Each comment is usually a single lexeme – preprocessor directives Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Scanning: 3 Home Page Title Page • Token: A sequence of characters to be treated as a single unit. JJ II J I • Examples of tokens. Reserved words (e.g. begin, end, struct, if etc.) Keywords (integer, true etc.) Operators (+, &&, ++ etc) Identifiers (variable names, procedure names, parameter names) – Literal constants (numeric, string, character constants etc.) – Punctuation marks (:, , etc.) – – – – Page 16 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Scanning: 4 • Identification of tokens is usually done by a Deterministic Finite-state automaton (DFA). Title Page JJ II J I • The set of tokens of a language is represented by a large regular expression. Page 17 of 100 • This regular expression is fed to a lexical-analyser generator such as Lex, Flex or ML-Lex. Go Back • A giant DFA is created by the Lexical analyser generator. Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Scanning: 5 Home Page Title Page a−e,g−z identifier if f 2 3 identifier 0−9,a−z 4 h, j− a− other chars white space 0−9 5 0−9 1 14 JJ II J I 0−9 z i 0−9,a−z 6 real 0−9 Page 18 of 100 0−9 Go Back 8 7 int any char ( 13 9 error error * 10 real Full Screen * 11 ) 12 comment Close Quit The Big Picture •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Syntax Analysis Home Page Consider the following two languages over an alphabet A = {a, b}. Title Page R = {anbn|n < 100} P = {anbn|n > 0} • R may be finitely represented by a regular expression JJ II J I (even though the actual expression is very long). • However, P cannot actually be represented by a regular Page 19 of 100 expression Go Back • A regular expression is not powerful enough to represent languages which require parenthesis matching to arbitrary depths. • All high level programming languages require an underlying language of expressions which require parentheses to be nested and matched to arbitrary depth. Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit CF-Grammars: Definition A context-free grammar (CFG) G = hN, T , P, Si consists of • a set N of nonterminal symbols, Home Page Title Page JJ II J I • a set T of terminal symbols or the alphabet, • a set P of productions or rewrite rules, • each production is of the form X −→ α, where – X ∈ N is a nonterminal and – α ∈ (N ∪ T )∗ is a string of terminals and nonterminals • a start symbol S ∈ N . Page 20 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit CFG: Example G = h{S}, {a, b}, P, Si, where S −→ ab and S −→ aSb are the only productions in P . Derivations look like this: Home Page Title Page JJ II J I S ⇒ ab Page 21 of 100 S ⇒ aSb ⇒ aabb S ⇒ aSb ⇒ aaSbb ⇒ aaabbb Go Back L(G), the language generated by G is {anbn|n > 0}. Full Screen Actually can be proved by induction on the length and structure of derivations. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit CFG: Empty word Home Page G = h{S}, {a, b}, P, Si, where S −→ SS | aSb | ε generates all sequences of matching nested parentheses, including the empty word ε. Title Page JJ II J I A leftmost derivation might look like this: S ⇒ SS ⇒ SSS ⇒ SS ⇒ aSbS ⇒ abS ⇒ abaSb . . . Page 22 of 100 A rightmost derivation might look like this: S ⇒ SS ⇒ SSS ⇒ SS ⇒ SaSb ⇒ Sab ⇒ aSbab . . . Other derivations might look like God alone knows what! S ⇒ SS ⇒ SSS ⇒ SS ⇒ . . . Go Back Full Screen Close Could be quite confusing! Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page CFG: Derivation trees 1 Derivation sequences • put an artificial order in which productions are fired. Title Page JJ II J I • instead look at trees of derivations in which we may think of productions as being fired in parallel. Page 23 of 100 • There is then no highlighting in red to determine which copy of a nonterminal was used to get the next member of the sequence. Go Back Full Screen • Of course, generation of the empty word ε must be shown explicitly in the tree. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit CFG: Derivation trees 2 Home Page Title Page S S S ε εε S b II J I S S a JJ a a S S Page 24 of 100 Go Back b b Derivation tree of abaabb Full Screen Close ε Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit CFG: Derivation trees 3 Home Page Title Page S S S a S b S S a S b ε a S b JJ II J I Page 25 of 100 Go Back Full Screen ε Another Derivation tree of ε Close abaabb Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit CFG: Derivation trees 4 Home Page Title Page S S S ε S S a S b a S b JJ II J I Page 26 of 100 Go Back a S b Full Screen ε Yet another Derivation tree of abaabb ε Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 27 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Ambiguity: 1 Home Page E → I | C | E+E | E∗E I → y | z C → 4 Title Page Consider the sentence y + 4 ∗ z. E JJ II J I E Page 28 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Ambiguity: 2 Home Page E → I | C | E+E | E∗E I → y | z C → 4 Title Page Consider the sentence y + 4 ∗ z. E JJ II J I E Page 29 of 100 E + E E * E Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Ambiguity: 3 Home Page E → I | C | E+E | E∗E I → y | z C → 4 Title Page Consider the sentence y + 4 ∗ z. E JJ II J I E Page 30 of 100 E E + * I E E E E * + E E Go Back I Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Ambiguity: 4 Home Page E → I | C | E+E | E∗E I → y | z C → 4 Title Page Consider the sentence y + 4 ∗ z. E JJ II J I E Page 31 of 100 E E + * I E E E * + E E Go Back E I Full Screen I y C I C z Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Ambiguity: 5 Home Page E → I | C | E+E | E∗E I → y | z C → 4 Title Page Consider the sentence y + 4 ∗ z. E JJ II J I E Page 32 of 100 E E + * I E E E * + E E Go Back E I Full Screen I y I C C z z 4 y Close 4 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 33 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 0 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | ( E ) a − a / JJ II J I b Page 34 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 1 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | ( E ) − a / JJ II J I b Page 35 of 100 Principle: Go Back Reduce whenever possible. Shift only when reduce is impossible Full Screen Close a Shift Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 2 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | ( E ) − a / JJ II J I b Page 36 of 100 Go Back Full Screen Close D Reduce by r5 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 3 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | ( E ) − a / JJ II J I b Page 37 of 100 Go Back Full Screen Close T Reduce by r4 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 4 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | ( E ) − a / JJ II J I b Page 38 of 100 Go Back Full Screen Close E Reduce by r2 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 5 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) a / b Page 39 of 100 Go Back Full Screen Close − E Shift Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 6 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 40 of 100 Go Back Full Screen a Shift Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 7 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 41 of 100 Go Back Full Screen D Reduce by r5 Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 8 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 42 of 100 Go Back Full Screen T Reduce by r4 Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 8a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 43 of 100 Go Back Full Screen T − E Reduce by r4 Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 9a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 44 of 100 Go Back Full Screen Close E Reduce by r1 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 10a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) b Page 45 of 100 Go Back Full Screen Close / E Shift Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 11a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 46 of 100 Go Back Full Screen b Shift Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 12a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 47 of 100 Go Back Full Screen D Reduce by r5 Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 13a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 48 of 100 Go Back Full Screen T Reduce by r4 Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 14a Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 49 of 100 k! Go Back St uc Full Screen E Get back! Reduce by r2 Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 14b Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 50 of 100 Go Back Full Screen E Get back! Reduce by r2 Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 13b Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 51 of 100 Go Back Full Screen T Get back! Reduce by r4 Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 12b Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 52 of 100 Go Back Full Screen D Get back! Reduce by r5 Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 11b Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 53 of 100 Go Back Full Screen Get back! b Shift Close / E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 10b Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) b Page 54 of 100 Go Back Full Screen Close Get back! / E Shift Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 9b Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 55 of 100 Go Back Full Screen Get back to where you once belonged! Close E Reduce by r1 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 8b r1. E E − r2 E T r3 T T / r4 T D r5 D a fied i d o m Principle: T Reduce whenever possible, but but depending upon D Home Page Title Page JJ II J I lookahead | b | ( E ) / b Page 56 of 100 Shift instead of reduce here! Go Back ce redu − t f i Sh ict confl Full Screen T − E Reduce by r4 Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 8 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) / b Page 57 of 100 Go Back Full Screen T Reduce by r4 Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 9 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) b Page 58 of 100 Go Back Full Screen Shift / T Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 10 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 59 of 100 Go Back b Shift Full Screen / T Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 11 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 60 of 100 Go Back D / Reduce by r5 Full Screen T Close − E Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 12 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 61 of 100 Go Back Full Screen T − E Reduce by r3 Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: 13 Home Page r1. E E − r2 E T r3 T T / r4 T D r5 D a T Title Page D | b | JJ II J I ( E ) Page 62 of 100 Go Back Full Screen Close E Reduce by r1 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 0 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 63 of 100 Go Back Full Screen Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 1 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 64 of 100 Go Back Full Screen D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 2 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 65 of 100 Go Back T Full Screen D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 3 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 66 of 100 E Go Back T Full Screen D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 3a r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 67 of 100 E Go Back T Full Screen D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 3b r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 68 of 100 E Go Back T Full Screen D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 4 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 69 of 100 E Go Back T Full Screen D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 5 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 70 of 100 E Go Back T T Full Screen D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 5a r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 71 of 100 E Go Back T T Full Screen D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 5b r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 72 of 100 E Go Back T T Full Screen D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 6 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 73 of 100 E Go Back T T Full Screen D D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 7 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) JJ II J I Page 74 of 100 E T Go Back T T Full Screen D D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parse Trees: 8 r1. E E − r2 E T r3 T T / Home Page Title Page T r4 T r5 D D D a | b | ( E ) E JJ II J I Page 75 of 100 E T Go Back T T Full Screen D D D Close a − a / b Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Parsing: Summary: 1 Title Page • All high-level languages are designed so that they may JJ II be parsed in this fashion with only a single token lookahead. J I • Parsers for a language can be automatically constructed by parger-generators such as Yacc, Bison, MLYacc. • Shift-reduce conflicts if any, are automatically detected Page 76 of 100 Go Back and reported by the parser-generator. • Shift-reduce conflicts may be avoided by suitably Full Screen redesigning the context-free grammar. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Parsing: Summary: 2 Home Page Title Page • Very often shift-reduce conflicts may occur because of the prefix problem. In such cases many parsergenerators resolve the conflict in favour of shifting. • There is also a possiblility of reduce-reduce conflicts. This usually happens when there is more than one nonterminal symbol to which the contents of the stack may reduce. JJ II J I Page 77 of 100 Go Back • A minor reworking of the grammar to avoid redundant non-terminal symbols will get rid of reduce-reduce conflicts. The Big Picture Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Semantic Analysis: 1 • Every Programming langauge can be used to program any computable function, assuming of course, it has – unbounded memory, and – unbounded time • The parser of a programming language provides the framework within which the target code is to be generated. • The parser also provides a structuring mechanism that divides the task of code generation into bits and pieces determined by the individual nonterminals and production rules. • However, contex-free grammars are not powerful enough to represent all computable functions. Example, the language {an bn cn |n > 0}. Home Page Title Page JJ II J I Page 78 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Semantic Analysis: 2 Title Page JJ II J I • There are context-sensitive aspects of a program that cannot be represented/enforced by a context-free grammar definition. Examples include – correspondence between formal and actual parameters – type consistency between declaration and use. – scope and visibility issues with respect to identifiers in a program. Page 79 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 80 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 0 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 81 of 100 n F Go Back ( ) E E T Full Screen − T F Close F n Quit n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 1 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 82 of 100 n F Go Back ( ) E E T − Synthesized Attributes 4 T F 3 2 Full Screen 1 Close F n Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 2 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 83 of 100 n F Go Back ( ) E E T − Synthesized Attributes 4 T 4 F 3 2 Full Screen 1 Close F n Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 3 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 84 of 100 n F Go Back ( ) E E T − Full Screen Synthesized Attributes 4 4 4 T F 3 2 1 Close F n Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 4 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 85 of 100 n F Go Back ( ) E 4 E T − Synthesized Attributes 4 4 4 T F 3 2 Full Screen 1 Close F n Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 5 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 86 of 100 n F Go Back ( ) E 4 E Full Screen T Synthesized Attributes − 4 4 4 T F 3 2 1 Close F n 1 Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 6 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 87 of 100 n F Go Back ( ) E 4 E Full Screen T Synthesized Attributes − 4 4 4 T F 1 n 1 3 2 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 7 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 88 of 100 n F Go Back ( ) E 4 E T − 1 Synthesized Attributes 4 4 4 T F 1 n 1 3 2 Full Screen 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 8 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 89 of 100 n F Go Back ( E 4 E 3 ) T − 1 Full Screen Synthesized Attributes 4 4 4 T F 1 n 1 3 2 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 9 Home Page Title Page E T F / ( ) E JJ II J I n T − T F / Page 90 of 100 F Go Back ( E 4 n 3 E 3 ) T − 1 Full Screen Synthesized Attributes 4 4 4 T F 1 n 1 3 2 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 10 Home Page Title Page E T F / ( ) E JJ II J I n T − T 3 F / Page 91 of 100 F Go Back ( E 4 n 3 E 3 ) T − 1 Full Screen Synthesized Attributes 4 4 4 T F 1 n 1 3 2 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 11 Home Page Title Page E T F / ( ) E JJ II J I n T − T 3 F / Page 92 of 100 F 2 Go Back ( E 4 n 3 E 3 ) T − 1 Synthesized Attributes 4 4 4 T F 1 n 1 3 2 Full Screen 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 12 Home Page Title Page E T F / ( ) E JJ II J I n T − T 3 F 2 n 2 / Page 93 of 100 F 3 Go Back ( E 4 E 3 ) T − 1 Synthesized Attributes 4 4 4 T F 1 n 1 3 2 Full Screen 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 13 Home Page Title Page E T F / ( ) E JJ II J I n 1 T − T 3 F 2 n 2 / Page 94 of 100 F 3 Go Back ( E 4 E 3 ) T − 1 Full Screen Synthesized Attributes 4 4 4 T F 1 n 1 3 2 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Synthesized Attributes: 14 Home Page Title Page E T F / ( ) E 1 T 1 JJ II J I n − T 3 F 2 n 2 / Page 95 of 100 F 3 Go Back ( E 4 E 3 ) T − 1 Synthesized Attributes 4 4 4 T F 1 n 1 3 2 Full Screen 1 Close F Quit 4 n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit An Attribute Grammar Home Page Title Page T E 1 T 1 E0 → E1−T B E0.val := sub(E1.val, T.val) E → T 3 F 2 3 n 2 T0 → T1/F II J I B E.val := T.val / F JJ B T0.val := div(T1.val, F.val) Page 96 of 100 ( E 4 E 3 ) T − 4 4 4 T 1 F 1 n 1 T → F B T.val := F.val Go Back F → (E) B F.val := E.val F n F → n B F.val := n.val Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 0 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D T Title Page JJ II J I L I , int L Page 97 of 100 z I L Go Back , y Full Screen I D L x y T z I int Close x Quit , •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 1 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D T Title Page JJ II J I L I int , int L Page 98 of 100 z I L Go Back , y Full Screen I D L x y T z I int Close x Quit , int •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 2 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D int T Title Page JJ II J I L I int , int L Page 99 of 100 z I L Go Back , y Full Screen I D L x y T z I int Close x Quit , int int •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 3 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D int T L Title Page JJ II J I int I int , int L Page 100 of 100 z I L Go Back , y Full Screen I D L x y T z I int Close x Quit , int int •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 4 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D int T L JJ II J I int I int Title Page int , int L int Page 101 of 100 z I L Go Back , y Full Screen I D L x y T z I int Close x Quit , int int •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 5 Home Page D Title Page int T L int I int JJ II J I int , int L int z I L int int int Page 102 of 100 , y Go Back I D L T I Full Screen x y z int x Close , int int Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 6 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D int T L L L I x y z I int I Page 103 of 100 int int int Go Back , y T J int int I L II , int z D JJ int I int Title Page int Full Screen int Close x Quit , int int •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Inherited Attributes: 7 C -style declarations generating int x, y, z. D → TL L → L,I | I Home Page T → int | float I → x | y | z D int T L L L I x y z int int int Go Back , int Full Screen int I int I Page 104 of 100 y T J int int I L II , int z D JJ int I int Title Page Close x int Quit , int int •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit An Attribute Grammar Home Page Title Page D int D → TL T L I int int , int L I int I int T → int T → float B T.type := float.f loat B T.type := int.int int z L B L.in := T.type JJ II J I int int , int Page 105 of 100 y int L0 → L1,I B L1 := L0.in Go Back x int L → I B I.in := L.in Full Screen I → id B id.type := I.in Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 106 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Abstract Syntax: 0 Home Page E → E−T | T T → T /F | F F → n | (E) Title Page Suppose we want to evaluate an expression (4 − 1)/2. What we actually want is a tree that looks like this: JJ II J I Page 107 of 100 / Go Back −− 2 Full Screen Close 4 1 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Evaluation: 0 Home Page Title Page JJ II J I / Page 108 of 100 −− 2 Go Back Full Screen 4 1 Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Evaluation: 1 Home Page Title Page JJ II J I / Page 109 of 100 −− 2 Go Back Full Screen 4 1 Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Evaluation: 2 Home Page Title Page JJ II J I / Page 110 of 100 3 2 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Evaluation: 3 Home Page Title Page JJ II J I / Page 111 of 100 3 2 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Evaluation: 4 Home Page Title Page 1 JJ II J I Page 112 of 100 Go Back Full Screen Close But what we actually get during parsing is a tree that looks like . . . Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Abstract Syntax: 1 . . . THIS! Home Page E Title Page T T F JJ II n J I / F Page 113 of 100 ( ) E / E Go Back T − n −− T Full Screen F n n Close F n Quit n •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Abstract Syntax: 2 Home Page Title Page We use attribute grammar rules to construct the abstract syntax tree (AST)!. But in order to do that we first require two procedures for tree construction. makeLeaf(literal) : Creates a node with label literal and returns a pointer to it. makeBinaryNode(opr, opd1, opd2) : Creates a node with label opr (with fields which point to opd1 and opd2) and returns a pointer to the newly created node. Now we may associate a synthesized attribute called ptr with each terminal and nonterminal symbol which points to the root of the subtree created for it. JJ II J I Page 114 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Abstract Syntax: 3 Home Page Title Page E0 → E1−T B E0.ptr := makeBinaryN ode(−, E1.ptr, T.ptr) E → T T0 → T1/F JJ II J I B E.ptr := T.ptr B T0.ptr := makeBinaryN ode(/, T1.ptr, F.ptr) Page 115 of 100 T → F B T.ptr := F.ptr Go Back F → (E) B F.ptr := E.ptr Full Screen F → n B F.ptr := makeLeaf (n.val) Close The Big Picture Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Symbol Table:1 • The store house of context-sensitive and run-time information about every identifier in the source program. Title Page JJ II J I • All accesses relating to an identifier require to first find the attributes of the identifier from the symbol table Page 116 of 100 • Usually organized as a hash table – provides fast access. Go Back • Compiler-generated temporaries may also be stored in the symbol table Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Symbol Table:2 Title Page Attributes stored in a symbol table for each identifier: • type • size JJ II J I • scope/visibility information Page 117 of 100 • base address • addresses to location of auxiliary symbol tables (in case Go Back of records, procedures, classes) • address of the location containing the string which actually names the identifier and its length in the string pool Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Symbol Table:3 • A symbol table exists through out the compilation and Title Page JJ II J I run-time. • Major operations required of a symbol table: – insertion – search – deletions are purely logical (depending on scope and visibility) and not physical • Keywords are often stored in the symbol table before Page 118 of 100 Go Back Full Screen the compilation process begins. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Symbol Table:4 Accesses to the symbol table at every stage of the compilation process, Home Page Title Page JJ II J I Scanning: Insertion of new identifiers. Parsing: Access to the symbol table to ensure that an operand exists (declaration before use). Page 119 of 100 Semantic analysis: • Determination of types of identifiers from declarations • type checking to ensure that operands are used in type-valid contexts. • Checking scope, visibility violations. Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Symbol Table:5 IR generation: . Memory allocation and relativea address calculation. Title Page JJ II J I Optimization: All memory accesses through symbol table Target code: Translation of relative addresses to absolute addresses in terms of word length, word boundary etc. Page 120 of 100 Go Back The Big picture a i.e.relative to a base address that is known only at run-time Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Intermediate Representation Home Page Title Page Intermediate representations are important for reasons of portability. JJ II • (more or less) independent of specific features of the J I high-level language. Example. Java byte-code for any high-level language. Page 121 of 100 • (more or less) independent of specific features of any particular target architecture (e.g. number of registers, memory size) – number of registers – memory size – word length Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR Properties: 1 1. It is fairly low-level containing instructions common to all target architectures and assembly languages. How low can you stoop? . . . 2. It contains some fairly high-level instructions that are common to most high-level programming languages. How high can you rise? 3. To ensure portability • an unbounded number of variables and memory locations • no commitment to Representational Issues Home Page Title Page JJ II J I Page 122 of 100 Go Back 4. To ensure type-safety Full Screen • memory locations are also typed according to the data they may contain, • no commitment is made regarding word boundaries, and the structure of individual data items. Close Quit Next •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page IR: Representation? • No commitment to word boundaries or byte boundaries • No commitment to representation of – – – – int vs. float, float vs. double, packed vs. unpacked, strings – where and how?. Back to IR Properties:1 Title Page JJ II J I Page 123 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page IR: How low can you stoop? Title Page JJ II J I • most arithmetic and logical operations, load and store instructions etc. • so as to be interpreted easily, Page 124 of 100 • the interpreter is fairly small, • execution speeds are high, Go Back • to have fixed length instructions (where each operand position has a specific meaning). Back to IR Properties:1 Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: How high can you rise? • typed variables, • temporary variables instead of registers. Home Page Title Page JJ II J I • array-indexing, • random access to record fields, • parameter-passing, Page 125 of 100 Go Back • pointers and pointer management • no limits on memory addresses Back to IR Properties:1 Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit A typical instruction set: 1 Three address code: A suite of instructions. Each instruction has at most 3 operands. • an opcode representing an operation with at most 2 Home Page Title Page JJ II J I operands Page 126 of 100 • two operands on which the binary operation is performed Go Back • a target operand, which accumulates the result of the (binary) operation. Full Screen If an operation requires less than 3 operands then one or more of the operands is made null. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page A typical instruction set: 2 • Assignments (LOAD-STORE) • Jumps (conditional and unconditional) Title Page JJ II J I Page 127 of 100 • Procedures and parameters Go Back • Arrays and array-indexing • Pointer Referencing and Dereferencing Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page A typical instruction set: 2 Title Page JJ II J I • Assignments (LOAD-STORE) – x := y bop z, where bop is a binary operation – x := uop y, where uop is a unary operation – x := y, load, store, copy or register transfer • Jumps (conditional and unconditional) Page 128 of 100 Go Back • Procedures and parameters Full Screen • Arrays and array-indexing • Pointer Referencing and Dereferencing Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page A typical instruction set: 2 Title Page JJ II J I • Assignments (LOAD-STORE) • Jumps (conditional and unconditional) – goto L – Unconditional jump, – x relop y goto L – Conditional jump, where relop is a relational operator Page 129 of 100 Go Back • Procedures and parameters • Arrays and array-indexing Full Screen • Pointer Referencing and Dereferencing Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page A typical instruction set: 2 Title Page JJ II J I • Assignments (LOAD-STORE) • Jumps (conditional and unconditional) • Procedures and parameters Page 130 of 100 – call p n, where n is the number of parameters – return y, return value from a procedures call – param x, parameter declaration Go Back Full Screen • Arrays and array-indexing • Pointer Referencing and Dereferencing Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit A typical instruction set: 2 • Assignments (LOAD-STORE) Home Page Title Page JJ II J I • Jumps (conditional and unconditional) • Procedures and parameters • Arrays and array-indexing Page 131 of 100 – x := a[i] – array indexing for r-value – a[j] := y – array indexing for l-value Note: The two opcodes are different depending on whether l-value or r-value is desired. x and y are always simple variables Go Back Full Screen Close • Pointer Referencing and Dereferencing Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit A typical instruction set: 2 Home Page Title Page • Assignments (LOAD-STORE) • Jumps (conditional and unconditional) • Procedures and parameters JJ II J I • Arrays and array-indexing • Pointer Referencing and Dereferencing – x := ˆy – referencing: set x to point to y – x := *y – dereferencing: copy contents of location pointed to by y into x – *x := y – dereferencing: copy r-value of y into the location pointed to by x Page 132 of 100 Go Back Full Screen Close Picture Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Pointers y x *x x x x := ^y *y y @y @z Title Page *y y x y *x Home Page JJ II J I @z *z x := *y Page 133 of 100 *z *z z z y Go Back y *y *y x Full Screen x *x := y @z @z z z *z Close *y Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: Generation Home Page • Can be generated by recursive traversal of the abstract syntax tree. Title Page • Can be generated by syntax-directed translation as follows: For every non-terminal symbol N in the grammar of the source language there exist two attributes N.place , which denotes the address of a temporary variable where the result of the execution of the generated code is stored N.code , which is the actual code segment generated. • In addition a global counter for the instructions gener- JJ II J I Page 134 of 100 Go Back Full Screen ated is maintained as part of the generation process. • It is independent of the source language but can express target machine operations without committing to too much detail. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: Infrastructure Home Page Title Page Given an abstract syntax tree T, with T also denoting its root node. T.place address of temporary variable where result of execution of the T is stored. newtemp returns a fresh variable name and also installs it in the symbol table along with relevant information T.code the actual sequence of instructions generated for the tree T. newlabel returns a label to mark an instruction in the generated code which may be the target of a jump. emit emits an instructions (regarded as a string). JJ II J I Page 135 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page IR: Infrastructure Colour and font coding of IR code generation Title Page JJ II J I • Green: Nodes of the Abstract Syntax Tree • Brown: Characters and strings of the Intermediate Representation • Red: Variables and data structures of the language in which the IR code generator is written Page 136 of 100 Go Back • blue: Names of relevant procedures used in IR code generation. Full Screen • Black: All other stuff. Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: Example Home Page Title Page E → id B E.place := id.place; E.code := emit() JJ II J I Page 137 of 100 E0 → E1 − E2 B Go Back E0.place := newtemp; E0.code := E1.code || E2.code || emit(E0.place := E1.place − E2.place) Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: Example Home Page S → id := E S.code := E.code || emit(id.place:=E.place) S0 B → while E do S1 Title Page JJ II J I B Page 138 of 100 S0.begin := newlabel; S0.af ter := newlabel; S0.code := emit(S0.begin:) || E.code || emit(if E.place= 0 goto S0.af ter) || S1.code || emit(gotoS0.begin) || emit(S0.af ter:) Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: Example Home Page S → id := E S.code := E.code || emit(id.place:=E.place) S0 B → while E do S1 Title Page JJ II J I B Page 139 of 100 S0.begin := newlabel; S0.af ter := newlabel; S0.code := emit(S0.begin:) || E.code || emit(if E.place= 0 goto S0.af ter) || S1.code || emit(gotoS0.begin) || emit(S0.af ter:) Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit IR: Generation Home Page Title Page While generating the intermediate representation, it is sometimes necessary to generate jumps into code that has not been generated as yet (hence the address of the label is unknown). This usually happens while processing JJ II J I • forward jumps • short-circuit evaluation of boolean expressions It is usual in such circumstances to either fill up the empty label entries in a second pass over the the code or through a process of backpatching (which is the maintenance of lists of jumps to the same instruction number), wherein the blank entries are filled in once the sequence number of the target instruction becomes known. Page 140 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit A Calling Chain Main program Home Page Title Page Globals Procedure P2 Locals of P2 JJ II J I Procedure P21 Locals of P21 Body of P21 Call P21 Page 141 of 100 Body of P2 Call P21 Go Back Procedure P1 Locals of P1 Full Screen Body of P1 Call P2 Main body Close Call P1 Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Run-time Structure: 1 Home Page Main program Title Page Globals Procedure P2 JJ II J I Locals of P2 Procedure P21 Locals of P21 Page 142 of 100 Body of P21 Body of P2 Go Back Procedure P1 Full Screen Locals of P1 Body of P1 Close Main body Globals Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Run-time Structure: 2 Home Page Main program Title Page Globals Procedure P2 JJ II J I Locals of P2 Procedure P21 Locals of P21 Page 143 of 100 Body of P21 Body of P2 Procedure P1 Locals of P1 Body of P1 Main body Go Back Return address to Main Dynamic link to Main Locals of P1 Static link to Main Formal par of P1 Full Screen Close Globals Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Run-time Structure: 3 Home Page Main program Title Page Globals Procedure P2 JJ II J I Locals of P2 Procedure P21 Locals of P21 Body of P21 Body of P2 Procedure P1 Locals of P1 Body of P1 Main body Return address to last of P1 Dynamic link to last P1 Locals of P2 Static link to last P1 Formal par P2 Go Back Return address to Main Dynamic link to Main Locals of P1 Static link to Main Formal par of P1 Page 144 of 100 Full Screen Close Globals Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Run-time Structure: 4 Home Page Main program Title Page Globals Procedure P2 Locals of P2 Procedure P21 Locals of P21 Body of P21 Body of P2 Procedure P1 Locals of P1 Body of P1 Main body Return address to last of P2 Dynamic link to last P2 Locals of P21 Static link last P2 Formal par P21 Return address to last of P1 Dynamic link to last P1 Locals of P2 Static link to last P1 Formal par P2 II J I Page 145 of 100 Go Back Return address to Main Dynamic link to Main Locals of P1 Static link to Main Formal par of P1 JJ Full Screen Close Globals Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Run-time Structure: 5 Main program Globals Return address to last of P21 Dynamic link to last P21 Locals of P21 Static link to last P2 Formal par P21 Procedure P2 Locals of P2 Procedure P21 Locals of P21 Body of P21 Body of P2 Procedure P1 Locals of P1 Body of P1 Main body Return address to last of P2 Dynamic link to last P2 Locals of P21 Static link last P2 Formal par P21 Return address to last of P1 Dynamic link to last P1 Locals of P2 Static link to last P1 Formal par P2 Title Page JJ II J I Page 146 of 100 Go Back Return address to Main Dynamic link to Main Locals of P1 Static link to Main Formal par of P1 Home Page Full Screen Close Globals Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit Home Page Title Page JJ II J I Page 147 of 100 Go Back Full Screen Close Quit •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit