Skip to content

Commit 3e6b5f6

Browse files
authored
Add files via upload
1 parent 1ae9413 commit 3e6b5f6

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

InhaCC/InhaCC/cc/ParserGenerator.cs

+29-8
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ private void print_hs(List<HashSet<int>> lhs, string prefix)
151151
$"{prefix}({production_rules[i].production_name})={{{string.Join(",", lhs[i].ToList().Select(x => x == -1 ? "$" : production_rules[x].production_name))}}}\r\n");
152152
}
153153

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+
154163
private void print_states(int state, List<Tuple<int, int, int, HashSet<int>>> items)
155164
{
156165
var builder = new StringBuilder();
@@ -275,6 +284,7 @@ public void Generate()
275284
FOLLOW[rule.Last().index].Add(r);
276285

277286
#if true
287+
print_header("FISRT, FOLLOW SETS");
278288
print_hs(FIRST, "FIRST");
279289
print_hs(FOLLOW, "FOLLOW");
280290
#endif
@@ -423,6 +433,7 @@ public void GenerateLR1()
423433
FOLLOW[rule.Last().index].Add(r);
424434

425435
#if true
436+
print_header("FISRT, FOLLOW SETS");
426437
print_hs(FIRST, "FIRST");
427438
print_hs(FOLLOW, "FOLLOW");
428439
#endif
@@ -571,6 +582,7 @@ public void GenerateLR1()
571582
if (shift_tokens.ContainsKey(tuple.Item1))
572583
{
573584
#if true
585+
print_header("SHIFT-REDUCE CONFLICTS");
574586
GlobalPrinter.Append($"Shift-Reduce Conflict! {(tuple.Item1 == -1 ? "$" : production_rules[tuple.Item1].production_name)}\r\n");
575587
GlobalPrinter.Append($"States: {ms.Key} {tuple.Item2}\r\n");
576588
print_states(ms.Key, states[ms.Key]);
@@ -632,6 +644,7 @@ public void GenerateLR1()
632644

633645
number_of_states = states.Count;
634646
#if true
647+
print_header("STATES INFO");
635648
foreach (var s in states)
636649
print_states(s.Key, s.Value);
637650
#endif
@@ -689,6 +702,7 @@ public void GenerateLALR()
689702
FOLLOW[rule.Last().index].Add(r);
690703

691704
#if true
705+
print_header("FISRT, FOLLOW SETS");
692706
print_hs(FIRST, "FIRST");
693707
print_hs(FOLLOW, "FOLLOW");
694708
#endif
@@ -773,6 +787,7 @@ public void GenerateLALR()
773787
}
774788

775789
#if true
790+
print_header("UNMERGED STATES");
776791
foreach (var s in states)
777792
print_states(s.Key, s.Value);
778793
#endif
@@ -806,6 +821,7 @@ public void GenerateLALR()
806821
}
807822

808823
#if true
824+
print_header("MERGED STATES WITH SOME SETS");
809825
foreach (var s in merged_states)
810826
print_merged_states(s.Key, states[s.Key], s.Value.Select(x => states[x].Select(y => y.Item4.ToList()).ToList()).ToList());
811827
#endif
@@ -821,6 +837,7 @@ public void GenerateLALR()
821837
}
822838

823839
#if true
840+
print_header("MERGED STATES");
824841
foreach (var s in merged_states)
825842
print_states(s.Key, states[s.Key]);
826843
#endif
@@ -866,6 +883,7 @@ public void GenerateLALR()
866883
if (shift_tokens.ContainsKey(tuple.Item1))
867884
{
868885
#if true
886+
print_header("SHIFT-REDUCE CONFLICTS");
869887
GlobalPrinter.Append($"Shift-Reduce Conflict! {(tuple.Item1 == -1 ? "$" : production_rules[tuple.Item1].production_name)}\r\n");
870888
GlobalPrinter.Append($"States: {ms.Key} {tuple.Item2}\r\n");
871889
print_states(ms.Key, states[ms.Key]);
@@ -931,6 +949,7 @@ public void GenerateLALR()
931949

932950
public void PrintStates()
933951
{
952+
print_header("FINAL STATES");
934953
for (int i = 0; i < number_of_states; i++)
935954
{
936955
var builder = new StringBuilder();
@@ -1042,8 +1061,8 @@ public void PrintTable()
10421061
builder.Append("\r\n");
10431062
}
10441063
builder.Append(split_line);
1045-
1046-
1064+
1065+
print_header("PARSING TABLE");
10471066
GlobalPrinter.Append(builder.ToString() + "\r\n");
10481067
}
10491068

@@ -1307,7 +1326,8 @@ public class ParsingTreeNode
13071326
{
13081327
public string Produnction;
13091328
public string Contents;
1310-
public string UserContents;
1329+
public object UserContents;
1330+
public int ProductionRuleIndex;
13111331
public ParsingTreeNode Parent;
13121332
public List<ParsingTreeNode> Childs;
13131333

@@ -1318,15 +1338,13 @@ public static ParsingTreeNode NewNode(string production)
13181338
public static ParsingTreeNode NewNode(string production, string contents)
13191339
=> new ParsingTreeNode { Parent = null, Childs = new List<ParsingTreeNode>(), Produnction = production, Contents = contents };
13201340
}
1321-
1322-
int tree_index;
1341+
13231342
ParsingTreeNode root;
13241343

1325-
public ParsingTree(int index = 0)
1344+
public ParsingTree(ParsingTreeNode root)
13261345
{
1327-
tree_index = index;
1346+
this.root = root;
13281347
}
1329-
13301348
}
13311349

