Quiz #1 Material Summary
Main phases/functions of compiler#
- Lexical Analyzer ==> reads the stream of characters and groups them into meaningful sequences
- Syntax Analyzer ==> using the components produced by the previous phase, creates a tree like intermediate representation that depicts the grammatical structure of the token stream.
- Semantic Analyzer ==> using the tree, check the semantic consistency of the source program with the language definition. Also gathers type information
- Intermediate Code Generator ==> constructs intermediate representation of the target code
- Machine Independent Code Optimzer ==> Optimizes intermediate representation
- Code Generator ==> generate target machine code from intermediate representation
- Machine-dependent Code Optimizer ==> optimizes machine code
Compiler Phases#

Compiler Phases with outputs of each phases:#

Context Free Grammar (CFG)#
- terminals / tokens ==> elementary symbols of the language
- nonterminals ==> represent string of terminals
- productions ==> consists of nonterminal (called left side), an arrow, and sequence of terminals and/or nonterminals (called body)
- a nonterminal designated as the start symbol
Finding FIRST() sets#
Definitions
- $\alpha$ ==> string of grammar symbolsl (terminals and/or non-terminals)
- FIRST($\alpha$) ==> set of terminals that appear as the first symols of one or more strings of terminals generated from $\alpha$
How to compute/find FIRST() sets
- if $X$ is a terminal, then FIRST($X$) = ${X}$
- if $X$ is a nonterminal and $X \to Y_1 Y_2 \cdots Y_k$ is a production,
- FIRST($X$) = FIRST($Y_1$)
- If FIRST($Y_i$) contains $\epsilon$ then FIRST($X$) = $\{ FIRST(Y_i) – \epsilon \} \cup \{FIRST(Y_{i+1})\}$, and so on
- If FIRST($Y_i$) contains $\epsilon$ for all $i$ = 1 to $n$, then add $\epsilon$ to FIRST($X$).
- if $X \to \epsilon$ is a production, then add $\epsilon$ to FIRST($X$).
terminals vs nonterminals#
- terminals ==> tokens, unchangeable like 1, 2, 3,
if - non-terminals ==> terms, like
<digit>,<stmt>, that can be changed to any valued according to definition
Finite Automatas (FAs)#
Nondeterministic FA (NFA) Definition#
- A finite of state $S$
- A set of input symbols $\Sigma$, the input alphabet. It is assumed that $\epsilon$, the empty string is not part of $\Sigma$.
- A transition function that gives, for each state, and for each symbol in $\Sigma\cup{\epsilon}$ a set of next states.
- A state $s_0$ from $S$ that is distinguished as the start/initial state
- A set of states $F$, a subset of $S$, that is distinguished as the accepting/final states
RE to NFA using Thompson’s construction#

NFA to DFA#
Example:
RE: (a|b)*abc
NFA Graph:

Transition table:
| Row | DFA state | NFA states | $a$ | $b$ | $c$ |
|---|---|---|---|---|---|
| 1 | $A$ | $1, 2, 4, 7$ | $B$ | $C$ | |
| 2 | $B$ | $1, 2, 3, 4, 6, 7, 8$ | $B$ | $D$ | |
| 3 | $C$ | $1, 2, 4, 5, 6, 7$ | $B$ | $C$ | |
| 4 | $D$ | $E$ | |||
| 5 | $E$ | $10$ |
Resulting DFA Graph:

Explanation (based on the example)
- What is the contents of the table mean?
| DFA state | NFA states | input character a | input char b | … | input char n |
|---|---|---|---|---|---|
| a state of DFA | corresponding set equivalent of NFA states | next DFA state if next input is a | next DFA state if next input is b | … | if next input is n |
-
how do I find the equivalent set of the NFA for each DFA state?
- for the first DFA state, the initial state, the equivalent set is by starting from the initial state of the NFA, unioned with all the states that can be reached from this initial state only from $\epsilon$ transitions, unioned again with all the other states that can be reached from the states in the set, and so on, and so on until no other states can be reached, or in other words: $$ \textit{NFA States Set} = \epsilon\textit{-closure}(0) $$ $$ \textit{NFA States Set} = \textit{NFA States Set} \cup \epsilon\textit{-closure}(\textit{NFA States Set}) $$ $$ \textit{NFA States Set} = \textit{NFA States Set} \cup \epsilon\textit{-closure}(\textit{NFA States Set}) $$ $$ \textit{And so on until no other states can be reached}$$
note: $\epsilon\textit{-closure}(s)$’s output is all the states that can be reached from $s$ using only $\epsilon\textit{-transitions}$ alone
- that’s the initial state, how do I find the next NFA States set?
- select a a input character, let’s say
WIP
usable operations of NFA
| Operation | Description |
|---|---|
| $\epsilon\textit{-closure}(s)$ | set of NFA states reachable from NFA state $s$, using only $\epsilon\textit{-transitions}$ alone |
| $\epsilon\textit{-closure}(T)$ | set of NFA states reachable from NFA state in set $T$ on $\epsilon\textit{-transitions}$ alone |
| $\textit{move}(T, a)$ | set of NFA states to which is a transition on input symbol $a$ from state $s$ in $T$ |
WIP
NB: I'm too lazy to write the whole explanation at the moment.
If you really want to understand and the book explanation is too hard to read, just ask me(Ian).
DFA Minimization/Optimization#
Example
DFA graph from the previous example:

Resulting Partition:
$$\Pi = \{A, C\}\{B\}\{D\}\{E\}$$
Resulting Minimized DFA graph

Explanation
Note: Not guaranteed to be correct
based on the transition table of the previous example (with NFA states column removed, it is uselss here),
| Row | DFA state | $a$ | $b$ | $c$ |
|---|---|---|---|---|
| 1 | $A$ | $B$ | $C$ | |
| 2 | $B$ | $B$ | $D$ | |
| 3 | $C$ | $B$ | $C$ | |
| 4 | $D$ | $E$ | ||
| 5 | $E$ |
- separate accepting state as it’s own group. So state $E$ becomes it’s own group
$$\Pi = \{A, B, C, D\}\{E\}$$
- group all the states with the same input character transition, like for example, state $A$ and $C$ both transition to state $B$ on input character $b$ and to state $C$ on input character $a$. So, the partition result becomes;
$$\Pi = \{A, C\}\{B\}\{D\}\{E\}$$
- now turn each group becomes a new state. so for this example, A and C merge into a single state
| Row | DFA state | $a$ | $b$ | $c$ |
|---|---|---|---|---|
| 1 | $A$ | $B$ | $C$ | |
| 2 | $B$ | $B$ | $D$ | |
| 4 | $D$ | $E$ | ||
| 5 | $E$ |
- Using the transition table, make the new minimized DFA.
Ambiguous grammar#
a grammar that produces more than one parse tree for some sentence
Example#
Definition: $$E \Rightarrow E + E | E - E | E \times E | E / E | (E) | id$$
Example $E$ statement:
$$ E + E * E$$
Graph showing how the above expression can be intrepreted into 2 different ways:
