Skip to content

Latest commit

History

64 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

ParseLib

.NET library that provides components for dynamic parsers generation.

NuGet version (Ry.ParseLib)

Features

  • End-to-end solution with a built-in lexical analyzer.
  • Fluent Regular Expressions building interface.
  • LALR(1) grammars with a flexible conflicts handling mechanism.
  • Unicode support.
  • In-memory dynamic type generation.
  • Async sequential parser implementation out-of-the-box.

For more information check the project's wiki page or examine the samples folder.

Usage Sample

First, we need to configure a grammar itself. For sample code we will define a single number terminal and a set of rules for basic math operations:

vardigit=Rex.Char("0-9");vargrammar=newGrammar();grammar.CreateTerminals("+","-","/","*","(",")");grammar.CreateNonTerminals("expr");grammar.CreateWhitespace("ws",Rex.Char(' ').OneOrMore());grammar.CreateTerminal("expr:num",digit.OneOrMore());grammar.AddRule("expr:unary","- expr").ReduceOn("*","/","+","-");grammar.AddRule("expr:add","expr + expr").ReduceOn("+","-").ShiftOn("*","/");grammar.AddRule("expr:sub","expr - expr").ReduceOn("+","-").ShiftOn("*","/");grammar.AddRule("expr:mul","expr * expr").ReduceOn("*","/","+","-");grammar.AddRule("expr:div","expr / expr").ReduceOn("*","/","+","-");grammar.AddRule("expr:group","( expr )");

Then, we need a user-defined parser class where handers for the grammar tokens and rules are implemented. The parser also determines a target type of the output:

publicabstractclassExpressionParser:StringParser{publicExpressionParser(stringexpression):base(expression){}[CompleteToken("num")]publicintCompleteNumber()=>Int32.Parse(GetLexeme());[Reduce("expr:add")]publicintAdd(intx,inty)=>x+y;[Reduce("expr:sub")]publicintSub(intx,inty)=>x-y;[Reduce("expr:mul")]publicintMul(intx,inty)=>x*y;[Reduce("expr:div")]publicintDiv(intx,inty)=>x/y;[Reduce("expr:unary")]publicintNegate(intx)=>-x;}

Finally, based on the grammar and the parser we can build a parser factory. The factory is just a wrapper for an appropriate constructor defined by a type of the parser was generated:

varfactory=grammar.CreateStringParserFactory<ExpressionParser>("expr");varparser=factory("-2 + -(2 * 2)");parser.Parse();Console.WriteLine(parser.GetResult());

About

.NET runtime LALR parser generator

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages