Parse this string
-
Hey guys!
I have a text like this:GUIApplication { title: "Hello World"; width: 600px; height: 450px; location: "center"; onLoad: { MessageBox("Hello World"); }; Button { text: "Click on me!"; x: 10px; y: 10px; onClick: { Console.Log("You clicked."); }; }; };
This is not QML!!
How can I parse this syntax?!
For example, how to get properties and events??!Thanks!!
-
Hello,
You have several options depending on your exact requirements:- Try to use QJsonDocument to provide the parsing (if this is valid JSON, which I'm not sure that it is).
- Try to use an external library for parsing (boost for example).
- Write your own simple top-down parser.
Kind regards.
-
Thank you "kshegunov".
This is not valid JSON. I have created it own.
You said: "Write your own simple top-down parser.", well How to do that?!
I mean, use "Regular Expressions" or something like that?! -
Besides kshegunov excellent points,
You could change syntax slightly so it was valid json and
then you would have many parsers.Or you could take something like
http://sourceforge.net/projects/supereasyjson/
and make it read your format.Do you care about the nesting ?
as getting the values is pretty easy but if u need to maintain the hierarchy
then it's far more involving.IF you want to write your own parser then I found this good info
http://www.dreamincode.net/forums/topic/234775-creating-a-recursive-descent-parser-oop-style/ -
@AliReza-Beytari
Hello,
If this is your own format, I'd suggest first to reconsider and use something that's instead available to standard parsers. You could use XML to the same effect., which is readily extensible and can be parsed through standard means. If you're still insistent on using your own format then the boost's spirit module might be your best bet. With it you define your language's grammar and it allows for parsing based on that grammar. If you still want to write your own parser, then @mrjj's suggestion is a good starting point. Usually when you need to write a parser you start from the language's grammar and based on it you create a tokenizer that breaks up the input into lexemes. Then from the tokens you build up a parse tree that can be handled by the parser itself. The simplest is to have LL(1) parser (recursive-descent with a single token look-ahead) but it depends on the grammar really. Most will use an automated tool to provide the lexing and parsing (i.e. Flex and Bison) but this is not so much a requirement as it's convenience, because if the grammar is complex (which usually is the case) writing the parser by hand might get very involved. A few years ago I had to write a C++ syntax parser (no semantics) used to change formatting for source files, but unfortunately that code is copyrighted and I can't share it with you. As a guide, after tokenization, you mirror the grammar in the source code (like in @mrjj's example) and from there you handle the parse tree directly. You could go around the internet and look up some ready-made parsers (there are quite a few) and try to deduce how it's done. I hope this helps.Kind regards.