Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rollrat authored May 13, 2019
1 parent 3e6b5f6 commit d383c0f
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions ParserGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ private void print_hs(List<HashSet<int>> lhs, string prefix)
$"{prefix}({production_rules[i].production_name})={{{string.Join(",", lhs[i].ToList().Select(x => x == -1 ? "$" : production_rules[x].production_name))}}}\r\n");
}

private void print_header(string head_text)
{
GlobalPrinter.Append("\r\n" + new string('=', 50) + "\r\n\r\n");
int spaces = 50 - head_text.Length;
int padLeft = spaces / 2 + head_text.Length;
GlobalPrinter.Append(head_text.PadLeft(padLeft).PadRight(50));
GlobalPrinter.Append("\r\n\r\n" + new string('=', 50) + "\r\n");
}

private void print_states(int state, List<Tuple<int, int, int, HashSet<int>>> items)
{
var builder = new StringBuilder();
Expand Down Expand Up @@ -275,6 +284,7 @@ public void Generate()
FOLLOW[rule.Last().index].Add(r);

#if true
print_header("FISRT, FOLLOW SETS");
print_hs(FIRST, "FIRST");
print_hs(FOLLOW, "FOLLOW");
#endif
Expand Down Expand Up @@ -423,6 +433,7 @@ public void GenerateLR1()
FOLLOW[rule.Last().index].Add(r);

#if true
print_header("FISRT, FOLLOW SETS");
print_hs(FIRST, "FIRST");
print_hs(FOLLOW, "FOLLOW");
#endif
Expand Down Expand Up @@ -571,6 +582,7 @@ public void GenerateLR1()
if (shift_tokens.ContainsKey(tuple.Item1))
{
#if true
print_header("SHIFT-REDUCE CONFLICTS");
GlobalPrinter.Append($"Shift-Reduce Conflict! {(tuple.Item1 == -1 ? "$" : production_rules[tuple.Item1].production_name)}\r\n");
GlobalPrinter.Append($"States: {ms.Key} {tuple.Item2}\r\n");
print_states(ms.Key, states[ms.Key]);
Expand Down Expand Up @@ -632,6 +644,7 @@ public void GenerateLR1()

number_of_states = states.Count;
#if true
print_header("STATES INFO");
foreach (var s in states)
print_states(s.Key, s.Value);
#endif
Expand Down Expand Up @@ -689,6 +702,7 @@ public void GenerateLALR()
FOLLOW[rule.Last().index].Add(r);

#if true
print_header("FISRT, FOLLOW SETS");
print_hs(FIRST, "FIRST");
print_hs(FOLLOW, "FOLLOW");
#endif
Expand Down Expand Up @@ -773,6 +787,7 @@ public void GenerateLALR()
}

#if true
print_header("UNMERGED STATES");
foreach (var s in states)
print_states(s.Key, s.Value);
#endif
Expand Down Expand Up @@ -806,6 +821,7 @@ public void GenerateLALR()
}

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

#if true
print_header("MERGED STATES");
foreach (var s in merged_states)
print_states(s.Key, states[s.Key]);
#endif
Expand Down Expand Up @@ -866,6 +883,7 @@ public void GenerateLALR()
if (shift_tokens.ContainsKey(tuple.Item1))
{
#if true
print_header("SHIFT-REDUCE CONFLICTS");
GlobalPrinter.Append($"Shift-Reduce Conflict! {(tuple.Item1 == -1 ? "$" : production_rules[tuple.Item1].production_name)}\r\n");
GlobalPrinter.Append($"States: {ms.Key} {tuple.Item2}\r\n");
print_states(ms.Key, states[ms.Key]);
Expand Down Expand Up @@ -931,6 +949,7 @@ public void GenerateLALR()

public void PrintStates()
{
print_header("FINAL STATES");
for (int i = 0; i < number_of_states; i++)
{
var builder = new StringBuilder();
Expand Down Expand Up @@ -1042,8 +1061,8 @@ public void PrintTable()
builder.Append("\r\n");
}
builder.Append(split_line);


print_header("PARSING TABLE");
GlobalPrinter.Append(builder.ToString() + "\r\n");
}

Expand Down

0 comments on commit d383c0f

Please sign in to comment.