I am looking for help about parsing text containing multilevel of {} to build a graph (well, sort of a tree) of every part of the string inside the outer-most {}
's
For example with this text : {a {b|c} d e | f}
I want to obtain tokens a, b, c, d, e, f . The graph would be
-> b -
/ \
-> a - -> d -> e
/ \ /
/ -> c -
\
\
-> f
I think, I need a non-regular expression grammar parser to parse the text to a sequence of tokens.
Can you help to do this with ANTLR, How can I define grammar & parser rules?
I make a C# applications to do parse the text and display the graph.
BR.
Christian.
Rem: Excuse me for my poor English, I'm french.
grammar L ;
debut : ( niveau )? EOF ;
niveau : ( Feuille | branche ) ( niveau )? ;
branche : '{' niveau '|' niveau '}' ;
Feuille : ~[{}| \t\r\n]+ ;
AttacheG : '{' ;
AttacheD : '}' ;
Barre : '|' ;
Espaces : [ \t\r\n]+ -> skip;
$ antlr4 L.g4
$ javac *.java
$ grun L debut -tokens -tree -gui << END
> {a {b|c} d e | f}
> END
[@0,0:0='{',<2>,1:0]
[@1,1:1='a',<1>,1:1]
[@2,3:3='{',<2>,1:3]
[@3,4:4='b',<1>,1:4]
[@4,5:5='|',<4>,1:5]
[@5,6:6='c',<1>,1:6]
[@6,7:7='}',<3>,1:7]
[@7,9:9='d',<1>,1:9]
[@8,11:11='e',<1>,1:11]
[@9,13:13='|',<4>,1:13]
[@10,15:15='f',<1>,1:15]
[@11,16:16='}',<3>,1:16]
[@12,18:17='<EOF>',<-1>,2:0]
(debut (niveau (branche { (niveau a (niveau (branche { (niveau b) | (niveau c) }) (niveau d (niveau e)))) | (niveau f) })) <EOF>)
$
grammar MSpin ;
/* la version claire
debut : ( corps )? EOF ;
corps : ( Texte | choix | niveau )+ ;
choix : '{' corps ( '|' corps )+ '}' ;
niveau : '{' corps '}' ;
*/
// la version inline, montrant un arbre simple
debut : ( ( Texte | choix | niveau )+ )? EOF ;
choix : '{' ( Texte | choix | niveau )+ ( '|' ( Texte | choix | niveau )+ )+ '}' ;
niveau : '{' ( Texte | choix | niveau )+ '}' ;
Texte : ~[{}| \t\r\n]+ ;
Greg, Merci.