【文章內(nèi)容簡介】
matical analysis on a sentence in natural language. ? The result is represented as a parse tree or a syntax tree. Parse Tree expression assignexpression expression expression = subscriptexpression additiveexpression expression expression expression expression [ ] + identifier a identifier index number 4 number 2 Abstract Syntax Tree An abstract syntax tree is a condensation of the information contained in a parse tree. assignexpression subscriptexpression additiveexpression identifier a identifier index number 4 number 2 The Semantic Analyzer ? The semantics of a program are its “meaning”. ? The semantics of a program determine its runtime behavior. ? Most programming languages have features (called static semantics) that can be determined prior to execution. ? Typical static semantics features – Declarations – Type checking ? The extra information puted by the semantic analyzer are called attributes. – They are added to the tree as annotations, or “decorations” Annotated Tree assignexpression subscriptexpression integer additiveexpression integer identifier a array of integer identifier index integer number 4 integer number 2 integer The Source Code Optimizer ? The earliest point at which optimization steps can be performed is just after semantic analysis. ? There may be possibilities that depend only on the source code. ? Compilers exhibit a wide variation in the kind of optimization and its placement. ? The output of the source code optimizer is the intermediate representation (IR) or intermediate code. Example ? 4 + 2 can be preputed by the piler. – This optimization is known as constant folding. – This optimization can be performed on the annotated syntax tree by collapsing the right hand subtree to its constant value. assignexpression subscriptexpression integer number 6 integer identifier a array of integer identifier index integer The Code Generator ? The code generator takes the intermediate code or IR and generates code for the target machine. – We will write target code in assembly language form. Most pilers generate object code directly. ? The properties of the target machine bee important. – Use instructions of the target machine. – Data representations: how many bytes or words integer and floatingpoint data types occupy in memory. Example ? amp。a is the address of a (the base address of the array) ? *R1 means indirect register addressing ? We assumed that the machine performs byte addressing. ? Integers occupy two bytes of memory. MOV R0, index 。 value of index R0 MUL R0, 2 。 double value in R0 MOV R1, amp。a 。 address of a R1 ADD R1, R0 。 add R0 to R1 MOV *R1, 6 。 constant 6 address in R1 The Target Code Optimizer ? Improvements include – Choosing addressing modes to improve performance. – Replacing slow instructions by faster ones. – Eliminating redundant or unnecessary operations ? Example: MOV R0, index 。 value of index R0 SHL R0 。 double the value in R0 MOV amp。a[R0], 6 。 constant