Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammars Assignment #44

Closed
wants to merge 5 commits into from
Closed

Conversation

harrisonnoble
Copy link

Harrison Noble
47475653

Comment on lines +52 to +54
r -> "r" <br>
g -> "g" <br>
b -> "b" <br>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No deduction; just for your information: no need for these last three lines: in the grammar, r, g, and b are already terminals.

Comment on lines +178 to 182
Compile using: g++ -std=c++11 -o fsm fsm.cpp <br>
Run using: ./fsm

You can change the sentences in the "tests" function

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing positive and negative tests. However, as tests they don't quite go far enough. An automated test should both run the code and also verify that the result was what was expected. So, for example, your code could be:

    vector<vector<string> > testSentences {
        {"the", "dog", "ate", "EOI"},
        {"the", "cat", "ate", "EOI"},
        {"the", "smelly", "dog", "ate", "EOI"},
        {"the", "lazy", "cat", "ran", "EOI"},
        {"the", "smelly", "cat", "ate", "noisily", "EOI"},
        {"the", "lazy", "dog", "ate", "slowly", "EOI"},
        {"the", "lazy", "smelly", "smelly", "smelly", "dog", "ate", "EOI"},
        {"the", "lazy", "dog", "barked", "noisily", "EOI"},
        {"the", "dog", "ate"},
        {"the", "EOI"}
    };

    bool expected[] = { true, true, true, true, true, true, true, false, false, false };

    for(int i = 0; i < testSentences.size(); i++){
        auto sentence = testSentences[i];

        if(valid(sentence) == expected[i])
            cout << "OK:     ";
        else
            cout << "FAILED: expected " << expected[i] << "; ";

        for (int i = 0; i < sentence.size(); i++) {
            cout << sentence[i] << " ";
        }
        cout << endl;
    }

-2

And then, just an observation. Clearly, C++ programmers are masochists, but even so... why

void initialization(){
    //create a table with all the valid states and next states
    table.insert(make_pair(make_pair(0, "the"), 1));
    table.insert(make_pair(make_pair(1, "lazy"), 1));
    table.insert(make_pair(make_pair(1, "smelly"), 1));
    table.insert(make_pair(make_pair(1, "dog"), 2));
    table.insert(make_pair(make_pair(1, "cat"), 2));
    table.insert(make_pair(make_pair(2, "ate"), 3));
    table.insert(make_pair(make_pair(2, "ran"), 3));
    table.insert(make_pair(make_pair(3, "slowly"), 4));
    table.insert(make_pair(make_pair(3, "noisily"), 4));
    table.insert(make_pair(make_pair(3, "EOI"), 5));
    table.insert(make_pair(make_pair(4, "EOI"), 5));
}

when you could write

map<pair<int, string>, int> table {
    {{ 0, "the" },     1},
    {{ 1, "lazy" },    1},
    {{ 1, "smelly" },  1},
    {{ 1, "dog" },     2},
    {{ 1, "cat" },     2},
    {{ 2, "ate" },     3},
    {{ 2, "ran" },     3},
    {{ 3, "slowly" },  4},
    {{ 3, "noisily" }, 4},
    {{ 3, "EOI" },     5},
    {{ 4, "EOI" },     5},
};

Just sayin'

@pragdave
Copy link
Owner

pragdave commented Apr 7, 2020

Nice job

@pragdave pragdave closed this Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants