Skip to content

Instantly share code, notes, and snippets.

@Dervall
Created October 30, 2012 09:32
Show Gist options
  • Save Dervall/3979248 to your computer and use it in GitHub Desktop.
Save Dervall/3979248 to your computer and use it in GitHub Desktop.
Stupid parsing
var configurator = ParserFactory.Configure<object>();
var price = configurator.CreateTerminal("[0-9]+,[0-9]* ", s => double.Parse(s.Substring(0, s.Length - 1)));
var unknownText = configurator.CreateTerminal("((-|[A-Öa-ö])[^\n]+)|[0-9]+,[0-9]*kr[^\n]*\n", s => s);
var reciept = configurator.CreateNonTerminal();
var recieptLine = configurator.CreateNonTerminal();
reciept.AddProduction(reciept, recieptLine);
reciept.AddProduction(recieptLine);
recieptLine.AddProduction(price, unknownText).SetReduceFunction(p =>
{
Console.WriteLine("Found item " + p[1] + " with price " + p[0]);
return null;
});
recieptLine.AddProduction(unknownText).SetReduceFunction(p =>
{
Console.WriteLine("Unknown text " + p[0] + " was found");
return null;
});
var parser = configurator.CreateParser();
parser.Parse(recieptText);
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment