Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parse this string

Parse this string

Scheduled Pinned Locked Moved Unsolved General and Desktop
parse
5 Posts 3 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliReza Beytari
    wrote on 31 Dec 2015, 17:24 last edited by
    #1

    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!!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 31 Dec 2015, 17:38 last edited by
      #2

      Hello,
      You have several options depending on your exact requirements:

      1. Try to use QJsonDocument to provide the parsing (if this is valid JSON, which I'm not sure that it is).
      2. Try to use an external library for parsing (boost for example).
      3. Write your own simple top-down parser.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        AliReza Beytari
        wrote on 31 Dec 2015, 17:51 last edited by
        #3

        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?!

        K 1 Reply Last reply 2 Jan 2016, 04:11
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Jan 2016, 00:22 last edited by
          #4

          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/

          1 Reply Last reply
          0
          • A AliReza Beytari
            31 Dec 2015, 17:51

            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?!

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 2 Jan 2016, 04:11 last edited by kshegunov 1 Feb 2016, 04:12
            #5

            @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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0

            5/5

            2 Jan 2016, 04:11

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved