Created
October 30, 2012 09:32
-
-
Save Dervall/3979248 to your computer and use it in GitHub Desktop.
Stupid parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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