




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















CSC373-Compiler Construction Week-2 Lecture- Semester-#5 Fall 2019 Prepared by: Eisha Tir Razia Lahore Garrison University
(^) Compiler types in general (^) Analysis (^) synthesis
(^) Understanding related (^) Compiler phases in detail
(^) 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.
(^) 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
(^) 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)
(^) 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.
(^) For building such type of syntax tree the production rules are to be designed. (^) The rules are usually expressed by context free grammar.
(^) 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
(^) 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
(^) Three-address code consists of instructions each of which has at the most three operands. (^) Properties of three-address code