in Books, Doing Books, Theory of Computation

Theory of Computation #2

Exercise 1.1.2: Write a lexical analyzer directly from the following transition diagram. And from ex3. Build a transition table from the diagram and write a lexical analyzer based on that table.

I use Clojure again and want to write it in an intelligent way. OK, the idea is that I have a function which takes the current state and the next symbol. Depending on the transitions it returns a new state.

I use this format because I can then use it recursively.

I will decode the states in the following matter.

Therefore we have the following transitions:

Thanks to this data structure our function is super easy. The saying about data structures seems to be true:

Smart data structures and dumb code works a lot better than the other way around. — esr

Which you can see looking at our function:

Pretty neat, huh? Lastly, finally some tests to test if our function seem to work:

And the tests successfully passed. Great

Write a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.