Docsity

Compiler design notes, Lecture notes of Compiler Design

Toggle sidebar
bg1
KnowledgeGate
www.knowledgegate.in
Compiler Design
•Compiler Design is a core subject in university exams and frequently appears in standardized tests such as GATE
(4–5 marks) and NET (8–10 marks). Its syllabus is relatively small and heavily math-oriented, comprising about
25% theory and 75% numerical problems. Although it’s not commonly emphasized in industry interviews, it
remains an important academic subject. A background in Theory of Computation, as well as some familiarity with
Operating Systems and Computer Architecture, can be helpful. While time-consuming, the concise syllabus and its
focus on applied mathematics make it manageable.
•Title: Compilers: Principles, Techniques, and Tools
•Authors: Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D.
Ullman
•Publisher: Pearson
•Edition: 2nd Edition
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Compiler design notes and more Lecture notes Compiler Design in PDF only on Docsity!

nowledgeGate

Compiler Design

  • Compiler Design is a core subject in university exams and frequently appears in standardized tests such as GATE (4–5 marks) and NET (8–10 marks). Its syllabus is relatively small and heavily math-oriented, comprising about 25% theory and 75% numerical problems. Although it’s not commonly emphasized in industry interviews, it remains an important academic subject. A background in Theory of Computation, as well as some familiarity with Operating Systems and Computer Architecture, can be helpful. While time-consuming, the concise syllabus and its focus on applied mathematics make it manageable. - Title: Compilers: Principles, Techniques, and Tools - Authors: Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman - Publisher: Pearson - Edition: 2nd Edition

nowledgeGate

Syllabus

  • Lexical analysis
  • Parsing
  • Syntax-directed translation
  • Runtime environments
  • Intermediate code generation
  • Local optimisation, Data flow analyses: constant propagation, liveness analysis, common subexpression elimination.

Subject blue print ∙ Basics of compiler (0 marks) o Language Process System, pre-processor, compiler, assembler, loader/linker o Phases and pass in a compiler o Compiler vs interpreter o Symbol table and error handler o Types of error ∙ Lexical Analysis (1-2 marks question, most of the time theoretical question) o Basics, Lexemes, Tokens o Functions of lexical analyser o Lexical analyser implementation ∙ Parsing Techniques (2-3, marks question mostly on numerical one theoretical) o Basics of parsing o CFG grammar, ambiguity, derivation, determinism o Top down parsing, LL (1) o Bottom up parsing, LR (0), SLR (1), CLR (1) and LALR (1) o OPP ∙ Syntax Directed Translation (1-2 basic question) o Basics o Types of attributes o DAG o Types of SDT, S/L attributes ∙ Code Generation and Optimization (1-2 marks theory or numerical) o Basics o 3 address code o Code optimization

nowledgeGate

High Level Language

  • High-level languages (HLLs) often include preprocessor directives, such as #include and #define, which simplify coding and make programs more readable. These directives instruct the preprocessor to integrate external files, define constants, and configure the code before it’s compiled. While this approach brings the program closer to human understanding, it remains distant from the machine’s low-level operations.

nowledgeGate

  • The preprocessor handles several tasks before compilation, such as including external files in place of #include directives and expanding #define macros. Essentially, it streamlines the source code by performing file inclusion, macro-processing, and other shorthand operations, ensuring that the compiler receives a fully prepared codebase.

nowledgeGate

Compiler Design

- Analysis Phase: - Converts source code into an intermediate representation (IR). - Focuses on understanding the program's structure and semantics. - Synthesis Phase: - Transforms the intermediate representation into the target program (e.g., assembly or machine code). - Produces executable code equivalent to the original source.

nowledgeGate

Assembly Language

- Assembly Language Overview: It serves as an intermediate representation between high-level languages and binary machine code. Consists of machine instructions and additional useful data required for execution. - Platform-Specific Nature: Each hardware and operating system (platform) has its unique assembler. Assembly languages are not universal and vary based on the platform. - Assembler Functionality: Translates assembly language into machine code. Produces an output called the object file , which can be executed on the target platform.

