View on GitHub

Pegasus

Super-easy PEG Parsing for .NET

Features

Pegasus is a PEG (Parsing Expression Grammar) parser generator for .NET that integrates with MSBuild and Visual Studio.

Syntax Example

Mathematical Expression Evaluator

@namespace PegExamples
@classname MathExpressionParser
@using System.Globalization

start <double>
  = _ value:additive _ EOF { value }

additive <double> -memoize
    = left:additive _ "+" _ right:multiplicative { left + right }
    / left:additive _ "-" _ right:multiplicative { left - right }
    / multiplicative

multiplicative <double> -memoize
    = left:multiplicative _ "*" _ right:power { left * right }
    / left:multiplicative _ "/" _ right:power { left / right }
    / power

power <double>
    = left:primary _ "^" _ right:power { Math.Pow(left, right) }
    / primary

primary <double> -memoize
    = decimal
    / "-" _ primary:primary { -primary }
    / "(" _ additive:additive _ ")" { additive }

decimal <double>
    = value:([0-9]+ ("." [0-9]+)?) { double.Parse(value, CultureInfo.InvariantCulture) }

_ = [ \t\r\n]*

EOF
  = !.
  / unexpected:. #error{ "Unexpected character '" + unexpected + "'." }