NPEG - .NET Parsing Expression Grammar



June 9, 2012
Leblanc Meneses
Improving Startups
817-226-5740
leblanc@
leblancmeneses
@leblancmeneses
leblancmeneses

What is it?

  • Implements PEG
  • Other Comparable Frameworks: LL(*) ANTLR , LALR(1) Irony
    • How do they handle predicates (lookahead/lookbehind) how far?
    • How they handle ambiguity
  • Scannerless Parser (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; widgets, markdown

Retail

retail 01
retail 02

How to start?

NPEG package

Math Expression

cos(25)+cos(15+sin(25.333))+3

math expression

Hello world

string input = "hello world";


AExpression parseTree= PEGrammar.Load(@"(?< Expression >): 'Hello World'{{'\'}}i;");
visitor = new NpegParserVisitor(new StringInputIterator(input));
parseTree.Accept(visitor);
Assert.IsTrue(visitor.IsMatch);
AstNode node = visitor.AST;
Assert.IsTrue(node.Token.Name == "Expression");
Assert.IsTrue(node.Token.Value == input);

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)
    • How they handle ambiguity
  • Build a grammar optimizer
    • Collapse rules -> “x” “x” “x” -> “x”{3}
    • De Morgans Theorem
  • Upcoming: Markdown
  • Research:
    • Language Implementation

Feedback

Leblanc Meneses
Improving Startups
817-226-5740
leblanc@
leblancmeneses
@leblancmeneses
leblancmeneses