The implementation for integer float hex

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: appears in the rows of the DFA). So given the following DFA, if we want to create the state machine to implement the lexer for integer, float, and ID, the table will look as follows, where S0 is the starting state, S1 is the Integer state, S2 is the intermediate state, S3 is the Float state, S4 is the ID state. 0- 9 \. $ _ A- Z S0 - Start S1 S2 S4 S4 S4 S1 - Integer S1 S2 S2 – Intermediate S3 S3 - Float S3 S4 - ID S4 S4 S4 S4 The cells in gray represent errors, it means combinations of a current state and an input that are not causing a valid state. Now the problem is how to implement this table in code. There are different ways to implement this table; one of it (used by the professor) is using a hash table, where given the current state and an input, the hash table return the next state or an error if this is the case. One possible way to implement this is to: 0. Create the table (using a data structure). 1. Define which state is the starting state. 2. Use a loop to go through the table and obtain the next state given the current state and an input (a value to change from one state to another). If we want to add the representation of Hexadecimals, Octal, or Binary numbers what we need to do is to add more rows and columns in the table, but the code does not change. So, adding new states generates more complexity in the table, but not in the code. In order to add these new patterns we need to know how those numbers are represented. For example: 1. Binaries numbers could be represented by a string that starts with the prefix “0b” and followed by a sequence of 0’s and 1’s, e.g., 0b0110. 2. Hexadecimal numbers could be represented by a string that starts with the prefix “0x” followed by a sequence of hexadecimal symbols [0- 9, A- F], e.g., 0xFF. 3. Integer is a sequence of [0- 9]. In some languages such as Java or C/C++ integers cannot start with a 0, e.g. 758. 4. Octal numbers are represented with a sequence of numbers [0- 7]. In some languages such as Java or C/C++, this sequence always starts with “0”, e.g., 075. Note that in our project a number that starts with a 0 could be recognized as a...
View Full Document

This note was uploaded on 09/29/2013 for the course CSE 340 taught by Professor Richa during the Spring '13 term at Arizona State University.

Ask a homework question - tutors are online