picoe / Eto.Parse

Recursive descent LL(k) parser for .NET with Fluent API, BNF, EBNF and Gold Grammars
MIT License
148 stars 30 forks source link

EBNF Sample code on homepage doesn't parse as expected (returns error) #13

Closed DanTup closed 4 years ago

DanTup commented 10 years ago

The sample code on the homepage doesn't appear to work as expected:

// Copied from https://github.com/picoe/Eto.Parse
var grammar = new EbnfGrammar().Build(@"
(* := is an extension to define a literal with no whitespace between repeats and sequences *)
ws := {? Terminals.WhiteSpace ?};

letter or digit := ? Terminals.LetterOrDigit ?;

simple value := letter or digit, {letter or digit};

bracket value = simple value, {simple value};

optional bracket = '(', bracket value, ')' | simple value;

first = optional bracket;

second = optional bracket;

grammar = ws, first, second, ws;
", "grammar");

var input = "  hello ( parsing world )  ";
var match = grammar.Match(input);

var firstValue = match["first"]["value"].Value;
var secondValue = match["second"]["value"].Value;

Console.WriteLine("F: {0}", firstValue);
Console.WriteLine("S: {0}", secondValue);
Console.WriteLine(match.ErrorMessage);

When run, this outputs:

F:
S:
Index=24, Context="ing world >>>)  "
ChildIndex=27, Context=" world )  >>>"
Expected:
letter or digit: Char: Letter or Digit
simple value: Sequence
cwensley commented 10 years ago

There is an outstanding issue where the errors will be populated even when successful. You should check the match.Success property first before looking at the Errors or ErrorMessage properties.

cwensley commented 4 years ago

Closing as this is really old and I believe it was answered.