Results 1 to 3 of 3

Thread: Algorithms problem of brackets including.

  1. #1
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Algorithms problem of brackets including.

    My problem is :
    I want to write a application aim to translate some information within a special formatted text to a custom struct. The organism like brackets is allowed in the text.
    For example, we can use M31 to open a section ,and use M30 to close the section. Within each section another some sections(also opened by M31 and closed by M30) can exist. And these inner sections was parallel.

    Below is a practical example:

    Qt Code:
    1. M31 //Open the outerest level section, we can call it the first level section
    2.  
    3. M31 //Open the second level section, which is included in the first level section.
    4. M31 //Open the third level section, which is included in the second level section.
    5. some operate code // Some codes belong to the third level section.
    6. M30 //Close the third level section.
    7. some operate code //Codes belong to the second level section. And more important
    8. is these codes will make some changes on the result of the
    9. third level codes. This is where is my problem. I can not figure
    10. out how to deal with this relationship.
    11. M30 //Close the second level section.
    12.  
    13. M31 //Open another second level section, which is parallel with the existing second
    14. level section.
    15. M31
    16. some operate code
    17. M30
    18. some operate code
    19. M30 //Close the second parallel second level section.
    20. some operate code //The codes belong to the outerest level section(the first level
    21. section). And these codes will also make some changes on
    22. the results of the two inner second level sections.
    23. M30 //Close the outerest section(the first level section).
    To copy to clipboard, switch view to plain text mode 


    I want to use somethind like List or Stack to solve the problem. But when I try to recognize which is the level of the operate codes within a section, I have some problems. Because the codes of outter section will be excuted after the codes of inner section. So it is very important to recognize WHICH level the codes are belong to.

    Thanks a lot!

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Algorithms problem of brackets including.

    I don't know the details of your parser but this doesn't sound so hard. You can either create a model-like infrastructure with a given type of objects, each having content and child(ren) or use a context stack, pushing one level up each time your encounter a M31 token and popping one level down each time you encounter a M30 token. There is no best solution that can handle any problem... Actually the first option is well-suited for maintaining tree of your data, iterating it in any order, possibly modifying it and saving it back. On the other, the second approach is generally chosen when the data is processed sequentially and do not need to be preserved/modified after having been parsed.

    Hope this helps.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Algorithms problem of brackets including.

    For simplification of the problem you might want to take a look at how mathematical expressions are parsed. For instance, convert infix expressions to postfix expressions (i.e. "9 + 5 - 2" turns into "9 5 + 2 -", "9 + 5 * 2" to "9 5 2 * +" et cetera). That way you can easily create a stack for your commands.

    The exact rules are defined by your set of operators, and the operator precedences.
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

Similar Threads

  1. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.