parse specific data in C#

366 views Asked by At

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.

3

There are 3 answers

0
Roman Starkov On BEST ANSWER

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.

0
grzeg On

Reading this update on M's status I don't see it coming to the market very soon.

I suggest using ANTLR, which:

  • is able to generate very powerful parsers in C# among others.
  • is a very mature product - has it's own IDE with debugger
  • uses standard EBNF grammars, so you won't invest your time in something that will die out soon
2
Lucero On

You could have a look at the GOLD Parser Builder and the bsn GoldParser engine (which can create a typed data structure when parsing the data using a grammar built with GOLD).

There's also a CodeProject article which shows how to use this engine.