Skip to content

Commit 25b4b57

Browse files
authored
Supports L-Attributed
1 parent 50ab0fe commit 25b4b57

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

ParserGenerator.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
33
Copyright (C) 2019. rollrat All Rights Reserved.
44
@@ -13,24 +13,41 @@
1313

1414
namespace ParserGenerator
1515
{
16+
public class ParserAction
17+
{
18+
public Action<ParsingTree.ParsingTreeNode> SemanticAction;
19+
public static ParserAction Create(Action<ParsingTree.ParsingTreeNode> action)
20+
=> new ParserAction { SemanticAction = action };
21+
}
22+
1623
public class ParserProduction
1724
{
1825
public int index;
1926
public string production_name;
2027
public bool isterminal;
2128
public List<ParserProduction> contents = new List<ParserProduction>();
2229
public List<List<ParserProduction>> sub_productions = new List<List<ParserProduction>>();
30+
public List<ParserAction> temp_actions = new List<ParserAction>();
31+
public List<ParserAction> actions = new List<ParserAction>();
2332

2433
public static ParserProduction operator +(ParserProduction p1, ParserProduction p2)
2534
{
2635
p1.contents.Add(p2);
2736
return p1;
2837
}
2938

39+
public static ParserProduction operator +(ParserProduction pp, ParserAction ac)
40+
{
41+
pp.temp_actions.Add(ac);
42+
return pp;
43+
}
44+
3045
public static ParserProduction operator |(ParserProduction p1, ParserProduction p2)
3146
{
3247
p2.contents.Insert(0, p2);
3348
p1.sub_productions.Add(new List<ParserProduction>(p2.contents));
49+
p1.actions.AddRange(p2.temp_actions);
50+
p2.temp_actions.Clear();
3451
p2.contents.Clear();
3552
return p1;
3653
}
@@ -1266,6 +1283,7 @@ public ShiftReduceParser CreateShiftReduceParserInstance()
12661283
var grammar = new List<List<int>>();
12671284
var grammar_group = new List<int>();
12681285
var production_mapping = new List<List<int>>();
1286+
var semantic_rules = new List<ParserAction>();
12691287
var pm_count = 0;
12701288

12711289
foreach (var pr in production_rules)
@@ -1280,6 +1298,7 @@ public ShiftReduceParser CreateShiftReduceParserInstance()
12801298
}
12811299
grammar.AddRange(ll);
12821300
production_mapping.Add(pm);
1301+
semantic_rules.AddRange(pr.actions);
12831302
}
12841303

12851304
for (int i = 0; i < number_of_states; i++)
@@ -1316,7 +1335,7 @@ public ShiftReduceParser CreateShiftReduceParserInstance()
13161335
}
13171336
}
13181337

1319-
return new ShiftReduceParser(symbol_table, jump_table, goto_table, grammar_group.ToArray(), grammar.Select(x => x.ToArray()).ToArray());
1338+
return new ShiftReduceParser(symbol_table, jump_table, goto_table, grammar_group.ToArray(), grammar.Select(x => x.ToArray()).ToArray(), semantic_rules);
13201339
}
13211340
}
13221341

@@ -1339,7 +1358,7 @@ public static ParsingTreeNode NewNode(string production, string contents)
13391358
=> new ParsingTreeNode { Parent = null, Childs = new List<ParsingTreeNode>(), Produnction = production, Contents = contents };
13401359
}
13411360

1342-
ParsingTreeNode root;
1361+
public ParsingTreeNode root;
13431362

13441363
public ParsingTree(ParsingTreeNode root)
13451364
{
@@ -1356,6 +1375,7 @@ public class ShiftReduceParser
13561375
List<string> symbol_index_name = new List<string>();
13571376
Stack<int> state_stack = new Stack<int>();
13581377
Stack<ParsingTree.ParsingTreeNode> treenode_stack = new Stack<ParsingTree.ParsingTreeNode>();
1378+
List<ParserAction> actions;
13591379

13601380
// 3 1 2 0
13611381
// Accept? Shift? Reduce? Error?
@@ -1364,13 +1384,14 @@ public class ShiftReduceParser
13641384
int[][] production;
13651385
int[] group_table;
13661386

1367-
public ShiftReduceParser(Dictionary<string, int> symbol_table, int[][] jump_table, int[][] goto_table, int[] group_table, int[][] production)
1387+
public ShiftReduceParser(Dictionary<string, int> symbol_table, int[][] jump_table, int[][] goto_table, int[] group_table, int[][] production, List<ParserAction> actions)
13681388
{
13691389
symbol_name_index = symbol_table;
13701390
this.jump_table = jump_table;
13711391
this.goto_table = goto_table;
13721392
this.production = production;
13731393
this.group_table = group_table;
1394+
this.actions = actions;
13741395
var l = symbol_table.ToList().Select(x => new Tuple<int, string>(x.Value, x.Key)).ToList();
13751396
l.Sort();
13761397
l.ForEach(x => symbol_index_name.Add(x.Item2));
@@ -1451,6 +1472,7 @@ private void reduce(int index)
14511472
reduction_parent.Contents = string.Join("", reduce_treenodes.Select(x => x.Contents));
14521473
reduction_parent.Childs = reduce_treenodes;
14531474
treenode_stack.Push(reduction_parent);
1475+
actions[reduction_parent.ProductionRuleIndex].SemanticAction(reduction_parent);
14541476
}
14551477
}
1456-
}
1478+
}

0 commit comments

Comments
 (0)