nowledgeGate

BASIS FOR COMPARISON COMPILER INTERPRETER Input It takes an entire program at a time. It takes a single line of code or instruction at a time. Output It generates intermediate object code. It does not produce any intermediate object code. Working mechanism The compilation is done before execution. Compilation and execution take place simultaneously. Speed Comparatively faster Slower Memory Memory requirement is more due to the creation of object code.

It requires less memory as it does not create intermediate object code. Errors Display all errors after compilation, all at the same time.

Displays error of each line one by one.

Error detection Difficult Easier comparatively Code Optimization Code Optimization is possible to a much greater extent

Code Optimization is not very effective

Pertaining Programming languages

C, C++, C#, Scala, typescript uses compiler. PHP, Perl, Python, Ruby uses an interpreter.

nowledgeGate

nowledgeGate

Lexical Analyzer

- Purpose of Lexical Analysis: Reads the program and breaks it into lexemes (smallest units of meaningful text). Converts lexemes into tokens , which are predefined structures recognized by the lexical analyzer. - Tokenization: Tokens are categorized using regular expressions. The lexical analyzer identifies and assigns token types like identifiers, operators, and constants.

Float x, y, z; x = y+ z*

X -> token -> identifier = -> token -> operator Y -> token -> identifier

  • -> token -> operator Z -> token -> identifier
  • -> token -> operator 60 -> token -> constant

id 1 = id 2 + id 3 * 60

nowledgeGate

Syntax Analyzer

- Role: Responsible for analyzing the structure of the code. Ensures the code adheres to the grammatical rules of the programming language. - Key Features: Constructs a Parse Tree or Syntax Tree to represent the program's grammatical structure. Utilizes Context-Free Grammar (CFG) rules to build the parse tree. - Process: Reads tokens generated by the lexical analyzer, one by one. Verifies the syntax based on predefined production rules.

S -> id = E E -> E + T / T T -> T * F / F F -> id / num id 1 = id 2 + id 3 * 60

nowledgeGate

Semantic Analyzer

- Role: Ensures that the parse tree constructed by the syntax analyzer is meaningful and adheres to semantic rules. - Key Features: Verifies the logical correctness of the parse tree. Checks for semantic errors like type mismatches, undeclared variables, or invalid operations. - Output: Produces a verified or annotated parse tree that includes additional information, such as data types and scope rules, for further processing.

nowledgeGate

Intermediate Code Generator

- Role: Produces an intermediate code that is simpler than source code and easier for machines to interpret. Acts as a bridge between the front-end (language-dependent) and back-end (platform-dependent) of the compiler. - Characteristics: Intermediate code is platform-independent. Common formats include three-address code (TAC) , among others. - Process Example: For the expression - x = y + z * 60: - t 1 = z * 60 - t 2 = y + t (^1) - x = t (^2) - Advantages: Simplifies the development of new compilers by reusing intermediate code from existing compilers. Final translation to machine code depends on the platform and occurs in the last two compiler phases, making it platform-dependent.

nowledgeGate

Target Code Generator

- Purpose: Converts intermediate code into machine-understandable code. Handles register allocation and instruction selection to optimize the code for execution. - Characteristics: The output is dependent on the type of assembler and the target machine architecture. This is the final phase of the compilation process.

MOV R 1, Z

MUL R 1, 60

ADD R 1, Y

STORE X, R 1

nowledgeGate

Symbol Table

  • A data structure used and maintained by the compiler to store all identifiers' names along with their types, scope, and other information.
  • Facilitates smooth compiler operations by allowing quick lookups for identifiers during compilation.
  • Tracks variables, arrays, records, procedures, and functions across the

program.

  1. int x = 10;

Line No Keyword identifier Constant Operator

1 int x 10 ;