-
Notifications
You must be signed in to change notification settings - Fork 10
/
EbnfAnalyzer.cpp
764 lines (700 loc) · 24.7 KB
/
EbnfAnalyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the EbnfStudio application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "EbnfAnalyzer.h"
#include "EbnfErrors.h"
#include "FirstFollowSet.h"
#include <QtDebug>
// https://stackoverflow.com/questions/19529560/left-recursive-grammar-identification
EbnfAnalyzer::EbnfAnalyzer()
{
}
QSet<QString> EbnfAnalyzer::collectAllTerminalStrings(EbnfSyntax* syn)
{
QSet<QString> res;
foreach( const Ast::Definition* d, syn->getDefs() )
{
res += collectAllTerminalStrings( d->d_node );
}
return res;
}
QStringList EbnfAnalyzer::collectAllTerminalProductions(EbnfSyntax* syn)
{
QStringList res;
foreach( const Ast::Definition* d, syn->getOrderedDefs() )
{
if( d->d_node == 0 && d->d_tok.d_op == EbnfToken::Normal )
res.append( d->d_tok.d_val.toStr() );
}
return res;
}
QSet<QString> EbnfAnalyzer::collectAllTerminalStrings(Ast::Node* node)
{
QSet<QString> res;
if( node == 0 )
return res;
if( node->d_type == Ast::Node::Terminal )
res << node->d_tok.d_val.toStr();
foreach( Ast::Node* sub, node->d_subs )
res += collectAllTerminalStrings( sub );
return res;
}
static inline void resize( EbnfAnalyzer::LlkNodes& l, quint16 curBin )
{
if( l.size() <= curBin )
{
const int max = curBin + 1 - l.size();
for( int i = 0; i < max; i++ )
l.append( Ast::NodeRefSet() );
}
}
static inline int countNotEmpty( const EbnfAnalyzer::LlkNodes& l )
{
int res = 0;
foreach( const Ast::NodeRefSet& s, l )
if( !s.isEmpty() )
res++;
return res;
}
static QByteArray ws( int level )
{
QByteArray res;
for( int i = 0; i < qAbs(level); i++ )
res += " | ";
return res;
}
struct _SubNodeBin
{
const Ast::Node* d_node;
quint16 d_from, d_to;
_SubNodeBin( const Ast::Node* n, int from, int to ):d_node(n),d_from(from),d_to(to){}
};
quint16 EbnfAnalyzer::calcLlkFirstSetImp(quint16 k, quint16 curBin, LlkNodes& res, const Ast::Node* node, FirstFollowSet* tbl, int level)
{
// Gehe entlang der Blätter des Baums und sammle alle Terminals ein gruppiert in Distanzboxen.
// Wird eigentlich nur mit node=Sequence aufgerufen, da ja Predicates nur dort vorkommen
// returns maximum number of symbols covered by this node
if( node == 0 || node->doIgnore() )
return 0;
//#ifdef _DEBUG
#if 0
qDebug() << ws(level).constData() << "visit bin" << curBin << "l/c" <<
node->d_tok.d_lineNr << node->d_tok.d_colNr << node->toString()
<< "level" << level << "k" << k << "reslen" << res.size(); // TEST
#endif
if( level > k * 10 )
{
#ifdef _DEBUG
qCritical() << ws(level).constData() << "calcLlkFirstSet level depth limit hit at " << level << k * 10;
#endif
return 0;
}
switch( node->d_type )
{
case Ast::Node::Terminal:
resize( res, curBin );
res[curBin].insert( node ); // hier ist dieser node gemeint, nicht Follow(node)!
//#ifdef _DEBUG
#if 0
qDebug() << ws(level).constData() << "insert" << curBin <<
node->d_tok.d_val.toBa() << node->d_tok.d_lineNr << node->d_tok.d_colNr; // TEST
#endif
return 1;
case Ast::Node::Nonterminal:
if( node->d_def && node->d_def->d_node )
return calcLlkFirstSetImp( k, curBin, res, node->d_def->d_node, tbl,level+1 );
else
{
// wie Terinal
resize( res, curBin );
res[curBin].insert( node );
//#ifdef _DEBUG
#if 0
qDebug() << ws(level).constData() << "insert" << curBin <<
node->d_tok.d_val.toBa() << node->d_tok.d_lineNr << node->d_tok.d_colNr; // TEST
#endif
return 1;
}
case Ast::Node::Sequence:
#if 0
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
calcLlkFirstSet( k, curBin++, res, sub, tbl,level+1 );
if( res.size() >= k )
break;
}
// TODO: repetitions and options
#else
{
QList<_SubNodeBin> toVisit;
int i = curBin;
int nullable = 0;
foreach( Ast::Node* sub, node->d_subs )
{
if( !sub->doIgnore() )
{
toVisit << _SubNodeBin(sub,i-nullable,i);
i++;
if( sub->isNullable() )
nullable++;
}
}
int max = 0;
for( int i = 0; i < toVisit.size(); i++ )
{
for( int bin = toVisit[i].d_from; bin <= toVisit[i].d_to && bin < k; bin++ )
{
const quint16 tmp = calcLlkFirstSetImp( k, bin, res, toVisit[i].d_node, tbl, level+1 );
if( tmp > 1 )
{
for( int j = i+1; j < toVisit.size(); j++ )
toVisit[j].d_to += tmp - 1;
}
if( tmp > 0 && bin > max )
max = bin;
}
}
return max - curBin + 1;
}
// TODO: repetition
#endif
break;
case Ast::Node::Alternative:
{
quint16 count = 0;
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
const quint16 tmp = calcLlkFirstSetImp( k, curBin, res, sub, tbl,level+1 );
if( tmp > count )
count = tmp;
}
return count;
}
default:
break;
}
return 0;
}
void EbnfAnalyzer::calcLlkFirstSet2Imp(quint16 k, int curBin, int level, LlkNodes& res,
const Ast::Node* node, FirstFollowSet* tbl, CheckSet& visited)
{
if( node == 0 || node->doIgnore() )
return;
if( visited.contains(node) )
return;
else
visited.insert(node);
//#ifdef _DEBUG
#if 0
qDebug() << ws(level).constData() << "visit bin" << curBin << "l/c" <<
node->d_tok.d_lineNr << node->d_tok.d_colNr << node->toString()
<< "level" << level << "k" << k << "res len" << res.size(); // TEST
#endif
// TODO: ev. separate Funktion um von einem Node zum nächsten zu wandern und im Falle von usedBy und Alternative
// mehrere parallele Nodes zurückzugeben. Offen ist die Ermittlung des curBin.
if( level >= 0 )
{
// CheckSet visited; // don't check visited downwards
switch( node->d_type )
{
case Ast::Node::Terminal:
resize( res, curBin );
res[curBin].insert( node ); // hier ist dieser node gemeint, nicht Follow(node)!
//#ifdef _DEBUG
#if 0
qDebug() << ws(level).constData() << "insert" << curBin <<
node->d_tok.d_val.data() << node->d_tok.d_lineNr << node->d_tok.d_colNr; // TEST
#endif
break;
case Ast::Node::Nonterminal:
if( node->d_def && node->d_def->d_node )
calcLlkFirstSet2Imp( k, curBin, level + 1, res, node->d_def->d_node, tbl, visited );
else
{
// wie Terinal
resize( res, curBin );
res[curBin].insert( node );
//#ifdef _DEBUG
#if 0
qDebug() << ws(level).constData() << "insert" << curBin <<
node->d_tok.d_val.data() << node->d_tok.d_lineNr << node->d_tok.d_colNr; // TEST
#endif
}
break;
case Ast::Node::Sequence:
#if 1
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
calcLlkFirstSet2Imp( k, curBin++, level + 1, res, sub, tbl, visited );
if( res.size() >= k )
break;
}
// TODO: repetition, option
#else
#if 1 // TODO the following does not work yet
{
QList<const Ast::Node*> toVisit;
foreach( Ast::Node* sub, node->d_subs )
{
if( !sub->doIgnore() )
toVisit << sub;
}
for( int i = curBin; i < k && i < toVisit.size(); i++ )
{
for( int j = i; j < toVisit.size(); j++ )
{
visited.clear();
calcLlkFirstSet2Imp( k, i, level + 1, res, toVisit[j], tbl, visited ); // TODO: visited too strong
if( !toVisit[j]->isNullable() )
break;
}
if( res.size() >= k )
break;
}
}
// TODO: repetition
#else
for( int i = curBin; i < k; i++ )
{
QSet<const Ast::Node*> check = visited;
int j = 0;
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
if( j++ < i )
continue;
calcLlkFirstSet2Imp( k, i, level + 1, res, sub, tbl, check );
if( !sub->isNullable() )
break;
}
//if( res.size() >= k )
// break;
}
// TODO: repetition
#endif
#endif
break;
case Ast::Node::Alternative:
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
calcLlkFirstSet2Imp( k, curBin, level + 1, res, sub, tbl, visited );
}
// TODO: repetition
break;
default:
break;
}
}
// Vermutlich unnötig, auch nach oben zu suchen
if( level <= 0 && res.size() < k )
{
// Vorsicht, es kann sein dass wir beim Weg nach oben wieder dort vorbeikommen, wo man angefangen haben
curBin = res.size() - 1;
if( const Ast::Node* next = node->getNext(&curBin) )
{
// finde nächsten nach node
calcLlkFirstSet2Imp(k,curBin,0,res,next,tbl, visited);
if( res.size() >= k )
return;
}else
{
// gehe entlang node->d_owner->d_usedBy
foreach( const Ast::Node* use, node->d_owner->d_usedBy )
{
int index = curBin;
const Ast::Node* next = use->getNext(&index);
if( next )
calcLlkFirstSet2Imp( k, index, 0, res, next, tbl, visited );
else // obsolet wegen visited: if( level > -10 ) // begrenze die Tiefe, da es hier ab und zu unendlich weitergeht TODO
calcLlkFirstSet2Imp( k,index,level - 1 , res, use, tbl, visited );
}
}
}
}
bool EbnfAnalyzer::findPath(Ast::ConstNodeList& path, const Ast::Node* to)
{
if( path.isEmpty() )
return false;
const Ast::Node* node = path.last();
if( node->doIgnore() )
return false;
switch( node->d_type )
{
case Ast::Node::Terminal:
if( node == to )
return true;
break;
case Ast::Node::Nonterminal:
if( node->d_def && node->d_def->d_node )
{
if( path.contains(node->d_def->d_node) )
return false;
path.append(node->d_def->d_node);
if( findPath( path, to ) )
return true;
path.pop_back();
}else if( node == to )
{
// wie Terinal
return true;
}
break;
case Ast::Node::Sequence:
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
path.append(sub);
if( findPath( path, to ) )
return true;
path.pop_back();
if( !sub->isNullable() )
break;
}
break;
case Ast::Node::Alternative:
foreach( Ast::Node* sub, node->d_subs )
{
if( sub->doIgnore() )
continue;
path.append(sub);
if( findPath( path, to ) )
return true;
path.pop_back();
}
break;
default:
break;
}
if( path.size() == 1 )
{
// Wir sind zuoberst und haben noch nichts gefunden
if( const Ast::Node* next = node->getNext() )
{
// finde nächsten nach node
path.append(next);
if( findPath( path, to ) )
return true;
path.pop_back();
}else
{
// gehe entlang node->d_owner->d_usedBy
foreach( const Ast::Node* use, node->d_owner->d_usedBy )
{
const Ast::Node* next = use->getNext();
if( next )
path.append(next);
else
path.append(use);
if( findPath( path, to ) )
return true;
path.pop_back();
}
}
}
return false;
}
Ast::NodeRefSet EbnfAnalyzer::intersectAll(const EbnfAnalyzer::LlkNodes& lhs, const EbnfAnalyzer::LlkNodes& rhs)
{
if( lhs.size() != rhs.size() )
return Ast::NodeRefSet();
int intersects = 0;
Ast::NodeRefSet sum;
for( int i = 0; i < lhs.size(); i++ )
{
const Ast::NodeRefSet diff = lhs[i] & rhs[i];
if( ( lhs[i].isEmpty() && rhs[i].isEmpty() ) || !diff.isEmpty() )
{
intersects++;
//sum += diff;
sum = diff; // behalte nur das letzte Diff
}
}
if( intersects == lhs.size() )
return sum;
else
return Ast::NodeRefSet();
}
void EbnfAnalyzer::calcLlkFirstSet(quint16 k, EbnfAnalyzer::LlkNodes& res, const Ast::Node* node, FirstFollowSet* tbl)
{
calcLlkFirstSetImp( k, 0, res, node, tbl, 0 );
}
void EbnfAnalyzer::calcLlkFirstTree(quint16 k, Ast::NodeTree* nt, const Ast::Node* node, FirstFollowSet* tbl)
{
// TODO: this is unfinished work; the goal is to have a reliable LL(k) algorithm which works better than
// the current \LL:k\, which is only a primitive approximation
if( nt == 0 || node == 0 || node->doIgnore() || nt->d_k >= k )
return;
switch( node->d_type )
{
case Ast::Node::Terminal:
nt->addLeaf(node);
return;
case Ast::Node::Nonterminal:
if( node->d_def && node->d_def->d_node )
{
calcLlkFirstTree(k, nt, node->d_def->d_node, tbl );
}else
// wie Terinal
nt->addLeaf(node);
return;
case Ast::Node::Sequence:
for( int i = 0; i < node->d_subs.size(); i++ )
{
calcLlkFirstTree(k, nt, node->d_subs[i], tbl);
}
return;
case Ast::Node::Alternative:
for( int i = 0; i < node->d_subs.size(); i++ )
{
calcLlkFirstTree(k, nt, node->d_subs[i], tbl);
}
return;
default:
break;
}
}
void EbnfAnalyzer::calcLlkFirstSet2(quint16 k, EbnfAnalyzer::LlkNodes& res, const Ast::Node* node, FirstFollowSet* tbl)
{
QSet<const Ast::Node*> visited;
calcLlkFirstSet2Imp( k, 0, 0,res, node, tbl, visited );
}
void EbnfAnalyzer::checkForAmbiguity(FirstFollowSet* set, EbnfErrors* err)
{
EbnfSyntax* syn = set->getSyntax();
for( int i = 0; i < syn->getOrderedDefs().size(); i++ )
{
const Ast::Definition* d = syn->getOrderedDefs()[i];
if( d->doIgnore() || ( i != 0 && d->d_usedBy.isEmpty() ) || d->d_node == 0 )
continue;
try
{
checkForAmbiguity( d->d_node, set, err );
}catch(...)
{
qCritical() << "EbnfAnalyzer::checkForAmbiguity exception";
}
}
}
void EbnfAnalyzer::checkForAmbiguity(Ast::Node* node, FirstFollowSet* set, EbnfErrors* errs , bool recursive)
{
if( node == 0 || node->doIgnore() )
return;
findAmbiguousAlternatives(node,set,errs);
findAmbiguousOptionals(node,set,errs);
if( !recursive )
return;
switch( node->d_type )
{
case Ast::Node::Sequence:
case Ast::Node::Alternative:
foreach( Ast::Node* sub, node->d_subs )
{
checkForAmbiguity( sub, set, errs, recursive );
}
break;
default:
break;
}
}
Ast::ConstNodeList EbnfAnalyzer::findPath(const Ast::Node* from, const Ast::Node* to)
{
Ast::ConstNodeList res;
if( from == 0 )
return res;
res << from;
if( !findPath( res, to ) )
res.clear();
return res;
}
void EbnfAnalyzer::findAmbiguousAlternatives(Ast::Node* node, FirstFollowSet* set, EbnfErrors* errs)
{
if( node->d_type != Ast::Node::Alternative )
return;
// Check each pair of alternatives
for(int i = 0; i < node->d_subs.size(); i++)
{
for(int j = i + 1; j < node->d_subs.size(); j++)
{
const Ast::Node* a = node->d_subs[i];
const Ast::Node* b = node->d_subs[j];
if( a->doIgnore() || b->doIgnore() )
continue;
// TODO: wenn eine Alternative Nullable ist, müsste auch noch ihre Follow mitberücksichtigt werden!
const Ast::NodeRefSet diff = set->getFirstSet(a) & set->getFirstSet(b);
if( diff.isEmpty() )
continue;
const Ast::Node* predA = EbnfSyntax::firstPredicateOf(a);
const Ast::Node* predB = EbnfSyntax::firstPredicateOf(b);
int ll = 0;
if( predA != 0 )
ll = predA->getLlk();
if( predB != 0 )
ll = qMax( ll, predB->getLlk() );
// TODO: each alternative might have a different predicate type LL or LA
// currently just assume everything is ok if an LA predicate is present
if( (predA && !predA->getLa().isEmpty()) || (predB && !predB->getLa().isEmpty()) )
continue;
if( ll > 0 )
{
EbnfAnalyzer::LlkNodes llkA;
calcLlkFirstSet2( ll, llkA, a, set );
EbnfAnalyzer::LlkNodes llkB;
calcLlkFirstSet2( ll, llkB, b, set );
const Ast::NodeRefSet diff = intersectAll( llkA, llkB );
if( llkA.size() == llkB.size() && llkA.size() == ll && diff.isEmpty() )
continue;
const Ast::Node* pred = predA != 0 ? predA : predB;
const Ast::Node* other = predA != 0 ? b : a;
if( pred )
errs->warning(EbnfErrors::Analysis, pred->d_tok.d_lineNr, pred->d_tok.d_colNr,
QString("predicate not effective for LL(%1)").arg(ll),
QVariant::fromValue(EbnfSyntax::IssueData(EbnfSyntax::IssueData::BadPred,
pred,other)));
}
if( !diff.isEmpty() )
{
const Ast::Node* aa = EbnfSyntax::firstVisibleElementOf(a);
Ast::NodeSet diff2 = EbnfSyntax::collectNodes( diff, set->getFirstNodeSet(a) );
diff2 += EbnfSyntax::collectNodes( diff, set->getFirstNodeSet(b) );
errs->error(EbnfErrors::Analysis, aa->d_tok.d_lineNr, aa->d_tok.d_colNr,
QString("alternatives %1 and %2 are LL(1) ambiguous because of %3")
.arg(i+1).arg(j+1).arg(EbnfSyntax::pretty(diff)),
QVariant::fromValue(EbnfSyntax::IssueData(
EbnfSyntax::IssueData::AmbigAlt,a,b,diff2.toList())));
}
}
}
}
void EbnfAnalyzer::findAmbiguousOptionals(Ast::Node* seq, FirstFollowSet* set, EbnfErrors* errs)
{
// ZeroOrOne and ZeroOrMore haben genau dieselben Mehrdeutigkeitskriterien. Es ist egal, ob
// a nur ein oder mehrmals wiederholt wird; in jedem Fall ist die Schnittmenge zwischen First(a) und Follow(a)
// massgebend!
if( seq->d_type != Ast::Node::Sequence )
return;
Ast::NodeRefSet upperFollow = set->getFollowSet(seq);
for(int i = 0; i < seq->d_subs.size(); i++)
{
const Ast::Node* a = seq->d_subs[i];
if( a->doIgnore() || !a->isNullable() )
continue;
Ast::NodeRefSet follow; // = set->getFollowSet(1,a);
bool nonNullableFound = false;
const Ast::Node* b = 0;
for( int j = i + 1; j < seq->d_subs.size(); j++ )
{
const Ast::Node* n = seq->d_subs[j];
if( n->doIgnore() )
continue;
if( b == 0 )
b = n;
follow += set->getFirstSet(n);
if( !n->isNullable() )
{
nonNullableFound = true;
break;
}
}
if( !nonNullableFound )
follow += upperFollow;
const Ast::NodeRefSet diff = set->getFirstSet(a) & follow;
if( diff.isEmpty() )
continue;
// else
const Ast::Node* pred = EbnfSyntax::firstPredicateOf(a);
int ll = 0;
if( pred != 0 )
{
// TODO: currently just assume everything is ok if an LA predicate is present
if( !pred->getLa().isEmpty() )
continue;
ll = pred->getLlk();
if( ll > 0 )
{
EbnfAnalyzer::LlkNodes llkA;
calcLlkFirstSet2( ll, llkA, a, set );
EbnfAnalyzer::LlkNodes llkB;
if( b == 0 )
{
QSet<const Ast::Node*> visited;
calcLlkFirstSet2Imp(ll,0,-1,llkB, seq, set, visited );
}else
calcLlkFirstSet2( ll, llkB, b, set );
const Ast::NodeRefSet diff = intersectAll( llkA, llkB );
if( llkA.size() == llkB.size() && llkA.size() == ll && diff.isEmpty() )
continue;
errs->warning(EbnfErrors::Analysis, pred->d_tok.d_lineNr, pred->d_tok.d_colNr,
QString("predicate not effective for LL(%1)").arg(ll),
QVariant::fromValue(EbnfSyntax::IssueData(
EbnfSyntax::IssueData::BadPred,pred, b ? b : seq)) );
}
}
Ast::NodeSet diff2 = EbnfSyntax::collectNodes( diff, set->getFirstNodeSet(a) );
if( b != 0 )
diff2 += EbnfSyntax::collectNodes( diff, set->getFirstNodeSet(b) );
// if( diff.size() != diff2.size() )
// qDebug() << "findAmbigOptions different diff size";
reportAmbig( seq, i, diff, diff2, set, errs );
}
}
void EbnfAnalyzer::reportAmbig(Ast::Node* sequence, int ambigIdx, const Ast::NodeRefSet& ambigSet,
const Ast::NodeSet& ambigSet2, FirstFollowSet* set, EbnfErrors* errs)
{
Ast::Node* a = sequence->d_subs[ambigIdx];
const Ast::Node* start = EbnfSyntax::firstVisibleElementOf(sequence);
QString ofSeq;
if( start && start != a )
ofSeq = QString("start. w. '%1' ").arg(start->d_tok.d_val.toStr());
const Ast::Node* next = 0;
bool fullAmbig = false;
for( int j = ambigIdx + 1; j < sequence->d_subs.size(); j++ )
{
const Ast::NodeRefSet diffDiff = ambigSet & set->getFirstSet(sequence->d_subs[j]);
if( !diffDiff.isEmpty() )
{
if( diffDiff == ambigSet )
fullAmbig = true;
next = EbnfSyntax::firstVisibleElementOf(sequence->d_subs[j]);
break;
}
}
QString ambig = "w. successors";
if( next )
{
QString dots;
if( !fullAmbig )
dots = "...";
ambig = QString("w. '%1'%2 ").arg(next->d_tok.d_val.toStr()).arg(dots);
}else
next = sequence;
errs->warning(EbnfErrors::Analysis, a->d_tok.d_lineNr, a->d_tok.d_colNr,
QString("opt. elem. %1 of seq. %2is LL(1) ambig. %3 because of %4")
.arg(ambigIdx+1).arg(ofSeq).arg(ambig).arg(EbnfSyntax::pretty(ambigSet)),
QVariant::fromValue(EbnfSyntax::IssueData(
EbnfSyntax::IssueData::AmbigOpt,a,next,ambigSet2.toList())));
}