Skip to content

Commit

Permalink
My last fix for left recursion detection didn't worked for any depth,…
Browse files Browse the repository at this point in the history
… this now seems to work in all cases
  • Loading branch information
mingodad committed Jun 11, 2021
1 parent e6a2b21 commit 5a04a9c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,25 +893,25 @@ bool Tab::GrammarCheckAll() {
int errors = 0;
if(!NtsComplete()) ++errors;
if(!AllNtReached()) ++errors;
if(!NoCircularProductions()) ++errors;
if(!NoCircularProductions()) exit(1);
if(!AllNtToTerm()) ++errors;
CheckResolvers(); CheckLL1();
return errors == 0;
}

//--------------- check for circular productions ----------------------

void Tab::GetSingles(const Node *p, TArrayList<Symbol*> &singles, const Node *rule) {
void Tab::GetSingles(const Node *p, TArrayList<Symbol*> &singles) {
if (p == NULL) return; // end of graph
if (p->typ == Node::nt) {
if (p->up || DelGraph(p->next) || p->sym->graph == rule) singles.Add(p->sym);
singles.Add(p->sym);
} else if (p->typ == Node::alt || p->typ == Node::iter || p->typ == Node::opt) {
if (p->up || DelGraph(p->next)) {
GetSingles(p->sub, singles, rule);
if (p->typ == Node::alt) GetSingles(p->down, singles, rule);
GetSingles(p->sub, singles);
if (p->typ == Node::alt) GetSingles(p->down, singles);
}
}
if (!p->up && DelNode(p)) GetSingles(p->next, singles, rule);
if (!p->up && DelNode(p)) GetSingles(p->next, singles);
}

bool Tab::NoCircularProductions() {
Expand All @@ -924,7 +924,7 @@ bool Tab::NoCircularProductions() {
for (i=0; i<nonterminals.Count; i++) {
sym = nonterminals[i];
TArrayList<Symbol*> singles;
GetSingles(sym->graph, singles, sym->graph); // get nonterminals s such that sym-->s
GetSingles(sym->graph, singles); // get nonterminals s such that sym-->s
Symbol *s;
for (int j=0; j<singles.Count; j++) {
s = singles[j];
Expand Down Expand Up @@ -955,7 +955,7 @@ bool Tab::NoCircularProductions() {
for (i=0; i<list.Count; i++) {
n = list[i];
ok = false; errors->count++;
wprintf(_SC(" %") _SFMT _SC(" --> %") _SFMT, n->left->name, n->right->name);
wprintf(_SC(" %") _SFMT _SC(":%d --> %") _SFMT _SC(":%d\n"), n->left->name, n->left->line, n->right->name, n->right->line);
}
for(int i=0; i<list.Count; ++i) delete list[i];
return ok;
Expand Down
2 changes: 1 addition & 1 deletion src/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class Tab {
}
};

void GetSingles(const Node *p, TArrayList<Symbol*> &singles, const Node *rule);
void GetSingles(const Node *p, TArrayList<Symbol*> &singles);
bool NoCircularProductions();

//--------------- check for LL(1) errors ----------------------
Expand Down
5 changes: 4 additions & 1 deletion src/TestSuite/TestCircular_Output.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Coco/R (Dec 01, 2018)
checking
D deletable
A --> B B --> C C --> Atrace output is in trace.txt
A:21 --> B:22
B:22 --> C:23
C:23 --> A:21
trace output is in trace.txt
3 errors detected

0 comments on commit 5a04a9c

Please sign in to comment.