Skip to content

Instantly share code, notes, and snippets.

@musicm122
Created July 12, 2014 21:36
Show Gist options
  • Save musicm122/53febdf3c26910a13967 to your computer and use it in GitHub Desktop.
Save musicm122/53febdf3c26910a13967 to your computer and use it in GitHub Desktop.
Outline of how the Lexing and parsing process is going to go down.
void Main()
{
//The user input expression
var expression = "\"java\" and \"SQL\" and \"C#\" or \"VB.NET\"";
//The collection of tokens that we will use to evaluate the logic in the expression
var tokens = Tokenize(expression);
//tokens.Dump();
//Our evaluated representation of the tokens; We can use this to
//convert the expression to whatever language we wish
var ConditionNode = Eval(tokens);
//Transforms our Nodes into our desired programming language
var endResult = CreateQueryString(ConditionNode );
}
// Define other methods and classes here
public static List<string> Tokenize(string expression)
{
var tempExpression = System.Security.SecurityElement.Escape(expression);
string doubleQuote = "&quot;";
var regEx = string.Format("(not\\s+)?{0}(.+?){0}(and|or)?",doubleQuote);
Regex RE = new Regex(regEx);
var result = (RE.Split(tempExpression)).Select (r => r.Trim());
return result.Where (re => !String.IsNullOrWhiteSpace(re)).ToList();
}
public enum LogicalOperator
{
None = 0,
And = 1,
Or = 2,
}
public static LogicalOperator ParseOperator(string expression)
{
var retval = LogicalOperator.None;
switch (expression.Trim().ToLower())
{
case "and":
retval = LogicalOperator.And;
break;
case "or":
retval = LogicalOperator.Or;
break;
default:
retval = LogicalOperator.None;
break;
}
return retval;
}
public static Condition Eval(List<string> tokens)
{
var retval = new Condition();
var tempPtr = retval;
for(var i=0;i<tokens.Count();i++)
{
if(tokens[i].ToLower()=="not")
{
tempPtr.HasNot = true;
}
else if(tokens[i].ToLower()=="and"||tokens[i].ToLower()=="or")
{
tempPtr.LogicalOperator = tokens[i].ToLower()=="and"?LogicalOperator.And:LogicalOperator.Or;
tempPtr.Right = new Condition();
tempPtr = tempPtr.MoveRight();
}
else
{
tempPtr.expression = tokens[i];
}
}
return retval;
}
public class Node
{
}
public class Condition:Node
{
public int Order {get;set;}
public string expression {get;set;}
public LogicalOperator LogicalOperator {get;set;}
public bool HasNot{get;set;}
public Condition Right {get;set;}
}
public static class ConditionExt
{
public static Condition MoveRight(this Condition node)
{
return node.Right;
}
}
public static string CreateQueryString(Condition node)
{
StringBuilder sb = new StringBuilder();
while (node!=null)
{
if(node.HasNot)
{
sb.Append("Not ");
}
sb.Append(node.expression+" ");
if(node.LogicalOperator!=LogicalOperator.None)
{
sb.Append(Enum.GetName(typeof(LogicalOperator), node.LogicalOperator) +" ");
}
node = node.Right;
}
return sb.ToString();
}
void Main()
{
//The user input expression
var expression = "\"java\" and \"SQL\" and \"C#\" or \"VB.NET\"";
//The collection of tokens that we will use to evaluate the logic in the expression
var tokens = Tokenize(expression);
//tokens.Dump();
//Our evaluated representation of the tokens; We can use this to
//convert the expression to whatever language we wish
var ConditionNode = Eval(tokens);
//Transforms our Nodes into our desired programming language
var endResult = CreateQueryString(ConditionNode );
}
public enum LogicalOperator
{
None = 0,
And = 1,
Or = 2,
}
public static LogicalOperator ParseOperator(string expression)
{
var retval = LogicalOperator.None;
switch (expression.Trim().ToLower())
{
case "and":
retval = LogicalOperator.And;
break;
case "or":
retval = LogicalOperator.Or;
break;
default:
retval = LogicalOperator.None;
break;
}
return retval;
}
public static Condition Eval(List<string> tokens)
{
var retval = new Condition();
var tempPtr = retval;
for(var i=0;i<tokens.Count();i++)
{
if(tokens[i].ToLower()=="not")
{
tempPtr.HasNot = true;
}
else if(tokens[i].ToLower()=="and"||tokens[i].ToLower()=="or")
{
tempPtr.LogicalOperator = tokens[i].ToLower()=="and"?LogicalOperator.And:LogicalOperator.Or;
tempPtr.Right = new Condition();
tempPtr = tempPtr.MoveRight();
}
else
{
tempPtr.expression = tokens[i];
}
}
return retval;
}
// Define other methods and classes here
public static List<string> Tokenize(string expression)
{
var tempExpression = System.Security.SecurityElement.Escape(expression);
string doubleQuote = "&quot;";
var regEx = string.Format("(not\\s+)?{0}(.+?){0}(and|or)?",doubleQuote);
Regex RE = new Regex(regEx);
var result = (RE.Split(tempExpression)).Select (r => r.Trim());
return result.Where (re => !String.IsNullOrWhiteSpace(re)).ToList();
}
public class Node
{
}
public class Condition:Node
{
public int Order {get;set;}
public string expression {get;set;}
public LogicalOperator LogicalOperator {get;set;}
public bool HasNot{get;set;}
public Condition Right {get;set;}
}
public static class ConditionExt
{
public static Condition MoveRight(this Condition node)
{
return node.Right;
}
}
public static string CreateQueryString(Condition node)
{
StringBuilder sb = new StringBuilder();
while (node!=null)
{
if(node.HasNot)
{
sb.Append("Not ");
}
sb.Append(node.expression+" ");
if(node.LogicalOperator!=LogicalOperator.None)
{
sb.Append(Enum.GetName(typeof(LogicalOperator), node.LogicalOperator) +" ");
}
node = node.Right;
}
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment