Search
 
SCRIPT & CODE EXAMPLE
 

CPP

find first of a grammar

//Solution by Soumyadeep Ghosh
//LinkedIn : https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
//IG : soumyadepp
// Github : https://github.com/soumyadepp
#include <bits/stdc++.h>
#define NULLTERMINAL "#"
using namespace std;

class Grammar{
    unordered_map<char,vector<string>>rules;
    set<char>terminals;
    set<char>nonTerminals;
    public: 
    Grammar(vector<pair<string,string>>userRules){
        //extract terminals and non terminals from the grammar
        for(auto i : userRules){
            rules[i.first[0]].push_back(i.second);
            nonTerminals.insert(i.first[0]);
            for(auto j : i.second){
                if(isupper(j)){
                    nonTerminals.insert(j);
                }
                else{
                    terminals.insert(j);
                }
            }
        }
        //validate the grammar
        for(auto i : nonTerminals){
            char key = i;
            if(rules[key].size() == 0){
                cout<<"Incomplete grammar
";
                exit(0);
            }
        }
    }
    void showGrammar(){
        for(auto i : rules){
            cout<<i.first<<" : ";
            for(auto j : i.second){
                cout<<j<<" | ";
            }
            cout<<"
";
        }
    }
    void dfs(vector<string>mapped,vector<char>&ans,unordered_map<char,bool>&visited){
        for(auto i : mapped){
            char toBeChecked = i[0];
            if(terminals.find(toBeChecked) != terminals.end()){
                ans.push_back(toBeChecked);
            }
            else{
                if(!visited[toBeChecked]){
                    visited[toBeChecked] = true;
                    dfs(rules[toBeChecked],ans,visited);
                }
            }
        }
    }

    unordered_map<char,vector<char>> getFirst(){
        unordered_map<char,vector<char>>res;
        for(auto i : nonTerminals){
            unordered_map<char,bool>visited;
            vector<char>first;
            visited[i] = true;

            //find the first of corresponding non terminal
            dfs(rules[i],first,visited);
            res[i] = first;
        }
        return res;
    }
    void showTerminals(){
        for(auto i : terminals) if(i != NULLTERMINAL[0]) cout<< i << " ";
        cout<<"
";
    }
    void showNonTerminals(){
        for(auto j : nonTerminals) cout << j << " ";
    }
};

int main(){
    vector<pair<string,string>>vpp = {{"S","AB"},{"S","ac"},{"A","bB"},{"B","c"},{"A",NULLTERMINAL}};
    Grammar grammar(vpp);
    grammar.showGrammar();
    cout<<"Terminals : " << endl;
    grammar.showTerminals();
    cout<<"Non terminals : "<<endl;
    grammar.showNonTerminals();
    cout<<endl;
    unordered_map<char,vector<char>>ans = grammar.getFirst();
    cout << "First of the grammar is : 
";
    for(auto i : ans){
        cout<<i.first<<" : ";
        for(auto j : i.second){
            cout<<j<<" ";
        }
        cout<<"
";
    }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: int to string C++ Using stringstream class 
Cpp :: string append at position c++ 
Cpp :: what is the time complexitry of std::sort 
Cpp :: c++ sorting and keeping track of indexes 
Cpp :: c++ preprocessor commands 
Cpp :: oop in c++ have 5 
Cpp :: c++ program to find gcd of 3 numbers 
Cpp :: queue operations c++ 
Cpp :: c++ create function pointer 
Cpp :: c++ pass function as argument 
Cpp :: phi function 
Cpp :: A Program to check if strings are rotations of each other or not 
Cpp :: find second largest number in array c++ 
Cpp :: compile and run cpp file on mac c++ 
Cpp :: kmp c++ 
Cpp :: c++ bit shift wrap 
Cpp :: rgb type def 
Cpp :: C++ file . 
Cpp :: css window id 
Cpp :: 1822. Sign of the Product of an Array leetcode in c++ 
Cpp :: finding nth most rare element code in c++ 
Cpp :: void setup() { // put your setup code here, to run once:in m}void loop() { // put your main code here, to run repeatedly:} 
Cpp :: vector and algorithm 
Cpp :: c to assembly mips converter 
Cpp :: properties of loop in c++ and how it works 
Cpp :: Missing GL version 
Cpp :: lru cache gfg 
Cpp :: tan trigonometric function 
Cpp :: ue4 c++ bool to text 
Cpp :: set precision on floating numbers 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =