Turing Machines and Compiler Design Fundamentals
Introduction to Turing Machines (TM)
- Definition: A Turing Machine is an abstract machine that accepts languages generated by Type 0 grammars, also known as Unrestricted Grammars (UG).
- History: The concept was orignally invented in 1936 by Alan Turing.
- Language Hierarchy:
- Unrestricted Grammar (UG): Generates Recursively Enumerable Languages (REL).
- Recursively Enumerable Language (REL): A language that is accepted/recognized by a Turing Machine.
- Context-Sensitive Grammar (CSG): Generates Context-Sensitive Languages (CSL).
- Context-Free Grammar (CFG): Generates Context-Free Languages (CFL) accepted by Pushdown Automata (PDA).
- Regular Grammar (RG): Generates Regular Languages (RL) accepted by Finite Automata (FA).
Model of a Turing Machine
- Components: The Turing Machine model consists of three primary components:
- Infinite Input Tape:
- The tape is divided into a series of cells.
- Each cell can hold exactly one symbol.
- The symbols that make up the input string are initially placed on the tape; all other remaining cells hold the Blank symbol ().
- The tape is two-way, meaning it is infinite in both directions (or conceptually extending indefinitely from the input).
- Read/Write Head:
- This component is capable of reading symbols from the input tape and writing symbols back to the tape.
- It can move one cell at a time to the Left () or to the Right ().
- Finite Control (FC):
- The FC manages the actions performed by the machine.
- It tracks the current state of the TM.
- It makes decisions on where to move ( or ) and what to write based on the current state and the symbol being read.
- Infinite Input Tape:
Formal Mathematical Definition
- 7-tuple System: A TM is formally defined as .
- : A finite set of states.
- : The input alphabet (the set of symbols allowed in the input string).
- : The set of symbols that can appear on the input tape ().
- : The initial state ().
- : The Blank symbol (, but ).
- : The set of final or accepting states ().
- : The transition function.
- Transition Functions:
- Deterministic TM (DTM): .
- Non-Deterministic TM (NTM): .
- Halting Conditions:
- If a string , the machine halts in a final state.
- If a string , the machine either halts in a non-final state or loops infinitely (fails to halt).
Representations of Turing Machines
- Transition Table:
- A tabular representation of the transition function .
- Columns: Represent the symbols on the input tape ().
- Rows: Represent the various states ().
- Entries: Contain the next state, the symbol to be written, and the direction of movement.
- Symbols: The start state is marked with an arrow (), and the accept/final state is marked with a star ().
- Undefined Entries: Specify that there is no transition defined; if the machine reaches an undefined entry (a "dead state"), it halts and rejects the input.
- Transition Diagram:
- Consists of a set of nodes/circles representing states ().
- Directed edges (arrows) represent transitions from one state to another.
- The start state is indicated by an incoming arrow.
- Final states are represented by double circles.
- Edge Labels: Transitions are formatted as where:
- = Old tape symbol (the symbol being read).
- = New tape symbol (the symbol to be written).
- = Direction of movement ( or ).
- Instantaneous Description (ID):
- A string used to represent the current configuration of the TM.
- Format: , where is the current state and is the current content of the tape.
- The Read/Write head is assumed to be pointing at the first symbol of the substring .
- An ID captures:
- Content of all cells from the leftmost non-blank symbol to the rightmost non-blank symbol.
- The cell currently being scanned.
- The current state of the machine.
Case Study: Construction for
- Logic: The goal is to match an equal number of 's and 's.
- Read the leftmost , change it to , and move right looking for the leftmost .
- Once a is found, change it to and move left looking for the rightmost .
- Move one cell to the right of to find the next and repeat the cycle.
- If for every there is a corresponding , the machine will eventually see only 's and 's (blanks).
- Reach the final state if symbols match perfectly.
- States Summary:
- : Look for an to replace with .
- : Skip over 's and 's moving right to find a .
- : Skip over 's and 's moving left to find an .
- : Completion check (no move 's left, looking for blanks).
- : Final State.
- Transition Function Example: .
Case Study: Construction for
- Logic Extension:
- Read , write , move right to find the first .
- Read , write , move right to find the first .
- Read , write , move left until the first is reached.
- Move right to find the next and repeat.
- If all characters are successfully replaced by , the machine reaches an accepting state ().
- Symbols: .
Case Study: Palindrome Recognition
- Even Palindrome: (e.g., , ).
- Read the leftmost symbol ( or ) and replace it with a Blank ().
- Move right to the end of the string (before the Blank).
- Check if the rightmost symbol matches the one replaced on the left.
- If they match, replace the rightmost character with and move left to repeat the process on the remaining string.
- If the tape becomes entirely blank, the string is accepted.
- Odd Palindrome: (e.g., ).
- The logic is similar, but the machine accounts for a middle pivot character ().
Language Processing Systems
- Compiler Definition: A compiler is a language translator or program that translates a source language (typically high-level like C, Java, Fortran) into an equivalent target language (typically low-level machine code or assembly).
- Goals of Translation:
- Correctness: All valid programs must compile correctly.
- Performance: Minimizing the size of machine code and maximizing execution speed.
- Compile-Time Efficiency: How fast the compiler itself runs.
- Environment Components: Programs are often developed using a suite of tools:
- Preprocessor: Produces input for the compiler. Functions include Macro processing (e.g.,
#define PI 3.14), File inclusion (#include <stdio.h>), Rational preprocessors, and Language extensions. - Assembler: Translates assembly language (mnemonics) into machine code (object program).
- Linker: Combines several object files into a single program and resolves external references.
- Loader: Reads relocatable machine code, alters addresses to fit current memory, and loads it into memory for execution.
- Preprocessor: Produces input for the compiler. Functions include Macro processing (e.g.,
Phases of a Compiler
The compilation process is divided into two main parts: Analysis (Front End) and Synthesis (Back End).
- Lexical Analyzer (Scanner):
- Reads the source program character by character.
- Groups characters into meaningful sequences called Lexemes.
- Outputs a stream of Tokens in the format
<Token name, Attribute value>. - Removes comments and whitespace.
- Syntax Analyzer (Parser):
- Takes tokens as input.
- Uses production rules (grammar) to create a tree-like representation called a Parse Tree or Abstract Syntax Tree.
- Checks for grammatical errors.
- Semantic Analyzer:
- Checks the syntax tree for semantic correctness (meaning).
- Performs Type Checking: Identifies type mismatches and handles conversions (e.g.,
inttoreal). - Ensures variables are declared before use and functions have proper arguments.
- Intermediate Code Generator (ICG):
- Produces a low-level or machine-independent representation of the source program.
- Common forms: Three-address code (e.g., ).
- Code Optimizer (CO):
- Optional but highly beneficial phase.
- Improves the code to run faster or use less power/space.
- Technique examples: Loop optimization, removal of redundant computations, strength reduction.
- Code Generator (CG):
- The final phase that produces the target machine code (relocatable machine code or assembly).
- Manages memory allocation and register assignments.
Lexical Analysis and Symbol Table
- Tokens vs. Lexemes:
- Token: A syntactic category (e.g., Identifier, Constant, Keyword, Operator).
- Lexeme: The actual sequence of characters (e.g.,
100.01,counter).
- Symbol Table:
- The primary data structure of the compiler.
- Stores identifiers, their types, scopes, and memory addresses.
- Interacts with all phases to facilitate searching and fetching.
Input Buffering
- Rationale: Reading from a hard disk character by character is time-consuming and expensive. Buffering stores a block of characters in memory to be scanned efficiently.
- Pointers:
- Begin Pointer (
bp): Points to the start of the current lexeme. - Forward Pointer (
fp): Moves ahead to find the end of the lexeme.
- Begin Pointer (
- One Buffer Scheme:
- Uses a single buffer to store input.
- Problem: If a lexeme is longer than the buffer, the first part may be overwritten when the buffer is refilled.
- Two Buffer Scheme:
- Uses two buffers scanned alternately.
- When the
fpreaches the end of the first buffer, the second is filled and scanned. - Sentinels: A special Character (
eof) is placed at the end of each buffer to signal the boundary. This prevents the machine from checking the end of the buffer at every single character increment, improving speed.
- Pseudo-code for Buffer Navigation:
if forward at end of first half then reload second half; forward := forward + 1else if forward at end of second half then reload first half; move forward to beginning of first halfelse forward := forward + 1