Search code examples
c#parsingmgrammar

parse specific data in C#


I have data stored in specific text format:

FIDS_A1=CF_LAST:1|line_NETCHNG:2|QoS:3; FIDS_A2=[High and Low]:[{High} – {Low}]:1|CF_LAST:2; FIDS_A3=YR_RANGE:3|VOL:3; FIDS_A4=GR_AskBid; FIDS_C3=line_BID:3|line_ASK:3;

I need to parse it and get a C# typed data structure from it.

It's not simple to write parser in C# (very many Regexps and hard code).

I heard something about Oslo\MGrammar from Microsoft. Does this tool generate C# parser code for my specific data ?

Output i need only C# code of parser without reference to other libraries.


Solution

  • Parser generators don't help you avoid regexes. In fact, at least for the generators I used, a parser generator is a second stage of parsing. It accepts a stream of tokens, and outputs an abstract syntax tree.

    In order to convert text to tokens, you would write a lexer, and that can include a regex or three.

    If the language is simple enough, you might find it less work to write a parser from scratch than to learn how to use a parser generator.