13321350
/// <summary>
@@ -1371,6 +1389,8 @@ public void Clear()
13711389
treenode_stack.Clear();
13721390
}
13731391

1392+
public ParsingTree Tree => new ParsingTree(treenode_stack.Peek());
1393+
13741394
public string Stack() => string.Join(" ", new Stack<int>(state_stack));
13751395

13761396
public void Insert(string token_name, string contents) => Insert(symbol_name_index[token_name], contents);
@@ -1426,6 +1446,7 @@ private void reduce(int index)
14261446
state_stack.Push(goto_table[state_stack.Peek()][group_table[reduce_production]]);
14271447

14281448
var reduction_parent = ParsingTree.ParsingTreeNode.NewNode(symbol_index_name[group_table[reduce_production]]);
1449+
reduction_parent.ProductionRuleIndex = reduce_production - 1;
14291450
reduce_treenodes.ForEach(x => x.Parent = reduction_parent);
14301451
reduction_parent.Contents = string.Join("", reduce_treenodes.Select(x => x.Contents));
14311452
reduction_parent.Childs = reduce_treenodes;

InhaCC/InhaCC/cc/ScannerGenerator.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private diagram make_nfa(string pattern)
149149
break;
150150

151151
case ')':
152-
if (opstack.Peek() != '(')
152+
if (opstack.Count == 0 || opstack.Peek() != '(')
153153
{
154154
build_errors.Add($"[regex] {i} no opener!");
155155
return null;
@@ -896,6 +896,8 @@ public class Scanner
896896
bool err = false;
897897
int latest_pos;
898898
List<int> err_pos;
899+
int current_line;
900+
int current_column;
899901

900902
public Scanner(int[][] transition_table, string[] accept_table)
901903
{
@@ -907,6 +909,8 @@ public void AllocateTarget(string literal)
907909
{
908910
target = literal;
909911
pos = 0;
912+
current_line = 0;
913+
current_column = 0;
910914
err_pos = new List<int>();
911915
err = false;
912916
}
@@ -923,12 +927,15 @@ public bool Error()
923927

924928
public int Position { get { return latest_pos; } }
925929

926-
public Tuple<string, string> Next()
930+
public Tuple<string, string, int, int> Next()
927931
{
928932
var builder = new StringBuilder();
929933
var node_pos = 0;
930934
latest_pos = pos;
931935

936+
int cur_line = current_line;
937+
int cur_column = current_column;
938+
932939
for (; pos < target.Length; pos++)
933940
{
934941
int next_transition = transition_table[node_pos][target[pos]];
@@ -944,6 +951,9 @@ public Tuple<string, string> Next()
944951
latest_pos = pos;
945952
pos--;
946953
node_pos = 0;
954+
current_column--;
955+
cur_line = current_line;
956+
cur_column = current_column;
947957
continue;
948958
}
949959
if (accept_table[node_pos] == null)
@@ -952,20 +962,21 @@ public Tuple<string, string> Next()
952962
err_pos.Add(pos);
953963
continue;
954964
}
955-
return new Tuple<string, string>(accept_table[node_pos], builder.ToString());
965+
return new Tuple<string, string, int, int> (accept_table[node_pos], builder.ToString(), cur_line + 1, cur_column + 1);
956966

957967
default:
968+
if (target[pos] == '\n') { current_line++; current_column = 1; } else current_column++;
958969
builder.Append(target[pos]);
959970
break;
960971
}
961972

962973
node_pos = next_transition;
963974
}
964975

965-
return new Tuple<string, string>(accept_table[node_pos], builder.ToString());
976+
return new Tuple<string, string, int, int> (accept_table[node_pos], builder.ToString(), cur_line + 1, cur_column + 1);
966977
}
967978

968-
public Tuple<string, string> Lookahead()
979+
public Tuple<string, string, int, int> Lookahead()
969980
{
970981
var npos = pos;
971982
var result = Next();

0 commit comments

Comments
 (0)