80 likes | 295 Views
NPEG - .NET Parsing Expression Grammar. Leblanc Meneses Robust Haven Inc. What is it?. Implements PEG Bryan Ford – PEG @ MIT 2004 Martin.Holzherr – 2008 codeproject Other Comparable Frameworks: LL(*) ANTLR , LALR(1) Irony
E N D
NPEG - .NET Parsing Expression Grammar Leblanc Meneses Robust Haven Inc.
What is it? • Implements PEG • Bryan Ford – PEG @ MIT 2004 • Martin.Holzherr – 2008 codeproject • Other Comparable Frameworks: LL(*) ANTLR , LALR(1) Irony • How do they handle predicates (lookahead/lookbehind) how far? • How they handle ambiguity • ScannerlessParser (no need for scanner or parser code generation) – run in memory • Infinite lookahead, doesn’t care about ambiguity – prioritized choice. • Today: Can be deployed natively to C, C++, C#, and Java • Embedded, Mobile, Desktop • TDD Built – so the framework has a solid regression harness. To continue it’s up keep. • Can READ and WRITE itself! – DSL is written with NPEG.
Where is it used? • Custom DSL (NPEG) – Domain Specific Languages • Parse any proprietary format (Gerber files) • Flexibleconfig • Configuration based on product, branch, developer, environment, task • Staging, production, production snapshot, disaster recovery environments • When web.config transformations become cumbersome • SqlObfuscate • Production data back to development – RegexObfuscateStrategy • Content Management System
Backlog Items • Language Workbench *** • Unit test terminals/nonterminals • Output to all supported languages • Better error reporting and debugging rules • Save Memory - remove “Value” from the AstNode • Should be using iterator->Text(node.Start, node.End) • Build a grammar optimizer • Collapse rules -> “x” “x” “x” -> “x”{3} • De Morgans Theorem • Upcoming: Javascript & PHP (already started) implementation • Browser – example: knockoutjs clone. • Research: • Language Implementation
How to start? • Terminals, Non Terminals – Predicates ***
Hello world string input = "hello world"; AExpressionparseTree= PEGrammar.Load(@"(?<Expression>): 'Hello World'\i;"); visitor = new NpegParserVisitor(new StringInputIterator(input)); parseTree.Accept(visitor); Assert.IsTrue(visitor.IsMatch); AstNodenode = visitor.AST; Assert.IsTrue(node.Token.Name == "Expression"); Assert.IsTrue(node.Token.Value == input);
Boolean Algebra S: [\s]+; (?<Gate>): ('*' / 'AND') / ('~*' / 'NAND') / ('+' / 'OR') / ('~+' / 'NOR') / ('^' / 'XOR') / ('~^' / 'XNOR'); ValidVariable: '""' (?<Variable>[a-zA-Z0-9]+) '""' / '\'' (?<Variable>[a-zA-Z0-9]+) '\'' / (?<Variable>[a-zA-Z]); VarProjection1: ValidVariable / (?<Invertor>'!' ValidVariable); VarProjection2: VarProjection1 / '(' Expression ')' / (?<Invertor>'!' '(' Expression ')'); Expression: S? VarProjection2 S? (Gate S? VarProjection2 S?)*; (?<BooleanEquation>): Expression !.; • ((A)*(!(B))+(!(A))*(B)) • ((((!X*Y*Z)+(!X*Y*!Z)+(X*Z)))) • 'aA'*!'bB'+!'aA'*'bB'
Feedback • Leblanc Meneses • @leblancmeneses • http://about.me/leblancmeneses • https://github.com/leblancmeneses • leblanc@robusthaven.com