Real Number FSA Example

advertisement
Real Number FSA Example
Suppose that we wanted to create a lexical analysis routine that recognized a real number of the form
d+ . d+
and returned the binary equivalent of that real number. We could use an FSA to recognize the syntactic
part of the real number and use semantic routines to compute the binary equivalent of the real number. In
the example above, d represents any digit. d is called an equivalence class since all characters in the class
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } will be treated identically. This allows us to have only one arc signifying the
transition between states in the FSA rather than 10 arcs, one for each character.
Consider the FSA below and convenience yourself that the FSA really does recognize real number of the
form indicated above.
The FSA has the usual transitions. However, there is additional notation on each transition. This extra
notation indicates the semantic routine that will be executed each time that transition is taken. This
semantic routine is a mechanism that allows one to give meaning to the syntactic characters that indicate
which transition to take when.
Let’s take a look at the semantic routines for the FSA above.
1:
a. value ← binary(d)
2:
a. value ← value * 10 + binary(d)
3:
a. λ
4:
a. power ← -1
b. value ← value + binary(d) * 10 ** power
5:
a. power ← power - 1
b. value ← value + binary(d) * 10 ** power
6:
a. return (value)
Download