Skip to content

Commit

Permalink
fixed bug in XbnfToPckTransform/_GetDysRepeat()
Browse files Browse the repository at this point in the history
  • Loading branch information
codewitch-honey-crisis committed Nov 21, 2019
1 parent a22baee commit 1da3ddb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 78 deletions.
74 changes: 38 additions & 36 deletions cfg/CfgDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public string StartSymbol {
{
if (!IsSymbol(value))
throw new KeyNotFoundException("The specified symbol does not exist");
foreach(var sattrs in AttributeSets)
foreach (var sattrs in AttributeSets)
{
var i =sattrs.Value.IndexOf("start");
if(-1<i && (sattrs.Value[i].Value is bool) && (bool)sattrs.Value[i].Value)
var i = sattrs.Value.IndexOf("start");
if (-1 < i && (sattrs.Value[i].Value is bool) && (bool)sattrs.Value[i].Value)
sattrs.Value.RemoveAt(i);
if(sattrs.Key==value)
if (sattrs.Key == value)
{
i = sattrs.Value.IndexOf("start");
if (-1 < i)
sattrs.Value[i].Value=true;
else
sattrs.Value[i].Value = true;
else
sattrs.Value.Add(new CfgAttribute("start", true));
}
}
Expand All @@ -77,7 +77,7 @@ public string StartSymbol {
public void InternSymbols()
{
var syms = FillSymbols();
for(int ic=syms.Count,i=0;i<ic;++i)
for (int ic = syms.Count, i = 0; i < ic; ++i)
string.Intern(syms[i]);
}
public bool IsDirectlyLeftRecursive {
Expand All @@ -93,14 +93,14 @@ public IEnumerable<string> EnumNonTerminals()
{
var seen = new HashSet<string>();
var ic = Rules.Count;
for(var i=0;i<ic;++i)
for (var i = 0; i < ic; ++i)
{
var s = Rules[i].Left;
if (seen.Add(s))
yield return s;
}
}
public IList<string> FillNonTerminals(IList<string> result=null)
public IList<string> FillNonTerminals(IList<string> result = null)
{
if (null == result)
result = new List<string>();
Expand All @@ -126,7 +126,7 @@ public IEnumerable<string> EnumSymbols()
for (var i = 0; i < ic; ++i)
{
var right = Rules[i].Right;
for(int jc=right.Count,j=0;j<jc;++j)
for (int jc = right.Count, j = 0; j < jc; ++j)
{
var s = right[j];
if (seen.Add(s))
Expand Down Expand Up @@ -164,11 +164,11 @@ public IList<string> FillSymbols(IList<string> result = null)
result.Add(s);
}
}
foreach(var attrs in AttributeSets)
if(seen.Add(attrs.Key))
foreach (var attrs in AttributeSets)
if (seen.Add(attrs.Key))
if (!result.Contains(attrs.Key))
result.Add(attrs.Key);

if (!result.Contains("#EOS"))
result.Add("#EOS");
if (!result.Contains("#ERROR"))
Expand Down Expand Up @@ -405,7 +405,7 @@ public IDictionary<string, ICollection<string>> FillFirsts(IDictionary<string, I
if (null == result)
result = new Dictionary<string, ICollection<(CfgRule Rule, string Symbol)>>();
var predictNT = _FillPredictNT();

// finally, for each non-terminal N we still have in the firsts, resolve FIRSTS(N)
foreach (var kvp in predictNT)
{
Expand All @@ -416,14 +416,14 @@ public IDictionary<string, ICollection<string>> FillFirsts(IDictionary<string, I
_ResolvePredict(item.Symbol, res, predictNT, new HashSet<string>());
foreach (var r in res)
col.Add((item.Rule, r));

}
result.Add(kvp.Key, col);
}
return result;
}

void _ResolvePredict(string symbol,ICollection<string> result,IDictionary<string, ICollection<(CfgRule Rule, string Symbol)>> predictNT,HashSet<string> seen)
void _ResolvePredict(string symbol, ICollection<string> result, IDictionary<string, ICollection<(CfgRule Rule, string Symbol)>> predictNT, HashSet<string> seen)
{
if (seen.Add(symbol))
{
Expand Down Expand Up @@ -451,7 +451,7 @@ public IDictionary<string, ICollection<string>> FillFollows(IDictionary<string,
if (null == result)
result = new Dictionary<string, ICollection<string>>();

var followsNT = new Dictionary<string, ICollection<string>>();
var followsNT = new Dictionary<string, ICollection<string>>();

// we'll need the predict table
//Console.Error.WriteLine("Computing predict...");
Expand Down Expand Up @@ -524,7 +524,7 @@ public IDictionary<string, ICollection<string>> FillFollows(IDictionary<string,
// below we look for any non-terminals in the follows result and replace them
// with their follows, so for example if N appeared, N would be replaced with
// the result of FOLLOW(N)

foreach (var nt in EnumNonTerminals())
{
var col = new HashSet<string>();
Expand All @@ -534,7 +534,7 @@ public IDictionary<string, ICollection<string>> FillFollows(IDictionary<string,
}
//Console.Error.WriteLine("Done!");
return result;

}
void _ResolveFollows(string symbol, ICollection<string> result, IDictionary<string, ICollection<string>> followsNT, HashSet<string> seen)
{
Expand Down Expand Up @@ -619,21 +619,21 @@ public IList<string> FillClosure(string symbol, IList<string> result = null)
}
return result;
}
public object GetAttribute(string symbol,string name, object @default = null)
public object GetAttribute(string symbol, string name, object @default = null)
{
CfgAttributeList l;
if(AttributeSets.TryGetValue(symbol,out l))
if (AttributeSets.TryGetValue(symbol, out l))
{
var i = l.IndexOf(name);
if(-1<i)
if (-1 < i)
return l[i].Value;
}
return @default;
}
public bool IsNonTerminal(string symbol)
{
if (null != _ntCache) return _ntCache.Contains(symbol);
for(int ic=Rules.Count,i=0;i<ic;++i)
for (int ic = Rules.Count, i = 0; i < ic; ++i)
if (Rules[i].Left == symbol)
return true;
return false;
Expand All @@ -646,7 +646,7 @@ public bool IsSymbol(string symbol)
var rule = Rules[i];
if (rule.Left == symbol)
return true;
for(int jc=rule.Right.Count,j=0;j<jc;++j)
for (int jc = rule.Right.Count, j = 0; j < jc; ++j)
{
if (symbol == rule.Right[j])
return true;
Expand All @@ -657,7 +657,7 @@ public bool IsSymbol(string symbol)
public override string ToString()
{
var sb = new StringBuilder();
foreach(var attrSet in AttributeSets)
foreach (var attrSet in AttributeSets)
{
if (0 < attrSet.Value.Count)
{
Expand All @@ -671,7 +671,7 @@ public override string ToString()
sb.AppendLine();
}
}
for (int ic=Rules.Count,i=0;i<ic;++i)
for (int ic = Rules.Count, i = 0; i < ic; ++i)
sb.AppendLine(Rules[i].ToString());
return sb.ToString();
}
Expand Down Expand Up @@ -710,7 +710,7 @@ public static CfgDocument ReadFrom(string filename)
internal static CfgDocument Parse(ParseContext pc)
{
var result = new CfgDocument();
while(-1!=pc.Current)
while (-1 != pc.Current)
{
var line = pc.Line;
var column = pc.Column;
Expand All @@ -722,14 +722,14 @@ internal static CfgDocument Parse(ParseContext pc)
CfgNode.SkipCommentsAndWhitespace(pc);
}
var id = CfgNode.ParseIdentifier(pc);
if(string.IsNullOrEmpty(id))
if (string.IsNullOrEmpty(id))
{
pc.Advance();
CfgNode.SkipCommentsAndWhitespace(pc);
continue;
}
CfgNode.SkipCommentsAndWhitespace(pc);

pc.Expecting(':', '-', '=');
if (':' == pc.Current) // attribute set
{
Expand All @@ -747,7 +747,8 @@ internal static CfgDocument Parse(ParseContext pc)
}
result.AttributeSets.Add(id, d);
CfgNode.SkipCommentsAndWhitespace(pc);
} else if ('-' == pc.Current)
}
else if ('-' == pc.Current)
{
pc.Advance();
pc.Expecting('>');
Expand All @@ -762,14 +763,15 @@ internal static CfgDocument Parse(ParseContext pc)
CfgNode.SkipCommentsAndWhitespace(pc);
}
result.Rules.Add(rule);
} else if ('=' == pc.Current)
}
else if ('=' == pc.Current)
{
pc.TrySkipUntil('\n', true);
}
if ('\n' == pc.Current)
pc.Advance();
CfgNode.SkipCommentsAndWhitespace(pc);

}
return result;
}
Expand All @@ -790,7 +792,7 @@ public bool Equals(CfgDocument rhs)
foreach (var attr in attrs.Value)
{
var i = d.IndexOf(attr.Name);
if (0>i || !Equals(d[i].Value, attr.Value))
if (0 > i || !Equals(d[i].Value, attr.Value))
return false;
}
}
Expand All @@ -813,11 +815,11 @@ public override int GetHashCode()
var result = 0;
foreach (var attrs in AttributeSets)
{
if(null!=attrs.Key)
if (null != attrs.Key)
result ^= attrs.Key.GetHashCode();
foreach (var attr in attrs.Value)
{
if(null!=attr.Name)
if (null != attr.Name)
result ^= attr.Name.GetHashCode();
if (null != attr.Value)
result ^= attr.Value.GetHashCode();
Expand All @@ -843,4 +845,4 @@ public override int GetHashCode()
}
#endregion
}
}
}
Loading

0 comments on commit 1da3ddb

Please sign in to comment.