Compiler design Examination 2015 a. Define pass, cross compiler and preprocessor. Cross compiler: A compiler that runs on platform (A) and is capable of generating executable code for platform (B) is called a cross-compiler. Preprocessor: A preprocessor, generally considered as a part of compiler, is a tool that produces input for compilers. It deals with macro-processing, augmentation, file inclusion, language extension, etc. Pass: b. Describe the different phases of compiler. The different phase of a compiler are as follows: 1. Lexical analyzer 2. Syntax analyzer 3. Semantic analyzer 4. Intermediate code generator 5. Code optimizer 6. Code generator Lexical analyzer: in lexical analysis the stream of character making up the source program is read from left to rite- and grouped into tokens that are sequences of characters having a collective meaning. The phase that makes this analysis portion is called lexical analyzer. Syntax analyzer: syntax analysis involves grouping the tokens of the source program into grammatical phrases that are used by the compiler to synthesize output. The phase that makes this analysis portion is called syntax analyzer. Semantic analyzer: the semantic analyzer makes the semantic analysis portion. The semantic analysis phase cheeks the source program for semantic error and gathers type information for the subsequent code generation phase. It uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operands of expressions and statements.
COMPILER DESIGN
SHAHEEN REZA
Page | 1
Compiler design Source program
Lexical analyzer Page | 2
Syntax analyzer
Symantec analyzer Symbol table handler
Error handler Intermediate code generator
Code optimizer
Code generator
Target program
Figure: phases of a compiler
Intermediate code generator: after syntax and semantic analysis some compilers generate an explicit intermediate representation of the source program. We can think of this intermediate representation as a program for an abstract machine. This intermediate representation should have two important properties. • It should be easy to produce. • It should be easy to translate into the target program. COMPILER DESIGN
SHAHEEN REZA
Compiler design Code optimizer: the code optimizer phase attempts to improve the intermediate code. So that faster running machine code will result. Code generator: the final phase of the compiler is the generation of target code. Memory locatable machine code or assembly code. Memory locations are selected for each of the variables used by the program. Then, intermediate instructions that perform the same task. A crucial aspect is the assignment of variables to registers. Symbol table manager: an essential function of a compiler is to record the identifier used in the source program and collect information about various attribute of each identifier. These attributes may provide information about the storage allocated for an identifier. A symbol table is a data structure containing a, record for each identifier, with fields for the attributes of the identifier. The data structure allows us to find the record for each identifier quickly and to store or retrieve data form that record quickly. Error handler: each phase can encounter errors. However, after detecting an error, a phase must somehow deal with that error, so that compilation can proceed, allowing farther errors in the source program to be detected. The syntax and semantic analysis phases usually handle a large fraction of the error detectable by the compiler. Consider the statement. Position: =initial + rate*60.
c. What are the types of errors found in different phases of compiler? Ans: each of the six phases (but mainly the analysis phases) of a compiler can encounter errors. On detecting an error, the compiler must: • Report the error in a helpful way. • Correct the error if possible, and • Continue processing (if possible) after the error to look for further errors. Types of error. Errors are either syntactic or semantic: Syntax errors are errors in the program text; they may be either lexical or grammatical; (i) A lexical error is a mistake in a lexeme, for examples, typing then instead of then, or missing off one of the quotes in a literal. COMPILER DESIGN
SHAHEEN REZA
Page | 3
Compiler design (ii)
A grammatical error is a one that violates the (grammatical) rules of the language. For example if x=7 y: =4(missing then).
Semantic errors are mistakes concerning the meaning of a program of a program construct; they may be either type errors, lexical error or run time errors; (i) type errors occur when an operator is applied to an argument of the wrong type. Or to the wrong number of arguments. (ii) Logical errors occur when a badly conceived program is executed. For example: while X=y do …………. when x and y initially have the same value and the body of loop need not change the value of either x or y. (iii) Run time errors that can be detected only when the program is executed. For example: var x: real; readIn(x); writeIn(1/x) which would produce a run time error if the user input 0 syntax errors must be detected by a compiler and at least reported to the user (in a helpful way). If possible, the compiler should make the appropriate correction(s). semantic errors are much harder and sometimes impossible for a computer to detect. d. Define symbol table and write down the functions of symbol table. Ans: follow the question 2012 Examination 2012 a. Define compiler. What are the differences between compiler and interpreter? b. What do you mean by preprocessor? What are the operations of preprocessor? c. What do you mean by token? Describe different types of tokens. d. What is symbol table? What are the function of a symbol table? Ans: symbol table: a symbol table is a data structure use by a language translator such as a compiler or interpreter, where each identifier in a programs source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.
COMPILER DESIGN
SHAHEEN REZA
Page | 4
Compiler design A symbol table is a table that binds names to information. We need a number of function on symbol tables to accomplish this. • We need an empty symbol, in which no name is defined • We need to be able to behind a name to a piece of information.in case the name is already defined in the symbol table. The new binding takes precedence over the old. • We need to able to look up a name in a symbol table to find the information the name is bound to. If the name is not defined in the symbol table, we need to be told that. • We need to be able to enter a new scope. • We need to be able to exit a scope, reestablishing the symbol table to what it was before the scope was entered. Examination 2013 a. What is compiler? Explain the different phases of compiler. Ans: the different phases of compiler: follow the question of 2015 b. Why do we need translators? Ans: follow the question of 2010 c. What so you mean by three address code? Describe with an example. Three address code: Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the form of an annotated syntax tree. That syntax tree then can be converted into a linear representation, e.g., postfix notation. Intermediate code tends to be machine-independent code. Therefore, code generator assumes to have unlimited number of memory storage (register) to generate code. For example: a = b + c * d; The intermediate code generator will try to divide this expression into sub-expressions and then generate the corresponding code. r1 = c * d; r2 = b + r1; r3 = r2 + r1; a = r3 r being used as registers in the target program. A three-address code has at most three address locations to calculate the expression. A three-address code can be represented in two forms: quadruples and triples. COMPILER DESIGN
SHAHEEN REZA
Page | 5
Compiler design d. What is the code optimization? How compiler optimize source code? Examination 2014 a. Define compiler. Illustrate the representation of the following statement after each phase of a compiler: Position = initial + rate × 60. Compiler: a compiler is a program that read a program written in one language the source language and translates it into an equivalent program in another language the target language. A compiler reports to its search the presence of errors in the source program. An approach of a compiler is shown in figure. Compiler are sometimes classified as single pass, multiphases load and go, debugging or optimizing, depending on how they have been constructed or on what function they are supposed to perform.
Source program
Compiler
Target program
b. What are the reasons for separating the analysis phase of compiling into lexical analysis and parsing? c. What are the phase of analysis and describe them. Ans: the phases of analysis: follow the question of 2015 d. Define preprocessor. What are the function of preprocessor? Preprocessor: A preprocessor, generally considered as a part of compiler, is a tool that produces input for compilers. It deals with macro-processing, augmentation, file inclusion, language extension, etc. Function of preprocessor: Examination 2010 a. Define translator software. what are the differences between compiler and interpreter? Translator software: a translator is a computer program that translates a program written in a given programming language into a functionally equivalent program in a different language. Depending on the translator, this may involve changing or simplifying the program flow, without losing COMPILER DESIGN
SHAHEEN REZA
Page | 6
Compiler design the essence of the program, thereby producing a functionally equivalent program. Differences between compiler and interpreter: Compiler Interpreter 1. Compiler takes entire program 1. Interpreter takes signal as input instruction as input. 2. Intermediate object code is 2. No. intermediate code is generated. generated. 3. Conditional control statements 3. Conditional control are executing faster. statements execute slower. 4. Memory requirement more. 4. Memory every time higher level program is converted into lower level program. 5. Example: c compiler. 5. Example: BASIC b. Describe different phases of compiler with example. Ans: phases of compiler: follow the question of 2015 c. What is symbol table? What are the function of symbol table? Ans: follow the question of 2012 d. What are the type of error found in different phases of compiler? Ans: follow the question of 2015
COMPILER DESIGN
SHAHEEN REZA
Page | 7