Skip to content

Commit

Permalink
wip : smelling good :)
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Nov 22, 2021
1 parent 93e58ff commit 134e39a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 55 deletions.
84 changes: 42 additions & 42 deletions Issue254/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ namespace Issue254
{
public static class Program
{

public static void Bench()
{
Console.WriteLine("starting bench");
// var summary = BenchmarkRunner.Run<Bench>();
}

public static void Main(string[] args)
{
Test254();
Expand All @@ -40,29 +39,29 @@ private static void Test254()
{
var tests = new Dictionary<string, double>()
{
{"1 + 2 * 3", 7.0},
{"2 - 3 / 4", 1.25},
{"-3 - -4", 1.0},
{"-3--4", 1.0},
{"-3+-4", -7.0},
{"-(1+2)", -3.0},
{ "-(1+2 * 3)", -7.0 } ,
{"3 * -2", -6.0},
{"9 & 8", 8.0},
{"7 & 8", 0.0},
{"8 & 8", 8.0},
{"1 + 7 & 15 - 7", 8.0},
{"1 | 2", 3.0},
{"3 | 5", 7.0},
{"1 + 3 | 3 - 1", 6.0}
{ "1 + 2 * 3", 7.0 },
{ "2 - 3 / 4", 1.25 },
{ "-3 - -4", 1.0 },
{ "-3--4", 1.0 },
{ "-3+-4", -7.0 },
{ "-(1+2)", -3.0 },
{ "-(1+2 * 3)", -7.0 },
{ "3 * -2", -6.0 },
{ "9 & 8", 8.0 },
{ "7 & 8", 0.0 },
{ "8 & 8", 8.0 },
{ "1 + 7 & 15 - 7", 8.0 },
{ "1 | 2", 3.0 },
{ "3 | 5", 7.0 },
{ "1 + 3 | 3 - 1", 6.0 }
};

var times = new List<long>() {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
var times = new List<long>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

var c = new Stopwatch();

int repetition = 10;

for (int i = 0; i < repetition; i++)
{
Console.WriteLine($"repetition #{i}");
Expand All @@ -73,6 +72,10 @@ private static void Test254()
var builder = new ParserBuilder<ExpressionToken, IAstNode>();
var parser = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT,
"MSFSExpressionParser_expressions");
if (sly.Debug.DEBUG_EXPRESSION_OPTIMIZATION)
{
Console.WriteLine(parser.Result.Configuration.Dump());
}

c.Reset();
c.Start();
Expand All @@ -88,7 +91,6 @@ private static void Test254()

//times = times.Select(x => x / repetition).ToList();
PrintTimes(times);

}

private static void PrintTimes(List<long> times)
Expand All @@ -101,7 +103,6 @@ private static void PrintTimes(List<long> times)
{
using (var reader = new StreamReader(stream))
{

string line = reader.ReadLine();
int i = 0;
while (line != null)
Expand All @@ -114,50 +115,49 @@ private static void PrintTimes(List<long> times)
{
line += ";" + times[i - 1];
}

lines.Add(line);
line = reader.ReadLine();
i++;
}
}
}

File.WriteAllLines(file,lines);
File.WriteAllLines(file, lines);
}
}

private static void Test254Old()
{
var tests = new Dictionary<string, double>()
{
{"1 + 2 * 3", 7.0},
{"2 - 3 / 4", 1.25},
{"-3 - -4", 1.0},
{"-3--4", 1.0},
{"-3+-4", -7.0},
{"-(1+2)", -3.0},
{ "-(1+2 * 3)", -7.0 } ,
{"3 * -2", -6.0},
{"9 & 8", 8.0},
{"7 & 8", 0.0},
{"8 & 8", 8.0},
{"1 + 7 & 15 - 7", 8.0}
,
{"1 | 2", 3.0},
{"3 | 5", 7.0},
{"1 + 3 | 3 - 1", 6.0}
{ "1 + 2 * 3", 7.0 },
{ "2 - 3 / 4", 1.25 },
{ "-3 - -4", 1.0 },
{ "-3--4", 1.0 },
{ "-3+-4", -7.0 },
{ "-(1+2)", -3.0 },
{ "-(1+2 * 3)", -7.0 },
{ "3 * -2", -6.0 },
{ "9 & 8", 8.0 },
{ "7 & 8", 0.0 },
{ "8 & 8", 8.0 },
{ "1 + 7 & 15 - 7", 8.0 },
{ "1 | 2", 3.0 },
{ "3 | 5", 7.0 },
{ "1 + 3 | 3 - 1", 6.0 }
};

var c = new Stopwatch();



foreach (var test in tests)
{
var parserInstance = new OldMSFSExpressionParser();
var builder = new ParserBuilder<OldExpressionToken, IAstNode>();
var parser = builder.BuildParser(parserInstance, ParserType.EBNF_LL_RECURSIVE_DESCENT,
"logicalExpression");
File.WriteAllText(@"c:\temp\parser.dump.old.txt",parser.Result.Configuration.Dump());
File.WriteAllText(@"c:\temp\parser.dump.old.txt", parser.Result.Configuration.Dump());
c.Reset();
c.Start();
var x = parser.Result.Parse(test.Key);
Expand Down
7 changes: 7 additions & 0 deletions sly/Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace sly
{
public class Debug
{
public static bool DEBUG_EXPRESSION_OPTIMIZATION = true;
}
}
17 changes: 11 additions & 6 deletions sly/parser/generator/ExpressionRulesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ private NonTerminal<IN> BuildPrecedenceNonTerminal(string name, string nextName,
{
rule.SetVisitor(x);
rule.IsExpressionRule = true;
rule.IsInfixExpressionRule = true;

});
nonTerminal.Rules.Add(rule);
}
Expand Down Expand Up @@ -275,12 +277,15 @@ private NonTerminal<IN> BuildPrecedenceNonTerminal(string name, string nextName,
nonTerminal.Rules.Add(rule);
}

var rule0 = new Rule<IN>();
rule0.Clauses.Add(new NonTerminalClause<IN>(nextName));
rule0.IsExpressionRule = true;
rule0.ExpressionAffix = Affix.NotOperator;
rule0.IsByPassRule = true;
nonTerminal.Rules.Add(rule0);
if (InFixOps.Count == 0 || !Debug.DEBUG_EXPRESSION_OPTIMIZATION )
{
var rule0 = new Rule<IN>();
rule0.Clauses.Add(new NonTerminalClause<IN>(nextName));
rule0.IsExpressionRule = true;
rule0.ExpressionAffix = Affix.NotOperator;
rule0.IsByPassRule = true;
nonTerminal.Rules.Add(rule0);
}

return nonTerminal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private DotNode Visit(SyntaxNode<IN> node)

if (node.IsByPassNode)
{
result = children[0];
//result = children[0];
}
else
{
Expand Down
25 changes: 22 additions & 3 deletions sly/parser/parser/llparser/EBNFRecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public EBNFRecursiveDescentSyntaxParser(ParserConfiguration<IN, OUT> configurati
public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> rule, int position,
string nonTerminalName)
{
if (rule.IsInfixExpressionRule && rule.IsExpressionRule)
if (rule.RuleString == "primary: DECIMAL_NUMBER")
{
;
}

if (rule.IsInfixExpressionRule && rule.IsExpressionRule && Debug.DEBUG_EXPRESSION_OPTIMIZATION)
{
return ParseExpressionRule(tokens, rule, position, nonTerminalName);
}
Expand Down Expand Up @@ -151,6 +156,12 @@ public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> ru
public virtual SyntaxParseResult<IN> ParseExpressionRule(IList<Token<IN>> tokens, Rule<IN> rule, int position,
string nonTerminalName)
{

if (nonTerminalName == "expr_14_PLUS_MINUS")
{
;
}

var currentPosition = position;
var errors = new List<UnexpectedTokenSyntaxError<IN>>();
var isError = false;
Expand Down Expand Up @@ -189,7 +200,14 @@ public virtual SyntaxParseResult<IN> ParseExpressionRule(IList<Token<IN>> tokens
return firstResult;
}
}
else
{
currentPosition = secondResult.EndingPosition;
}
}



if (second is TerminalClause<IN> secondTerminal)
{
secondResult = ParseTerminal(tokens, secondTerminal, currentPosition);
Expand All @@ -210,7 +228,7 @@ public virtual SyntaxParseResult<IN> ParseExpressionRule(IList<Token<IN>> tokens
currentPosition = secondResult.EndingPosition;
var third = rule.Clauses[2];
SyntaxParseResult<IN> thirdResult;
if (second is NonTerminalClause<IN> thirdNonTerminal)
if (third is NonTerminalClause<IN> thirdNonTerminal)
{
thirdResult = ParseNonTerminal(tokens, thirdNonTerminal, currentPosition);

Expand All @@ -230,7 +248,7 @@ public virtual SyntaxParseResult<IN> ParseExpressionRule(IList<Token<IN>> tokens
children.Add(firstResult.Root);
children.Add(secondResult.Root);
children.Add(thirdResult.Root);

currentPosition = thirdResult.EndingPosition;
var finalNode = new SyntaxNode<IN>( nonTerminalName, children);
finalNode.ExpressionAffix = rule.ExpressionAffix;
finalNode = ManageExpressionRules(rule, finalNode);
Expand All @@ -239,6 +257,7 @@ public virtual SyntaxParseResult<IN> ParseExpressionRule(IList<Token<IN>> tokens
finalResult.IsEnded = currentPosition >= tokens.Count - 1
|| currentPosition == tokens.Count - 2 &&
tokens[tokens.Count - 1].IsEOS;
finalResult.EndingPosition = currentPosition;
return finalResult;
}
}
Expand Down
5 changes: 3 additions & 2 deletions sly/parser/parser/llparser/RecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, string startingNonTe
matchingRuleCount++;
var r = Parse(tokens, rule, 0, start);
rs.Add(r);
if (typeof(IN).FullName.Contains("ExpressionToken"))
if (typeof(IN).FullName.Contains("ExpressionToken") && Debug.DEBUG_EXPRESSION_OPTIMIZATION)
{
var tree = r.Root;
var dump = tree.Dump("");
Expand Down Expand Up @@ -276,6 +276,7 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, string no
if (startPosition < tokens.Count && !tokens[startPosition].IsEOS &&
innerrule.PossibleLeadingTokens.Contains(tokens[startPosition].TokenID) || innerrule.MayBeEmpty)
{
Console.WriteLine($"{innerrule.Key} @ {startPosition}");
var innerRuleRes = Parse(tokens, innerrule, startPosition, nonTerminalName);
rulesResults.Add(innerRuleRes);

Expand Down Expand Up @@ -354,7 +355,7 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, string no
max.IsError = true;
max.Root = null;
max.IsEnded = false;
max.EndingPosition = currentPosition;
//max.EndingPosition = currentPosition;
}

var result = new SyntaxParseResult<IN>();
Expand Down
2 changes: 1 addition & 1 deletion sly/parser/syntax/tree/SyntaxLeaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SyntaxLeaf(Token<IN> token, bool discarded)

public string Dump(string tab)
{
return $"{tab}+ {Token.TokenID} : {Token.Value}";
return $"{tab}+ {Token.TokenID} : {Token.Value} @{Token.PositionInTokenFlow}";
}
}
}

0 comments on commit 134e39a

Please sign in to comment.