-3

I am constructing a Deterministic Finite Automaton to work as a lexical analyzer for school. Rather than listing every possible state transition in my hashtables, I would like a catch-all reject state if the entry is not found in the table for that state. My program seems to recognize chars that are integers but not chars that are letters. Also, when returning the state to main(), I get a random integer instead of the expected state. Any insight would be appreciated. Here is my chopped-down code to illustrate the issues:

#include <iostream>
#include <unordered_map>

using std::cout;

int calculateNextState(int current_state, char letter);

int main() {
    char key = 'h';
    int current_state = 0;
    int new_state = 0;
    new_state = calculateNextState(current_state, key);
    cout << "The new state is: " << new_state << '\n';
    return 0;
}

int calculateNextState(int current_state, char key) {
    int new_state = current_state;

    std::unordered_map<char, int> hashtable0; 
    hashtable0.emplace('0', 1);
    hashtable0.emplace('1', 1);
    hashtable0.emplace('2', 1);
    hashtable0.emplace('3', 1);
    hashtable0.emplace('4', 1);
    hashtable0.emplace('5', 1);
    hashtable0.emplace('6', 1);
    hashtable0.emplace('7', 1);
    hashtable0.emplace('8', 1);
    hashtable0.emplace('9', 1);

    hashtable0.emplace('.', 1);

    hashtable0.emplace('$', 4);
    hashtable0.emplace('_', 4);
    hashtable0.emplace('a' || 'A', 4);
    hashtable0.emplace('b' || 'B', 4);  
    hashtable0.emplace('c' || 'C', 4);  
    hashtable0.emplace('d' || 'D', 4);  
    hashtable0.emplace('e' || 'E', 4);  
    hashtable0.emplace('f' || 'F', 4);  
    hashtable0.emplace('g' || 'G', 4);  
    hashtable0.emplace('h' || 'H', 4);  
    hashtable0.emplace('i' || 'I', 4);  
    hashtable0.emplace('j' || 'J', 4);  
    hashtable0.emplace('k' || 'K', 4);  
    hashtable0.emplace('l' || 'L', 4);  
    hashtable0.emplace('m' || 'M', 4);  
    hashtable0.emplace('n' || 'N', 4);  
    hashtable0.emplace('o' || 'O', 4);  
    hashtable0.emplace('p' || 'P', 4);  
    hashtable0.emplace('q' || 'Q', 4);  
    hashtable0.emplace('r' || 'R', 4);  
    hashtable0.emplace('s' || 'S', 4);  
    hashtable0.emplace('t' || 'T', 4);  
    hashtable0.emplace('u' || 'U', 4);  
    hashtable0.emplace('v' || 'V', 4);  
    hashtable0.emplace('w' || 'W', 4);  
    hashtable0.emplace('x' || 'X', 4);  
    hashtable0.emplace('y' || 'Y', 4);  
    hashtable0.emplace('z' || 'Z', 4);

    switch(current_state) {
    case 0:
        cout << "The count of the letter 'h': " << hashtable0.count(key) << '\n';
        cout << "The count of an integer: " << hashtable0.count('0') << '\n';
        if(hashtable0.count(key) != 0) //check if key is in hashtable
            new_state = hashtable0[key];
        else
            new_state = -1; //return reject state if not found
        break;

        return new_state;
    }
}

Output:

The count of the letter 'h': 0
The count of an integer: 1
The new state is: 3341764
2
  • hashtable0.emplace('a' || 'A', 4); - are you expecting that this will put the value 4 in both the a and A keys? Feb 1, 2014 at 20:26
  • Yes, it made sense when I did it but I can see now that it doesn't really make sense. I was thinking as emplace as an if statement, apparently.
    – Johnny
    Feb 1, 2014 at 20:31

1 Answer 1

1

That's not working like you think it does:

'a' || 'A'

That's logical OR operator. Both operands will be converted to bool and they both yield true (as they are both non-zero). The result of the expression is true, which converted back to char, yields 1.

Make a test hashtable0.count(1) and it'll return the number of insertions you made this way ('a' || 'A', 'b' || 'B',... they're all the same).

5
  • Any ideas on the other problem I am seeing with the integer return value?
    – Johnny
    Feb 1, 2014 at 20:36
  • Which one? If you mean '0', it seems to be correct.
    – jrok
    Feb 1, 2014 at 20:39
  • @Johnny count returns the number of elements with specific key. If you want to check the mapped value of the key, you need to use operator[]
    – jrok
    Feb 1, 2014 at 20:44
  • @ jrok Right, I understand that. I am only using count to determine if the key is in the table. The returned state is what is what was not updating correctly to the main. It's fine, it's working in my actual program now. The problem with this test code is that it was in the switch case after the break. So should I update my code on SO to what works or leave it all jacked up. I'm not sure what the norm is?
    – Johnny
    Feb 1, 2014 at 20:48
  • Usualy we don't not change questions in a way that invalidates answers. Leave your question as it is, click the checkmark next to an answer that helped you most (if there is one) and post a new question if you still have problems. Sort of like that :) I suggest you read the faq. Good luck.
    – jrok
    Feb 1, 2014 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.