PRINCIPLES OF COMPILER DESIGN 2 MARKS UNIT I INTRODUCTION TO COMPILING 1. Define compiler? A compiler is a program that reads a program written in one language (source language) and translates it into an equivalent program in another language (target language) and the compiler reports to its user the presence of errors in the source program. 2. What are the classifications of compiler? i) Single pass compiler ii) Multi pass compiler iii) Load-and-go compiler iv) Debugging or optimizing compiler 3. What are the phases of compiler? 1. Lexical analyzer 2. Syntax analyzer 3. Semantic analyzer 4. Intermediate code generation 5. Code generation 6. Code optimization 7. symbol table manager. 8. Error Handler 4. Define preprocessor & what are the functions of preprocessor? Preprocessor produce input to the compilers (i.e.) the program will be divided in to the modules. They perform the following functions. i) Macro processing ii) File inclusion iii) Rational preprocessor
iv) Language extension 5. What are the tools available in analysis phase? i) Structure editors ii) Pretty printer iii) Static checkers iv) Interpreters. 6. Define pretty printers? A pretty printer analyzes a program and prints it in such a way that the structure of the program becomes clearly visible. For the comments may appear with an amount of indentation proportional to the depth of their nesting in the hierarchical organization of the statements. 7. Define assembler and its types? It is defined by the low level language is assembly language and high level language is machine language is called assembler. One pass assembler Two pass assembler 8. Give the types of a language processing system? a) Preprocessors b) Compilers c) Assembler d) Loaders and link editors 9. What are the functions performed in analysis phase? a) Lexical analysis or Linear analysis b) Syntax analysis or hierarchical analysis c) Semantic analysis 10. What are the functions performed in synthesis phase? i) Intermediate code generation ii) Code generation iii) Code optimization 11. Give the classification of processing performed by the semantic analysis? a) Processing of declarative statements. b) Processing of executable statements. 12. Give the properties of intermediate representation?
a) It should be easy to produce. b) It should be easy to translate into the target program. 13. What are the two different parts of compilation? a) Analysis phases of compilation b) Synthesis phases of compilation 14. What is the role of lexical analysis phase? Lexical analyzer reads the source program one character at a time, and grouped into a sequence of atomic units called tokens. Identifiers, keywords, constants, operators and punctuation symbols such as commas, parenthesis, are typical tokens. 15. What is meant by syntax analysis? This phase processes the string of descriptors, synthesized by the lexical analyzer, to determine the syntactic structure of an input statement. This process is known as parsing. Output of the parsing step is a representation of the syntactic structure of a statement in the form of syntax tree. 16. What is meant by intermediate code generation? After syntax and semantic analysis, some compilers generate an explicit intermediate representation of the source program. It can have a variety of forms. This form called three-address code. It consists of sequence of instructions, each of which has at most three operands. 17. What is meant by semantic analysis? This phase checks the source program for semantic errors and gathers type of information for the subsequent phase. An important component of semantic analysis is type checking.in type checking the compiler checks that each operator has operands that are permitted by the source language specification 18. What do you meant by interpreter? A translator that transform a programming language into a simplified language called intermediate code, which can directly execute using a program called an interpreter. 19. What do you meant by phase? Each of which transforms the source program one representation to another. A phase is a logically cohesive operation that takes as input one representation of the source program and produces as output another representation 20. Write short notes on symbol table manager?
The table management or bookkeeping portion of the compiler keeps track of the names used by program and records essential information about each, such as its type (int,real etc.,) the data structure used to record this information is called a symbol table manager. 21. Write short notes on error handler? The error handler is invoked when a flaw in the source program is detected. It must warn the programmer by issuing a diagnostic, and adjust the information being passed from phase to phase so that each phase can proceed. So that as many errors as possible can be detected in one compilation. 22. Mention some of the cousins of the compiler? i) Preprocessors ii) Assemblers iii) Two pass assembly iv) Loaders and Linker-editors. 23. What is front end and back end? The phases are collected into a front end and a back end. The front end consists of those phases or parts of phases, that depends primarily on the source language and is largely independent of the target machine. The back ends that depend on the target machine and generally these portions do not depend on the source language. 24. What do you meant by pass? A pass reads the source program or the output of the previous pass, makes the transformations specified by its phases and writes output into an intermediate file, which may then be read by a subsequent pass. In an implementation of a compiler, portions of one or more phases are combined into a module called pass. 25. List some compiler construction tools? i) Parser generators ii) Scanner generators iii) Syntax-directed translation engine iv) Automatic code generators v) Data-flow engine. 26. Explain any one compiler construction tool? Scanner generators, these automatically generate lexical analyzers normally from a specification based on regular expressions. The resulting of lexical analyzer is in effect of finite automata.
27. What does translator mean? A translator is a program that takes a input program on one programming language (source language) and produces output in another language (object language or target language). 28. What are the two functions of parser? * It checks the tokens appearing in its input, which is output of the lexical analyzer. * It involves grouping the tokens of source program into grammatical phrases that are used by the compiler to synthesize the output. Usually grammatical phrases of the source program are represented by tree like structure called parse tree. 29. What is a language processing system? 16 MARKS 1. Write about the phases of compiler and by assuming an input and show the output of various phases.
The process of compilation is very complex. So it comes out to be customary from the logical as well as implementation point of view to partition the compilation process into several phases. A phase is a logically cohesive operation that takes as input one representation of source program and produces as output another representation. Source program is a stream of characters: E.g. pos = init + rate * 60 lexical analysis: groups characters into non-separable units, called token, and generates token stream: id1 = id2 + id3 * const The information about the identifiers must be stored somewhere (symbol table). Syntax analysis: checks whether the token stream meets the grammatical specification of the language and generates the syntax tree. Semantic analysis: checks whether the program has a meaning (e.g. if pos is a record and init and rate are integers then the assignment does not make a sense). Intermediate code generation, intermediate code is something that is both close to the final machine code and easy to manipulate (for optimization). One example is the threeaddress code: dst = op1 op op2 The three-address code for the assignment statement: temp1 = inttoreal(60); temp2 = id3 * temp1; temp3 = id2 + temp2; id1 = temp3 Code optimization: produces better/semantically equivalent code. temp1 = id3 * 60.0 id1 = id2 + temp1
Code generation: generates assembly code MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 Symbol Table Creation / Maintenance Contains Info (storage, type, scope, args) on Each.Meaningful. Token, typically Identifiers Data Structure Created / Initialized During Lexical Analysis Utilized / Updated During Later Analysis & Synthesis Error Handling Detection of Different Errors Which Correspond to All Phases Each phase should know somehow to deal with error, so that compilation can proceed, to allow further errors to be detected
2. Explain briefly about compiler construction tools. Parser Generators : Produce Syntax Analyzers Scanner Generators : Produce Lexical Analyzers Syntax-directed Translation Engines : Generate Intermediate Code Automatic Code Generators : Generate Actual Code Data-Flow Engines : Support Optimization 3. Explain how abstract stack machines can be used as translators. The front end of a compiler constructs an intermediate representation of source program from which the back end generates the target program. One popular form of intermediate representation is code for an abstract stack machine. Arithmetic instructions L-values and r-values stack manipulation translation of expressions control flow translation of statements emitting a translation 4. Illustrate the compiler internal representation of the changes in the source program, as translation progresses by considering the translation of the statement A:=B+C*50. 5. Write note on front and back end of compiler Grouping of phases. Passes Reducing the no of passes Problems due to grouping of phases 6. Explain Symbol table management and error handling Symbol table Error detection and reporting Error in lexical phase Error in Syntax Analysis phase Error in Semantic Analysis phase 7. Explain cousins of compiler Preprocessors Functions Macro processing, File Inclusion, Rational preprocessors, Language Extensions Macro Definition,
Macro Use Assemblers Assembly code Two Pass Assembly Pass1 Pass2 Loaders and Link Editors Loading, Link Editing External references 8. Describe the following software tools i. Structure Editors ii. Pretty printers iii. Interpreters Structure Editors: Analyses the program text Pretty printers Analyses and prints the program that the structure of the program become clearly visible. Interpreters: Perform operations implied by the source program