Docsity

Compiler Construction: Explaining Compilation with Analysis and Code Generation, Assignments of Compiler Construction

Partial preview of the text

Download Compiler Construction: Explaining Compilation with Analysis and Code Generation and more Assignments Compiler Construction in PDF only on Docsity!

CSC373-Compiler Construction Week-2 Lecture- Semester-#5 Fall 2019 Prepared by: Eisha Tir Razia Lahore Garrison University

Overview of previous lecture

 (^) Compiler types in general  (^) Analysis  (^) synthesis

Lecture Outcomes

 (^) Understanding related  (^) Compiler phases in detail

Lexical Analysis

 (^) First phase of compiler works as a text scanner  (^) This phase scans the source code as a stream of characters and convert it into a meaningful lexemes  (^) This phase scans the characters in the source program and groups them into a stream of tokens in which each token represents a logically organized sequence of characters such as an identifier, a keyword, a punctuation character.

Example

 (^) Total = Count + Rate * 10  (^) The characters in this assignment could be grouped into the following lexemes and mapped into the following tokens passed on to the syntax analyzer:  (^) Total is a lexeme that would be mapped into a token {id, 1 ), where id is an abstract symbol standing for identifier and 1 points to the symbol table entry for total. The symbol-table entry for an identifier holds information about the identifier, such as its name and type  (^) The assignment symbol = is a lexeme that is mapped into the token {=). Since this token needs no attribute-value, we have omitted the second component. We could have used any abstract symbol such as assign for the token-name, but for notational convenience we have chosen to use the lexeme itself as the name of the abstract symbol

Cont..

 (^) Count is a lexeme that is mapped into the token (id, 2 ) , where 2 points to the symbol-table entry for count  (^) + is a lexeme that is mapped into the token (+).  (^) rate is a lexeme that is mapped into the token ( id, 3 ) , where 3 points to the symbol-table entry for rate  (^) ***** is a lexeme that is mapped into the token (* )  (^) 10 is a lexeme that is mapped into the token (10)  (^) Blanks separating the lexemes would be discarded by the lexical analyzer. ( id, l) ( = ) (id, 2) (+) (id, 3) ( * ) (60)

Syntax Analysis

 (^) The second phase of the compiler is syntax analysis or parsing. The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream. A typical representation is a syntax tree in which each interior node represents an operation and the children of the node represent the arguments of the operation.  (^) This tree shows the order in which the operations in the assignment are to be performed.

Cont..

Cont..

 (^) For building such type of syntax tree the production rules are to be designed.  (^) The rules are usually expressed by context free grammar.

Operations of Syntax Analysis

  1. Obtain tokens from lexical analyzer
  2. Check whether the expression is syntactically correct
  3. Report syntax errors, if any
  4. Group tokens into statements  (^) Determine the statement class, is it an assignment statement, a condition statement (if statement), etc.
  5. Construct hierarchical structure called parse tree

Cont..

 (^) All variables are defined before they are used.  (^) All functions are defined before they are called, and called with correct number (and types) of parameters, as well as correct return type.  (^) All arithmetic & boolean operators have (+, -, *, /, &&, || and so on) operands of the correct types.  (^) variables (array and classes) are accessed correctly  (^) Assign statements are compatible left-hand and right-hand sides  (^) Control expressions in statements have the correct types

Cont..

 (^) The language specification may permit some type conversions called coercions.  (^) For example, a binary arithmetic operator may be applied to either a pair of integers or to a pair of floating-point numbers. If the operator is applied to a floating-point number and an integer, the compiler may convert the integer into a floating-point number

Intermediate Code Generation

 The next step in compilation process is to translate the abstract syntax

tree into another intermediate representation- abstract assembly.

 It is a kind of code & this code can easily be converted to target code.

 This code is in variety of forms, such as three-address code, quadruple,

triple.

 Intermediate code can be either language specific (e.g., Byte Code for

Java) or language independent (three-address code).

 Abstract assembly is middle ground between the source language and

target language.

Three-Address Code

 (^) Three-address code consists of instructions each of which has at the most three operands.  (^) Properties of three-address code

  1. Each three address instruction has at the most one operator in addition to the assignment.
  2. The compiler must generate a temporary name to hold the value computed by each instruction.
  3. Some instruction may have fewer than operands.