By Bhupendra Saud Compiler Design and Construction (CSC 352) By Bhupendra Saud for B. Sc. Computer Science & Information Technology (Prime College) Course Title: Complier Design and Construction Course no: CSC-352 Full Marks: 70+10+20 Credit hours: 3 Pass Marks: 28+4+8 Nature of course: Theory (3 Hrs.) + Lab (3 Hrs.) Course Synopsis: Analysis of source program. The phases of compiler. Goal: This course introduces fundamental concept of compiler and its different phases. Course Contents: Unit. 1: 1.1 Introduction to compiling: Compilers, Analysis of source program, the phases of compiler, compiler-construction tools. 4 hrs. 1.2 A Simple One-Pass Compiler: Syntax Definition, Syntax directed translation, Parsing, Translator for simple expression, Symbol Table, Abstract Stack Machines. 5 hrs Unit 2: 2.1 Lexical Analysis: The role of the lexical analyzer, Input buffering, Specification of tokens, Recognition of tokens, Finite Automata, Conversion Regular Expression to an NFA and ten to DFA, NFA to DFA, State minimization in DFA, Flex/lex introduction. 8 Hrs. 2.2 Syntax Analysis: The role of parser, Context frees grammars, Writing a grammars, Top-down parsing, Bottom-up parsing, error recovery mechanism, LL grammar, Bottom up parsingHandles, shift reduced parsing, LR parsers-SLR,LALR,LR,LR/LALR Grammars, parser generators. 10 Hrs. 1
By Bhupendra Saud Unit 3: 3.1 Syntax Directed Translation: Syntax-directed definition, Syntax tree and its construction, Evaluation of S-attributed definitions, L-attributed, Top-down translation, Recursive evaluators. 5 Hrs. 3.2 Type Checking: Type systems, Specification of a simple type checker, Type conversions equivalence of type expression, Type checking Yacc/Bison. 3 Hrs. Unit 4: 4.1 Intermediate Code Generation: Intermediate languages, three address code, Declarations, Assignments Statements, Boolean Expressions, addressing array elements, case statements, Back patching, procedure calls. 4 Hrs. 4.2 Code Generation and optimization: Issues in design of a code generator, the target machine, Run –time storage management, Basic blocks and flow graphs, next use information’s, a simple code generator, Peephole organization, generating code from dags. 6 Hrs. Third Year/Six Semester Subject: Compiler Design and Construction FM: 60 Time: 3 hours PM: 24 Candidates are required to give their answer in their own words as for as practicable. Attempt all the questions. Every question contains equal marks. Year: 2068 1. What do mean by compiler? How source program analyzed? Explain in brief. 2. Discuss the role of symbol table in compiler design. 3. Convert the regular expression ‘0+ (1+0)*00’ first into NFA and then into DFA using Thomson’s and Subset Construction methods. 4. Consider the grammar: S->( L )| a L->L, S|S 5. Consider the grammar C→AB A →a B→ a Calculate the canonical LR (0) items. 6. Describe the inherited and synthesized attributes of grammar using an example. 7. Write the type expressions for the following types. a. An array of pointers to reals, where the array index range from 1 to 100. b. Function whose domains are function from characters and whose range is a pointer of integer. 8. What do you mean by intermediate code? Explain the role of intermediate code in compiler design. 9. What is operation of simple code generator? Explain. 10. Why optimization is often required in the code generated by simple code generator? Explain the unreachable code optimization. 2
By Bhupendra Saud Prerequisites – Introduction to Automata and Formal Languages – Introduction to Analysis of Algorithms and Data Structures – Working knowledge of C/C++ --Introduction to the Principles of Programming Languages, Operating System & Computer Architecture is plus Resources Text Book: Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools, Addison-Wesley, 1986 References: Research and technical papers. What is Compiler? A compiler is a program that takes a program written in a source language and translates it into an equivalent low level program in a target language. 3
By Bhupendra Saud Phases of a Compiler There are two major parts of a compiler: Analysis and Synthesis – In analysis phase, an intermediate representation is created from the given source program. This phase (Source code analysis phase) is mainly divided into following three parts: • Lexical Analyzer • Syntax Analyzer and • Semantic Analyzer – In synthesis phase, the equivalent target program is created from this intermediate representation. This phase is divided into following three parts: • Intermediate Code Generator • Code Optimizer and • Final Code Generator Figure: Phases of a compiler Lexical Analyzer Lexical Analyzer reads the source program in character by character ways and returns the tokens of the source program. Normally a lexical analyzer doesn’t return a list of tokens, it returns a token only when the parser asks a token from it. Lexical analyzer may also perform other auxiliary operation like removing redundant white space, removing token separator (like semicolon) etc 4 .