0

I am looking for help about text containing multilevel of braces

For example with this text :

{{abc|{cde|fgh}} bb|cc}

I want to obtain

{abc bb|cde bb|fgh bb|cc}

and with this

pp {vv {ff|ii|nn|aa} | {ee|hh|rr} } xx {{abc|{cde|fgh} bb|cc}

the results is

pp {vv ff|vv ii|vv nn|vv aa | ee|hh|rr} xx {abc bb|cde bb|fgh bb|cc}

the idea is to put the text with several levels of braces only one level of braces.

How can I do that? I want to do this in C#.

LeMoussel
  • 5,290
  • 12
  • 69
  • 122

4 Answers4

1

I'll just give you the basic idea, as opposed to writing the code for you.

Build a tree (well, sort of a tree) of every part of the string inside the outer-most {}'s (a {b} c {d} would have 2 trees - one for {b} and one for {d}). Each element following on another must be a child of that element. Whenever there are multiple options, each of them must be a child of the previous node and each of them must have the next node as a child.

So for, for example, {a {b|c} d e | f}, you'd have:

         -> b -
        /      \
  -> a -        -> d -> e
 /      \      /
/        -> c -
\
 \
  -> f

Then generate all paths from the root, using depth-first search for example.

The paths are -> a -> b -> d -> e and -> a -> c -> d -> e and -> f,
so we'd have {a b d e | a c d e | f}.

Implementation hints:

A stack might be a good idea for keeping track of the nodes.

It might be wise to have blank intermediate nodes before and after {}'s (trust me, this would make it a lot easier to implement).

So {{a|b} {c|d}} would look like:

       -> a -        -> c -
      /      \      /      \
-> . -        -> . -        -> .
      \      /      \      /
       -> b -        -> d -
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • Thank you for your help. Another implementation is to use [QuickGraph](http://quickgraph.codeplex.com/) with and applying a [depth-first-search algorithm](http://quickgraph.codeplex.com/wikipage?title=Depth%20First%20Search%20Example) on a graph. – LeMoussel Nov 06 '13 at 14:37
  • The problem is that I do not know how to find the vertices (a, b, c, ...) because I do not know the use of parsers. – LeMoussel Nov 06 '13 at 14:45
  • @PapyRef `{`, `|` and `}` should be the only characters you need to look out for. Whatever's between any of those can basically be taken as a vertex. Take a few (trivial and non-trivial) examples and create the trees by hand. Then try to define explicit steps for what you need to do and each of those 3 characters (keeping in mind that the stack is all-important here). – Bernhard Barker Nov 06 '13 at 15:28
1

Lexer rules (I use NLT format):

"|" -> PIPE;
/[a-z]+/ -> ID;
/[ ]+/ -> WS;
"{" -> LBRACE;
"}" -> RBRACE;
%EOF -> EOF;

And parser:

s -> n:node { n };
node -> (PIPE- n:node)+ { new SeqNode("|",n) }
      | (WS- n:node)+ { new SeqNode(" ",a) }
      | LBRACE n:node RBRACE { new BracesNode(seq) }
      | id:ID { new SeqNode("",new string[]{id}) }
      ;  

I am writing this off the top of my head ;-).

Define SeqNode and BracesNode to get a tree. Next traverse it, and count how many times you hit BracesNode -- when the counter says "the first time", recreate braces, if more -- ignore them.

greenoldman
  • 16,895
  • 26
  • 119
  • 185
  • Very interesting tool. But I found little documentation for beginners for NaiveLanguageTools format and C# example code (I only find some code in Example project). – LeMoussel Nov 07 '13 at 18:39
  • @PapyRef There is a documentation about syntax in generator project, and I wrote this piece 2 days ago: http://www.codeproject.com/Articles/679139/Ambiguity-Let-NLT-parse-it-for-you – greenoldman Nov 08 '13 at 06:47
0

You'll need a non-regular expression grammar parser in order to parse the text to a sequence of tokens. See How to write a Parser in C#? for advice on writing such parsers.

Compared with that, reformatting the tokens (probably via a binary expression tree) should be fairly straightforward.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131
0

If there are oprators right outside braces that has higher precedence than the lowest of those of oprators inside, you keep the braces,otherwise you drop them