@@ -151,6 +151,15 @@ private void print_hs(List<HashSet<int>> lhs, string prefix)
151
151
$ "{ prefix } ({ production_rules [ i ] . production_name } )={{{string.Join(", ", lhs[i].ToList().Select(x => x == -1 ? " $" : production_rules[ x ] . production_name ) ) } } } \r \n ") ;
152
152
}
153
153
154
+ private void print_header ( string head_text )
155
+ {
156
+ GlobalPrinter . Append ( "\r \n " + new string ( '=' , 50 ) + "\r \n \r \n " ) ;
157
+ int spaces = 50 - head_text . Length ;
158
+ int padLeft = spaces / 2 + head_text . Length ;
159
+ GlobalPrinter . Append ( head_text . PadLeft ( padLeft ) . PadRight ( 50 ) ) ;
160
+ GlobalPrinter . Append ( "\r \n \r \n " + new string ( '=' , 50 ) + "\r \n " ) ;
161
+ }
162
+
154
163
private void print_states ( int state , List < Tuple < int , int , int , HashSet < int > > > items )
155
164
{
156
165
var builder = new StringBuilder ( ) ;
@@ -275,6 +284,7 @@ public void Generate()
275
284
FOLLOW [ rule . Last ( ) . index ] . Add ( r ) ;
276
285
277
286
#if true
287
+ print_header ( "FISRT, FOLLOW SETS" ) ;
278
288
print_hs ( FIRST , "FIRST" ) ;
279
289
print_hs ( FOLLOW , "FOLLOW" ) ;
280
290
#endif
@@ -423,6 +433,7 @@ public void GenerateLR1()
423
433
FOLLOW [ rule . Last ( ) . index ] . Add ( r ) ;
424
434
425
435
#if true
436
+ print_header ( "FISRT, FOLLOW SETS" ) ;
426
437
print_hs ( FIRST , "FIRST" ) ;
427
438
print_hs ( FOLLOW , "FOLLOW" ) ;
428
439
#endif
@@ -571,6 +582,7 @@ public void GenerateLR1()
571
582
if ( shift_tokens . ContainsKey ( tuple . Item1 ) )
572
583
{
573
584
#if true
585
+ print_header ( "SHIFT-REDUCE CONFLICTS" ) ;
574
586
GlobalPrinter . Append ( $ "Shift-Reduce Conflict! { ( tuple . Item1 == - 1 ? "$" : production_rules [ tuple . Item1 ] . production_name ) } \r \n ") ;
575
587
GlobalPrinter . Append ( $ "States: { ms . Key } { tuple . Item2 } \r \n ") ;
576
588
print_states ( ms . Key , states [ ms . Key ] ) ;
@@ -632,6 +644,7 @@ public void GenerateLR1()
632
644
633
645
number_of_states = states . Count ;
634
646
#if true
647
+ print_header ( "STATES INFO" ) ;
635
648
foreach ( var s in states )
636
649
print_states ( s . Key , s . Value ) ;
637
650
#endif
@@ -689,6 +702,7 @@ public void GenerateLALR()
689
702
FOLLOW [ rule . Last ( ) . index ] . Add ( r ) ;
690
703
691
704
#if true
705
+ print_header ( "FISRT, FOLLOW SETS" ) ;
692
706
print_hs ( FIRST , "FIRST" ) ;
693
707
print_hs ( FOLLOW , "FOLLOW" ) ;
694
708
#endif
@@ -773,6 +787,7 @@ public void GenerateLALR()
773
787
}
774
788
775
789
#if true
790
+ print_header ( "UNMERGED STATES" ) ;
776
791
foreach ( var s in states )
777
792
print_states ( s . Key , s . Value ) ;
778
793
#endif
@@ -806,6 +821,7 @@ public void GenerateLALR()
806
821
}
807
822
808
823
#if true
824
+ print_header ( "MERGED STATES WITH SOME SETS" ) ;
809
825
foreach ( var s in merged_states )
810
826
print_merged_states ( s . Key , states [ s . Key ] , s . Value . Select ( x => states [ x ] . Select ( y => y . Item4 . ToList ( ) ) . ToList ( ) ) . ToList ( ) ) ;
811
827
#endif
@@ -821,6 +837,7 @@ public void GenerateLALR()
821
837
}
822
838
823
839
#if true
840
+ print_header ( "MERGED STATES" ) ;
824
841
foreach ( var s in merged_states )
825
842
print_states ( s . Key , states [ s . Key ] ) ;
826
843
#endif
@@ -866,6 +883,7 @@ public void GenerateLALR()
866
883
if ( shift_tokens . ContainsKey ( tuple . Item1 ) )
867
884
{
868
885
#if true
886
+ print_header ( "SHIFT-REDUCE CONFLICTS" ) ;
869
887
GlobalPrinter . Append ( $ "Shift-Reduce Conflict! { ( tuple . Item1 == - 1 ? "$" : production_rules [ tuple . Item1 ] . production_name ) } \r \n ") ;
870
888
GlobalPrinter . Append ( $ "States: { ms . Key } { tuple . Item2 } \r \n ") ;
871
889
print_states ( ms . Key , states [ ms . Key ] ) ;
@@ -931,6 +949,7 @@ public void GenerateLALR()
931
949
932
950
public void PrintStates ( )
933
951
{
952
+ print_header ( "FINAL STATES" ) ;
934
953
for ( int i = 0 ; i < number_of_states ; i ++ )
935
954
{
936
955
var builder = new StringBuilder ( ) ;
@@ -1042,8 +1061,8 @@ public void PrintTable()
1042
1061
builder . Append ( "\r \n " ) ;
1043
1062
}
1044
1063
builder . Append ( split_line ) ;
1045
-
1046
-
1064
+
1065
+ print_header ( "PARSING TABLE" ) ;
1047
1066
GlobalPrinter . Append ( builder . ToString ( ) + "\r \n " ) ;
1048
1067
}
1049
1068
@@ -1307,7 +1326,8 @@ public class ParsingTreeNode
1307
1326
{
1308
1327
public string Produnction ;
1309
1328
public string Contents ;
1310
- public string UserContents ;
1329
+ public object UserContents ;
1330
+ public int ProductionRuleIndex ;
1311
1331
public ParsingTreeNode Parent ;
1312
1332
public List < ParsingTreeNode > Childs ;
1313
1333
@@ -1318,15 +1338,13 @@ public static ParsingTreeNode NewNode(string production)
1318
1338
public static ParsingTreeNode NewNode ( string production , string contents )
1319
1339
=> new ParsingTreeNode { Parent = null , Childs = new List < ParsingTreeNode > ( ) , Produnction = production , Contents = contents } ;
1320
1340
}
1321
-
1322
- int tree_index ;
1341
+
1323
1342
ParsingTreeNode root ;
1324
1343
1325
- public ParsingTree ( int index = 0 )
1344
+ public ParsingTree ( ParsingTreeNode root )
1326
1345
{
1327
- tree_index = index ;
1346
+ this . root = root ;
1328
1347
}
1329
-
1330
1348
}
1331
1349
1332
1350
/// <summary>
@@ -1371,6 +1389,8 @@ public void Clear()
1371
1389
treenode_stack . Clear ( ) ;
1372
1390
}
1373
1391
1392
+ public ParsingTree Tree => new ParsingTree ( treenode_stack . Peek ( ) ) ;
1393
+
1374
1394
public string Stack ( ) => string . Join ( " " , new Stack < int > ( state_stack ) ) ;
1375
1395
1376
1396
public void Insert ( string token_name , string contents ) => Insert ( symbol_name_index [ token_name ] , contents ) ;
@@ -1426,6 +1446,7 @@ private void reduce(int index)
1426
1446
state_stack . Push ( goto_table [ state_stack . Peek ( ) ] [ group_table [ reduce_production ] ] ) ;
1427
1447
1428
1448
var reduction_parent = ParsingTree . ParsingTreeNode . NewNode ( symbol_index_name [ group_table [ reduce_production ] ] ) ;
1449
+ reduction_parent . ProductionRuleIndex = reduce_production - 1 ;
1429
1450
reduce_treenodes . ForEach ( x => x . Parent = reduction_parent ) ;
1430
1451
reduction_parent . Contents = string . Join ( "" , reduce_treenodes . Select ( x => x . Contents ) ) ;
1431
1452
reduction_parent . Childs = reduce_treenodes ;
0 commit comments