409.f
409.f
1. Core Concepts
Back: A translator that converts high-level code (e.g., C++) into machine code all at once.
Back:
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
4. Intermediate Code Generation
5. Code Optimization
6. Gode Generation
Back: Validates code structure using grammar rules and builds a parse tree.
Example:
temp1 = b * 60.0
a = temp1 + c
Example: Replacing x = x + 0 → x.
Example:
MOV b, R1
MUL #60.0, R1
ADD c, R1
Example:
Back:
Example:
=
/\
a +
/\
b *
/\
c 60
Example: a + b * c → a + (b * c).
Example:
t1 = b * 60
t2 = a + t1
Example:
Original: t1 = b + c; t2 = b + c;
Optimized: t1 = b + c; t2 = t1;
Example:
Back: A "dictionary" storing identifiers and their attributes (type, scope, memory address).
Back:
1. Panic Mode
2. Phrase-Level Recovery
3. Error Productions
4. Global Correction
Back: Adding grammar rules to handle common errors (e.g., missing semicolon).
Back:
• Lexical: Lex/Flex
• Syntax: Yacc/Bison
Back:
Back:
Back:
• Lexical: Tokens.
• Syntax: Parse tree.
Back:
9. Exam Hotspots
Back: Machine code with adjustable memory addresses (handled by the loader).
Back: Processes macros (#define), file inclusion (#include), and conditional compilation (#ifdef).
Back: Combines object files into an executable and loads it into memory.
1. Core Concepts
Term Definition/Example
2. Phases of Compilation
Syntax Validates a + b * c as a + (b * c)
Tokens → Parse Tree
Analysis (operator precedence).
Code
Optimizat Intermediate Code → Optimized Code Replaces x = x + 0 → x.
ion
Code
MOVF rate, R1; MULF #60.0, R1;
Generatio Optimized Code → Machine Code
ADDF initial, R1.
n
3. Lexical Analysis
"return" → keyword
Token Categorized lexeme (e.g., keyword, identifier).
token.
Rule (regex) defining valid lexemes (e.g., [a-z]+ for [0-9]+ → pattern for
Pattern
identifiers). numbers.
4. Error Handling
Panic Mode Skip input until a valid token (e.g., ;). Skip until ; after a = b +.
Local
Insert/delete/replace characters to fix errors. Replace fi with if.
Correction
Error Add grammar rules to handle common errors Allow if (x) { ... }
Productions (e.g., missing ;). without ;.
Componen
Role Tools
t
Lexical
Generates tokens from source code. Lex, Flex.
Analyzer
Syntax
Builds parse tree using grammar rules. Yacc, Bison.
Analyzer
Remove unreachable
Dead Code Elimination Delete if (false) { ... }.
code.
8. Mnemonics & Examples
Mnemon
Purpose Example
ic
Lazy
Phases of Compilation: Lex → Syntax → Semantic → position = initial +
Squirrel
Intermediate → Codegen → Generate. rate * 60.
s...
Token
"123" → lexeme,
vs. Lexeme = raw text, Token = categorized lexeme.
num → token.
Lexeme
Inserts tokens into the symbol table Updates symbol table with semantic
(basic entries). information (e.g., type, scope).
1. Simplicity of Design
a. Separating tokenization (lexical) from grammar validation (syntax) simplifies compiler
architecture.
2. Efficiency
a. Specialized buffering techniques in the lexical analyzer speed up tokenization.
3. Specialization
a. Lexical analyzers use regular expressions, while parsers use context-free grammars
(CFG).
4. Portability
a. Lexical analyzers handle platform-specific input (e.g., file encoding), isolating these
details from the parser.
1. Time-Consuming
a. Requires careful design of regex patterns for token recognition.
2. Complex Regular Expressions
a. Some patterns (e.g., floating-point numbers) are harder to define than PEG or EBNF
rules.
3. Debugging Overhead
a. Testing tokenization rules (e.g., edge cases like 0x1F vs. 0x1G) can be tedious.
4. Runtime Overhead
a. Generating token tables (e.g., DFA/NFA) adds initial compilation time.
5. Key Concepts
6. Example Workflow
Input Code:
int x = 42 + 5.3;
b. Syntax Error: Mixing int and float in an expression (detected in semantic analysis).
7. Summary Table