CHAPTER 1 Flashcards
what is compiler?
is a program that converts source code written in a high-level programming language (e.g., Java, C++) into machine code that a computer can understand.
Difference Between Compilers and Interpreters
Compiler: Translates the whole program at once (e.g., C, Java).
Interpreter: Translates and executes line by line (e.g., Python).
what is JIT compiler?
A hybrid that compiles parts of code while the program runs (used in Java’s JVM).
- What are the major compiler phases?
Lexical analysis (Scanning) – Converts source code into tokens.
Syntactic analysis (Parsing) – Checks syntax and constructs a parse tree.
Semantic analysis – Ensures meaningfulness, such as type checking.
Intermediate code generation – Converts the parsed code into an intermediate representation (IR).
Optimization – Improves performance without altering functionality.
Target code generation – Converts intermediate code into machine code.
What is the difference between the analysis and synthesis phases?
Analysis phase: Breaks down source code into structures (lexical analysis, parsing, and semantic analysis).
Synthesis phase: Converts the analyzed representation into executable code (intermediate code generation, optimization, and target code generation).
Why do we use intermediate code?
It is independent of both source and target machines.
Enables optimizations before generating machine code.
Allows multiple frontends (source languages) to work with multiple backends (target architectures).
What is the advantage of separating the front and back ends?
The frontend (language-specific) and backend (machine-specific) separation makes the compiler modular.
A single frontend can support multiple target architectures.
Easier debugging and maintenance.
Lexeme
A sequence of characters forming a meaningful unit (e.g., while in while (x < y)).
Token
A symbolic representation of a lexeme, often with attributes (e.g., WHILE for while).
Parse tree
A tree representation of the syntactic structure of a program.
Abstract syntax tree (AST)
A simplified version of the parse tree, removing unnecessary syntax details.
Intermediate code
A low-level representation between source and machine code, often resembling assembly.
What is the difference between assembly code, object code, and executable code?
Assembly code: Human-readable low-level code generated from source code.
Object code: Machine code with unresolved references, produced after assembly.
Executable code: Fully linked machine code ready for execution.
What is bytecode, an interpreter, a virtual machine?
Bytecode: Platform-independent low-level code executed by a virtual machine.
Interpreter: Directly executes code without converting it to machine language beforehand.
Virtual machine (VM): Software that emulates a physical machine, executing bytecode.
What kind of errors can be caught by a compiler? A runtime system?
Compiler: Catches lexical, syntactic, and static-semantic errors (e.g., undeclared variables, type mismatches).
Runtime system: Catches runtime errors such as null pointer exceptions, division by zero, and stack overflows.
what is transpiler
A transpiler (or source-to-source translator) translates code from one high-level language to another.
Example: Transpiling Typescript source code to Javascript.
Applications of compiler construction
Traditional compilers from source to assembly
* Source-to-source translators, transpilers, preprocessors
* Interpreters and virtual machines
* Integrated programming environments
* Program analysis tools
* Refactoring and other program transformation tools
* Domain-specific languages