From 5a04a9c66d274c3f6409fb3afc51a98d7c33dffa Mon Sep 17 00:00:00 2001 From: mingodad Date: Fri, 11 Jun 2021 14:55:36 +0200 Subject: [PATCH] My last fix for left recursion detection didn't worked for any depth, this now seems to work in all cases --- src/Tab.cpp | 16 ++++++++-------- src/Tab.h | 2 +- src/TestSuite/TestCircular_Output.txt | 5 ++++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Tab.cpp b/src/Tab.cpp index 1d6d81a..0ab9fa4 100644 --- a/src/Tab.cpp +++ b/src/Tab.cpp @@ -893,7 +893,7 @@ 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; @@ -901,17 +901,17 @@ bool Tab::GrammarCheckAll() { //--------------- check for circular productions ---------------------- -void Tab::GetSingles(const Node *p, TArrayList &singles, const Node *rule) { +void Tab::GetSingles(const Node *p, TArrayList &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() { @@ -924,7 +924,7 @@ bool Tab::NoCircularProductions() { for (i=0; i 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; jcount++; - 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 &singles, const Node *rule); + void GetSingles(const Node *p, TArrayList &singles); bool NoCircularProductions(); //--------------- check for LL(1) errors ---------------------- diff --git a/src/TestSuite/TestCircular_Output.txt b/src/TestSuite/TestCircular_Output.txt index 170f918..08079e1 100644 --- a/src/TestSuite/TestCircular_Output.txt +++ b/src/TestSuite/TestCircular_Output.txt @@ -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