Compilers Design: M. T. Bennani Assistant Professor, FST - El Manar University, LISI-INSAT
Compilers Design: M. T. Bennani Assistant Professor, FST - El Manar University, LISI-INSAT
Compilers Design
M. T. Bennani
Assistant Professor, FST - El Manar University, LISI-INSAT
Outline
I Finite automata
I Deterministic Finite Automata (DFAs)
I Non-deterministic Finite Automata (NFAs)
I Implementation
I RegExp ⇒ NFA ⇒ DFA ⇒ Tables
I Exercises
3 / 15
Regular expressions Finite automata Implementation
4 / 15
Regular expressions Finite automata Implementation
Ambiguities
I How much input is used? What if
I X1 ...Xi ∈ L(R) and also
I X1 ...Xk ∈ L(R)
Rule 1
Pick logest possible string in L(R): ”The maximal munch”
algorithm
I Which token is used? What if
I X1 ...Xi ∈ L(Rj ) and also
I X1 ...Xi ∈ L(Rk )
Rule 2
Use rule listed first (j if j < k )
- Treats ”if” as a keyword, not an identifier
5 / 15
Regular expressions Finite automata Implementation
Error Handling
6 / 15
Regular expressions Finite automata Implementation
Definitions
7 / 15
Regular expressions Finite automata Implementation
Notations
I Transition : S1 →a S2
I In state S1 on input ”a” go to state S2
I If end of input and in accepting state ⇒ accept
I Otherwise ⇒ reject
Examples
I Design a finite automaton that accepts only ”1”
I Design a finite automaton that accepts numbers of 1’s
followed by a single 0.
8 / 15
Regular expressions Finite automata Implementation
Automata examples
I Alphabet{0,1}
I What language does this recongnize?
Epsilon moves
I Another kind of transition: ε-moves
I A →ε B
I Machine can move from state A to state B without reading
input
9 / 15
Regular expressions Finite automata Implementation
10 / 15
Regular expressions Finite automata Implementation
Comparaison
I NFAs and DFAs recognize the same set of languages (regular
languages)
I DFSs are faster to execute because there are no choices to
consider
I For a given language NFA can be simpler than DFA
11 / 15
Regular expressions Finite automata Implementation
12 / 15
Regular expressions Finite automata Implementation
13 / 15
Regular expressions Finite automata Implementation
Example
14 / 15
Regular expressions Finite automata Implementation
Note
- NFA to DFA conversion is the heart of the tools such as flex
- But, DFAs can ve huge
- In practive, flex-like tools trade off speed for space in the choice
of NFA and DFA representations
15 / 15