forked from kdt3rd/lime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grammar.cpp
810 lines (616 loc) · 18 KB
/
Grammar.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/// @file Grammar.cpp
/// @brief File that declares...
///
///
//
//
// Copyright © 2003-2013 Kimball Thurston
//
// This program is free software; you can redisribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <iosfwd>
#include "Grammar.h"
#include "Error.h"
#include "Rule.h"
#include "RuleTable.h"
#include "Symbol.h"
#include "SymbolTable.h"
#include "Config.h"
#include "State.h"
#include "StateTable.h"
#include "Util.h"
#include "Version.h"
////////////////////////////////////////
Grammar::Grammar( void )
: myBasisOnly( false ), myCompressActions( true ), myNoActions( false ),
myDebugOutput( false ), myQuiet( true ), myStats( false ),
myLanguage( LanguageDriver::CPP ), myNumConflicts( 0 )
{
SymbolTable::get()->findOrCreate("$");
SymbolTable::get()->addDefault("{default}");
myErrSym = SymbolTable::get()->findOrCreate("error");
myErrSym->setDataType( "int" );
}
////////////////////////////////////////
Grammar::~Grammar( void )
{
}
////////////////////////////////////////
void
Grammar::setSourceFile( const char *sourceFile )
{
mySourceFile = sourceFile;
}
////////////////////////////////////////
void
Grammar::setOutputDirectory( const char *outputDir )
{
myOutputDir = outputDir;
}
////////////////////////////////////////
void
Grammar::setLanguage( LanguageDriver::Language lang )
{
myLanguage = lang;
}
////////////////////////////////////////
bool
Grammar::setValue( const std::string &name,
const std::string &value,
int line )
{
bool retval = true;
if ( mySettings.find( name ) == mySettings.end() )
mySettings[ name ] = ValueSetting( value, line );
else
retval = false;
return retval;
}
////////////////////////////////////////
void
Grammar::process( void )
{
RuleTable::get()->findPrecedences();
/// Compute the lambda-nonterminals and the first-sets for every
/// nonterminal
findFirstSets();
// Compute all LR(0) states. Also record follow-set propagation
// links so that the follow-set can be computed later
findStates();
// Tie up loose ends on the propagation links
findLinks();
// Compute the follow set of every reducible configuration
findFollowSets();
// Compute the action tables
findActions();
// Compress the action tables
if ( isCompressActions() )
compressTables();
// Generate a report of the parser generated. (the "y.output" file) */
if ( ! isQuiet() )
reportOutput();
// Generate the source code for the parser
outputFiles();
}
////////////////////////////////////////
int
Grammar::getNumConflicts( void )
{
return myNumConflicts;
}
////////////////////////////////////////
void
Grammar::reprintGrammar( void ) const
{
SymbolTable *symTable = SymbolTable::get();
size_t i, n;
std::cout << "// Reprint of input file \"" << mySourceFile
<< "\".\n// Symbols:\n";
n = symTable->getNumSymbols();
for ( i = 0; i < n; ++i )
{
std::cout << "// " << i << " " << symTable->getNthSymbol(i)->getName()
<< std::endl;
}
std::cout << std::endl;
n = RuleTable::get()->getNumRules();
for ( i = 0; i < n; ++i )
{
Rule *rp = RuleTable::get()->getNthRule( i );
std::cout << rp->getLHS() << " ::=";
Rule::RHSListConstIter ri, re;
ri = rp->getRHS().begin();
re = rp->getRHS().end();
for ( ; ri != re; ++ri )
{
std::cout << " " << (*ri).first;
}
std::cout << ".";
if ( ! rp->getPrecedence().empty() )
std::cout << " [" << rp->getPrecedence() << "]";
std::cout << std::endl;
}
}
////////////////////////////////////////
void
Grammar::printStats( void ) const
{
size_t nt = SymbolTable::get()->getNumTerminals();
size_t ns = SymbolTable::get()->getNumSymbols();
std::cout << "Grammar statistics: " << nt << " terminals, "
<< ns - nt << " nonterminals, "
<< RuleTable::get()->getNumRules() << " rules" << std::endl;
std::cout << " " << StateTable::get()->getNumStates()
<< " states, " << 0 << " parser table entries, "
<< myNumConflicts << " conflicts" << std::endl;
}
////////////////////////////////////////
/// Find all nonterminals which will generate the empty string.
/// Then go back and compute the first sets of every nonterminal.
/// The first set is the set of all terminal symbols which can begin
/// a string generated by that nonterminal.
void
Grammar::findFirstSets( void )
{
// First compute all lambdas
RuleTable::get()->computeLambdas();
// Now compute the first sets
RuleTable::get()->computeFirstSets();
}
////////////////////////////////////////
void
Grammar::findStates( void )
{
Symbol *startSym = getStartSymbol( true );
if ( ! startSym )
{
Error::get()->add( "No rules to choose as start rule." );
return;
}
if ( RuleTable::get()->isOnRightSide( startSym->getName() ) )
{
Error::get()->add( "The start symbol \"%s\" occurs on the "
"right-hand side of a rule. This will result "
"in a parser which does not work properly.",
startSym->getName().c_str() );
}
myCurConfigList.reset();
Rule *startRule = RuleTable::get()->findFirstRule( startSym->getName() );
while ( startRule )
{
Config *tmpCfg = myCurConfigList.addWithBasis( startRule, 0 );
// All start rules have the start symbol as their left hand side
// tmpCfg->addFollowSet( startSym->getName() );
tmpCfg->addFollowSet( SymbolTable::get()->getNthSymbol( 0 )->getName() );
startRule = RuleTable::get()->getNextRule( startRule );
}
getNextState();
}
////////////////////////////////////////
void
Grammar::findLinks( void )
{
// Convert all backlinks into forward links. Only the forward
// links are used in the follow-set computation.
size_t i, N;
N = StateTable::get()->getNumStates();
for ( i = 0; i < N; ++i )
{
State *stp = StateTable::get()->getNthState( i );
for ( Config *cfp = stp->getConfig(); cfp; cfp = cfp->getNext() )
{
// little bit of housekeeping (for debugging)
cfp->setState( stp );
const Config::PropList &bp = cfp->getBackwardPropLinks();
Config::PropListConstIter pi, pe;
pe = bp.end();
for ( pi = bp.begin(); pi != pe; ++pi )
(*pi)->addForwardPropLink( cfp );
}
}
}
////////////////////////////////////////
void
Grammar::findFollowSets( void )
{
size_t i, N;
N = StateTable::get()->getNumStates();
for ( i = 0; i < N; ++i )
{
State *stp = StateTable::get()->getNthState( i );
for ( Config *cfp = stp->getConfig(); cfp; cfp = cfp->getNext() )
cfp->setStatus( Config::INCOMPLETE );
}
bool progress = false;
do
{
progress = false;
for ( i = 0; i < N; ++i )
{
State *stp = StateTable::get()->getNthState( i );
for ( Config *cfp = stp->getConfig(); cfp; cfp = cfp->getNext() )
{
if ( cfp->getStatus() == Config::COMPLETE )
continue;
const Config::PropList &plp = cfp->getForwardPropLinks();
Config::PropListConstIter pi, pe;
pe = plp.end();
for ( pi = plp.begin(); pi != pe; ++pi )
{
if ( (*pi)->combineFollowSet( cfp->getFollowSet() ) )
{
(*pi)->setStatus( Config::INCOMPLETE );
progress = true;
}
}
cfp->setStatus( Config::COMPLETE );
}
}
} while ( progress );
}
////////////////////////////////////////
void
Grammar::findActions( void )
{
size_t i, j, nState, nSym, nRule;
State *stp;
nState = StateTable::get()->getNumStates();
nSym = SymbolTable::get()->getNumSymbols();
// Find all reduce actions...
for ( i = 0; i < nState; ++i )
{
stp = StateTable::get()->getNthState( i );
for ( Config *cfp = stp->getConfig(); cfp; cfp = cfp->getNext() )
{
// Check if dot at extreme right
if ( size_t( cfp->getDot() ) == cfp->getRule()->getRHS().size() )
{
for ( j = 0; j < nSym; ++j )
{
Symbol *sp = SymbolTable::get()->getNthSymbol( j );
if ( ( Symbol::TERMINAL == sp->getType() ||
sp->getName() == "$" ) &&
cfp->getFollowSet().isSet( sp->getName() ) )
{
stp->addAction( Action::REDUCE, sp->getName(),
0, cfp->getRule() );
}
}
}
}
}
Symbol *startSym = getStartSymbol();
stp = StateTable::get()->getNthState( 0 );
// Add the accepting token to the first state which is always the
// starting state of the finite state machine as an action to accept
// if the lookahead is the start nonterminal.
if ( startSym && stp )
stp->addAction( Action::ACCEPT, startSym->getName(), 0, 0 );
// Resolve conflicts
for ( i = 0; i < nState; ++i )
{
stp = StateTable::get()->getNthState( i );
// Check for SHIFT-SHIFT conflicts first (nothing we can do to resolve)
for ( Config *cfp = stp->getConfig(); cfp; cfp = cfp->getNext() )
{
if ( ! cfp->getForwardPropLinks().empty() )
continue;
Rule *curRule = cfp->getRule();
if ( size_t( cfp->getDot() ) == curRule->getRHS().size() )
continue;
for ( Config *next = stp->getConfig(); next; next = next->getNext() )
{
if ( next == cfp )
continue;
Rule *nextRule = next->getRule();
if ( size_t( next->getDot() ) == nextRule->getRHS().size() )
continue;
if ( curRule->getRHS()[cfp->getDot()].first ==
nextRule->getRHS()[next->getDot()].first )
{
std::cout << "Unresolved SHIFT-SHIFT conflict between:\n"
<< std::endl;
curRule->print( std::cout );
std::cout << std::endl << "\nand\n" << std::endl;
nextRule->print( std::cout );
std::cout << std::endl << std::endl;
++myNumConflicts;
}
}
}
stp->sortActions();
ActionList &ap = stp->getActions();
size_t nAct = ap.getNumActions();
if ( nAct > 0 )
{
for ( j = 0; j < (nAct - 1); ++j )
{
Action &act = ap.getNthAction( j );
for ( size_t k = j + 1; k < nAct; ++k )
{
Action &nact = ap.getNthAction( k );
int numCs = resolveConflict( act, nact );
if ( 0 != numCs )
{
std::cout << " Conflict in state " << i << std::endl;
myNumConflicts += numCs;
}
}
}
}
}
// Report errors for each rule that can never be reduced.
nRule = RuleTable::get()->getNumRules();
for ( i = 0; i < nRule; ++i )
RuleTable::get()->getNthRule( i )->setCanReduce( false );
for ( i = 0; i < nState; ++i )
{
stp = StateTable::get()->getNthState( i );
ActionList &ap = stp->getActions();
size_t nAct = ap.getNumActions();
for ( j = 0; j < nAct; ++j )
{
Action &act = ap.getNthAction( j );
if ( act.getType() == Action::REDUCE )
act.getRule()->setCanReduce( true );
}
}
for ( i = 0; i < nRule; ++i )
{
Rule *rp = RuleTable::get()->getNthRule( i );
if ( rp->canReduce() )
continue;
std::ostringstream tmpOut;
rp->print( tmpOut );
Error::get()->add( rp->getRuleLine(),
"Rule for '%s':\n %s\ncan not be reduced.\n",
rp->getLHS().c_str(), tmpOut.str().c_str() );
}
}
////////////////////////////////////////
void
Grammar::compressTables( void )
{
size_t i, nState;
nState = StateTable::get()->getNumStates();
// Find all reduce actions...
for ( i = 0; i < nState; ++i )
StateTable::get()->getNthState(i)->compress();
}
////////////////////////////////////////
void
Grammar::reportOutput( void )
{
std::string outputName;
Util::getFileName( outputName, myOutputDir, mySourceFile, ".out" );
std::ofstream out;
out.open( outputName.c_str() );
StateTable::get()->print( out, myBasisOnly );
out.close();
}
////////////////////////////////////////
void
Grammar::outputFiles( void )
{
Producer *producer = LanguageDriver::getProducer( myLanguage,
mySettings,
myOutputDir,
getName(),
mySourceFile );
if ( producer )
{
std::string arg, prefix;
producer->setDebugOutput( isDebugOutput() );
if ( producer->writeSource() )
{
if ( ! producer->writeHeader() )
Error::get()->add( "Unable to write the output header file." );
}
else
Error::get()->add( "Unable to write the output source file." );
delete producer;
}
}
////////////////////////////////////////
State *
Grammar::getNextState( void )
{
Config *bp;
State *state;
myCurConfigList.sortBasis();
bp = myCurConfigList.getBasis();
state = StateTable::get()->find( bp );
if ( state )
{
Config *sbp, *tbp;
for ( sbp = state->getBasis(), tbp = bp; sbp && tbp;
sbp = sbp->getNextBasis(), tbp = tbp->getNextBasis() )
{
sbp->mergePropLinks( tbp );
}
myCurConfigList.deleteConfigs();
}
else
{
myCurConfigList.computeClosure();
myCurConfigList.sort();
state = new State( bp, myCurConfigList.getConfig() );
myCurConfigList.resetPointers();
StateTable::get()->add( state );
// Cause things to recurse around (if necessary)
buildShifts( state );
}
return state;
}
////////////////////////////////////////
void
Grammar::buildShifts( State *state )
{
Config *cfp;
// Each configuration becomes complete after it contibutes to a successor
// state. Initially, all configurations are incomplete
for ( cfp = state->getConfig(); cfp; cfp = cfp->getNext() )
cfp->setStatus( Config::INCOMPLETE );
// Loop through all configs of state
for ( cfp = state->getConfig(); cfp; cfp = cfp->getNext() )
{
if ( cfp->getStatus() == Config::COMPLETE )
continue;
const Rule::RHSList &rhs = cfp->getRule()->getRHS();
if ( cfp->getDot() >= int( rhs.size() ) )
continue;
myCurConfigList.reset();
// Symbol after the dot
// for all configs in the state which have the symbol rhs[dot]
// following it's dot, add the same config to the basis set under
// construction but with the dot shifted one symbol to the right.
for ( Config *bcfp = cfp; bcfp; bcfp = bcfp->getNext() )
{
if ( bcfp->getStatus() == Config::COMPLETE )
continue;
const Rule::RHSList &brhs = bcfp->getRule()->getRHS();
if ( bcfp->getDot() >= int( brhs.size() ) )
continue;
if ( rhs[cfp->getDot()].first != brhs[bcfp->getDot()].first )
continue;
bcfp->setStatus( Config::COMPLETE );
Config *cfg = myCurConfigList.addWithBasis( bcfp->getRule(),
bcfp->getDot() + 1 );
cfg->addBackwardPropLink( bcfp );
}
State *newstp = getNextState();
// Add shift action to reach state newstp from state on symbol...
state->addAction( Action::SHIFT, rhs[cfp->getDot()].first, newstp, 0 );
}
}
////////////////////////////////////////
int
Grammar::resolveConflict( Action &act, Action &nextAct )
{
Symbol *symA, *symB;
int retval = 0;
if ( act.getLookAhead() != nextAct.getLookAhead() )
return 0;
else if ( act.getType() == Action::SHIFT &&
nextAct.getType() == Action::REDUCE )
{
symA = act.getLookAheadSymbol();
symB = nextAct.getRule()->getPrecedenceSymbol();
if ( ! symB || symA->getPrecedence() < 0 || symB->getPrecedence() < 0 )
{
nextAct.setType( Action::CONFLICT );
++retval;
}
else if ( symA->getPrecedence() > symB->getPrecedence() )
nextAct.setType( Action::RD_RESOLVED );
else if ( symA->getPrecedence() < symB->getPrecedence() )
act.setType( Action::SH_RESOLVED );
else if ( symA->getPrecedence() == symB->getPrecedence() &&
symA->getAssoc() == Symbol::RIGHT )
{
// Operator associativity breaks tie
nextAct.setType( Action::RD_RESOLVED );
}
else if ( symA->getPrecedence() == symB->getPrecedence() &&
symA->getAssoc() == Symbol::LEFT )
{
// Operator associativity breaks tie
act.setType( Action::SH_RESOLVED );
}
else
{
if ( !( symA->getPrecedence() == symB->getPrecedence() &&
symA->getAssoc() == Symbol::NONE ) )
{
throw std::logic_error( "unexpected situation resolving conflicts" );
}
nextAct.setType( Action::CONFLICT );
++retval;
}
if ( retval != 0 )
{
std::cout << "Unresolved SHIFT-REDUCE conflict between '"
<< act.getLookAhead() << "' and '"
<< nextAct.getLookAhead() << "'"
<< std::endl;
}
}
else if ( act.getType() == Action::REDUCE &&
nextAct.getType() == Action::REDUCE )
{
symA = act.getRule()->getPrecedenceSymbol();
symB = nextAct.getRule()->getPrecedenceSymbol();
if ( ! symA || ! symB || symA->getPrecedence() < 0 ||
symB->getPrecedence() < 0 ||
symA->getPrecedence() == symB->getPrecedence() )
{
nextAct.setType( Action::CONFLICT );
++retval;
}
else if ( symA->getPrecedence() > symB->getPrecedence() )
nextAct.setType( Action::RD_RESOLVED );
else if ( symA->getPrecedence() < symB->getPrecedence() )
act.setType( Action::SH_RESOLVED );
if ( retval != 0 )
{
std::cout << "Unresolved REDUCE-REDUCE conflict between '"
<< act.getLookAhead() << "' and '"
<< nextAct.getLookAhead() << "'"
<< std::endl;
}
}
return retval;
}
////////////////////////////////////////
Symbol *
Grammar::getStartSymbol( bool adderr )
{
Symbol *startSym = 0;
ValueMapIter si = mySettings.find( std::string( "start_symbol" ) );
if ( si != mySettings.end() )
{
startSym = SymbolTable::get()->find( (*si).second.first );
if ( ! startSym )
{
if ( RuleTable::get()->getNumRules() > 0 )
startSym = RuleTable::get()->getNthRule( 0 )->getLHSSymbol();
if ( startSym && adderr )
Error::get()->add( "The specified start symbol \"%s\" is not "
"in a nonterminal of the grammar. \"%s\" "
"will be used as the start symbol instead.",
(*si).second.first.c_str(),
startSym->getName().c_str() );
}
}
else
{
if ( RuleTable::get()->getNumRules() > 0 )
startSym = RuleTable::get()->getNthRule( 0 )->getLHSSymbol();
}
return startSym;
}
////////////////////////////////////////
const std::string &
Grammar::getName( void ) const
{
static const std::string defStr = VersionInfo::appName();
ValueMapConstIter si = mySettings.find( std::string( "name" ) );
return ( si != mySettings.end() ) ? (*si).second.first : defStr;
}