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

Cpp #3

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update first.cpp
  • Loading branch information
SssiiiSssiii committed Apr 17, 2022
commit 46569628281d7896e8588f3109091d9af136e1e2
120 changes: 71 additions & 49 deletions firstSet ( Compiler problem )/first.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstring>
#include <algorithm>
#include <set>
#include <regex>
using namespace std;

int theNubmerOfRules;
Expand All @@ -20,9 +21,9 @@ string getTerminal(string currentRule, int stratFrom) {

if (isspace(currentRule[i]))
break;
terminal += currentRule[i];

terminal += currentRule[i];

}
return terminal;
}
Expand All @@ -42,9 +43,9 @@ string getNonTerminal(string currentRule, int stratFrom) {
}
string firstTerminal(string currentRule, string productionsRules[]) {
string firstSet = "";
char TerminalOrNonTerminal = currentRule[currentRule.find('>')+2];
char TerminalOrNonTerminal = currentRule[currentRule.find('>') + 2];
int IndexOfTerminalOrNonTerminal = currentRule.find('>') + 2;

if (!isupper(TerminalOrNonTerminal)) {
//to guarantee epsilon in the correct pos.
if (currentRule.back() != '$' && TerminalOrNonTerminal == '$') {
Expand All @@ -55,11 +56,11 @@ string firstTerminal(string currentRule, string productionsRules[]) {
return firstSet;
}
else {
string nonTerminal = getNonTerminal(currentRule, IndexOfTerminalOrNonTerminal);
string nonTerminal = getNonTerminal(currentRule, IndexOfTerminalOrNonTerminal);

for (int rule = 0; rule < theNubmerOfRules; rule++) {
string anotherNonTerminal = productionsRules[rule].substr(0, productionsRules[rule].find('-')-1);
string anotherNonTerminal = productionsRules[rule].substr(0, productionsRules[rule].find('-') - 1);

if (nonTerminal == anotherNonTerminal) {
currentRule = productionsRules[rule];
firstSet += firstTerminal(currentRule, productionsRules) + " ";
Expand All @@ -70,63 +71,84 @@ string firstTerminal(string currentRule, string productionsRules[]) {
}
return firstSet;
}
int countNetminalOrNonterminal(string rule, regex x) {

sregex_iterator currentMatch(rule.begin(), rule.end(), x);
sregex_iterator lm;
string out = "";

int c = 0;
while (currentMatch != lm) {
smatch match = *currentMatch;
++c;
currentMatch++;
}
return c;

}
set<string> getFirstSet(string NonTerminal) {

string currentRule;
set<string> firstSet;

bool hasEpsilon = true;

for (int j = 0; j < theNubmerOfRules; j++) {
string anotherNonTerminal = productionsRules[j].substr(0, productionsRules[j].find('-') - 1);
if ( NonTerminal == anotherNonTerminal) {
currentRule = productionsRules[j];
if (NonTerminal == anotherNonTerminal) {
currentRule = productionsRules[j];
regex b("[A-Za-z]+");

//No repetition
if (firstTerminal(currentRule, productionsRules).find("$") != firstTerminal(currentRule, productionsRules).npos && currentRule.size() != 6) {
char TerminalOrNonTerminal = currentRule[currentRule.find('>') + 2];
int IndexOfTerminalOrNonTerminal = currentRule.find('>') + 2;

for (int i = IndexOfTerminalOrNonTerminal; i < currentRule.size(); i++) {
if (!isspace(TerminalOrNonTerminal)) {
// cout << TerminalOrNonTerminal;
if (!isupper(TerminalOrNonTerminal)) {
if (firstSet.count(getTerminal(currentRule, i)) == 0)
firstSet.insert(getTerminal(currentRule, i));
hasEpsilon = false;
i += getTerminal(currentRule, i).size();
TerminalOrNonTerminal = currentRule[i];
}
else {
//to save from override
set<string> save = getFirstSet(getNonTerminal(currentRule, i));
firstSet.insert(save.begin(), save.end());

if (firstSet.count("$") != 0)
firstSet.erase("$");
else
hasEpsilon = false;

i += getNonTerminal(currentRule, i).size();
TerminalOrNonTerminal = currentRule[i + 1];

}
if (firstTerminal(currentRule, productionsRules).find("$") != firstTerminal(currentRule, productionsRules).npos && countNetminalOrNonterminal(currentRule, b) >= 3) {
cout << 55;
char TerminalOrNonTerminal = currentRule[currentRule.find('>') + 2];
int IndexOfTerminalOrNonTerminal = currentRule.find('>') + 2;
for (int i = IndexOfTerminalOrNonTerminal; i < currentRule.size(); i++) {
if (!isspace(TerminalOrNonTerminal)) {
if (!isupper(TerminalOrNonTerminal)) {
if (firstSet.count(getTerminal(currentRule, i)) == 0)
firstSet.insert(getTerminal(currentRule, i));
hasEpsilon = false;
i += getTerminal(currentRule, i).size();
TerminalOrNonTerminal = currentRule[i];

break;
}
else {
//to save from override
set<string> save = getFirstSet(getNonTerminal(currentRule, i));
firstSet.insert(save.begin(), save.end());
if (firstSet.count("$") != 0)
firstSet.erase("$");
else {

hasEpsilon = false;
break;
}
i += getNonTerminal(currentRule, i).size();
if (i < currentRule.size())
TerminalOrNonTerminal = currentRule[i + 1];


}

}
}

else {
hasEpsilon = false;
firstSet.insert(firstTerminal(currentRule, productionsRules));
}

}

else {

hasEpsilon = false;
firstSet.insert(firstTerminal(currentRule, productionsRules));
}

}
}

if (hasEpsilon)
firstSet.insert("$");

return firstSet;

}
Expand All @@ -136,12 +158,12 @@ int main()
cin.ignore();
//get productions rules
for (int i = 0; i < theNubmerOfRules; i++)
getline(cin,productionsRules[i]);
getline(cin, productionsRules[i]);

multiset<string>visited;
for (int rule = 0; rule < theNubmerOfRules; rule++) {
string currentRule = productionsRules[rule];
string nonTerminal = getNonTerminal(currentRule,0);
string nonTerminal = getNonTerminal(currentRule, 0);
visited.insert(nonTerminal);
set<string> firstSet;

Expand Down