Results 1 to 14 of 14

Thread: Parsing Files

  1. #1

    Default Parsing Files

    Hey Guys,

    I'm looking for an professionell way for parsing Text files.
    How to start here with Qt? Is there any "Qt-way" or must I realize this with regular expressions?

    My purpose is, parsing C++ header files ...

  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: Parsing Files

    Quote Originally Posted by JayJay View Post
    My purpose is, parsing C++ header files ...
    What kind of parsing do you want to achieve? Building an AST? Extracting symbols/tags? Highlighting? Something else? The goal will affect the implementation but, in most case, a good old hand-made parser performs best with languages as complex as C++.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3

    Default Re: Parsing Files

    I want to extract class, functions and member informations of an header file.
    Handmade means some orgies with QRegExp?!

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Parsing Files

    I don't think you have to use QRegExp that much. I'd say, first separate the header file by its whitespace (and the places between words and special characters) to get a list of tokens. Then go through the list with some sort of finite state machine next to it.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Parsing Files

    If You want store some settings, easiest way is to use QSettings.

  6. #6
    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: Parsing Files

    Quote Originally Posted by JayJay View Post
    I want to extract class, functions and member informations of an header file.
    And what do you want to do with the extracted data?

    Quote Originally Posted by JayJay View Post
    Handmade means some orgies with QRegExp?!
    Not at all! Handmade means crafting a lexer and a parser in C++. The lexer reads a character stream and turns it into a sequence of token (or a token stream). Then the parser analyzes the tokens and builds a tree/tag list/whatsoever you want.

    If you don't want to waste time writing a parser from scratch you can have a look at the one used by Qt tools (lupdate or qt3to4, or both) or generate one using yacc/ANTLR or akin.
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #7
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Files

    Quote Originally Posted by JayJay View Post
    I want to extract class, functions and member informations of an header file.
    Handmade means some orgies with QRegExp?!
    how about another direction: run ctags on that file, and parse the tags file.

  8. #8

    Default Re: Parsing Files

    Storing isn't that problem. An Alghorithm for parsing these files is the problem ...

    After cutting all whitespaces, I've to go through it via QRegExp I think ... The best way I think ...

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Files

    A lexer can be implemented in any number of ways. Using regular expressions is one of the hardest/slowest.

    I say to look in the opposite direction, towards finite automata , especially if you are interested in only one language.

    Regards

  10. #10
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Parsing Files

    Quote Originally Posted by JayJay View Post
    Storing isn't that problem. An Alghorithm for parsing these files is the problem ...

    After cutting all whitespaces, I've to go through it via QRegExp I think ... The best way I think ...
    Why? I don't think you'll need QRegExp at all. What's your plan?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  11. #11

    Default Re: Parsing Files

    Quote Originally Posted by fullmetalcoder View Post
    Not at all! Handmade means crafting a lexer and a parser in C++. The lexer reads a character stream and turns it into a sequence of token (or a token stream). Then the parser analyzes the tokens and builds a tree/tag list/whatsoever you want.
    Easy to read ... But how to write? You've got some examples or links?

    I don't want to use ctags, no depency is the best depency ...

  12. #12
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Parsing Files

    Quote Originally Posted by JayJay View Post
    Storing isn't that problem. An Alghorithm for parsing these files is the problem ...

    After cutting all whitespaces, I've to go through it via QRegExp I think ... The best way I think ...
    bad idea, you are not aware of the context in which the string is found. some bad examples (FullMetalCoder will be able to explain about NFA and DFA, I suggest you to read about Turing machines as well)

    Qt Code:
    1. QString s = "class a { int foo; };";
    2.  
    3. // class a { int foo; };
    4.  
    5. #if 0
    6. class a
    7. {
    8. int foo;
    9. };
    10. #endif
    11.  
    12. /*
    13. class a
    14. {
    15.   int foo;
    16. };
    17. */
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Parsing Files

    Quote Originally Posted by elcuco View Post
    bad idea, you are not aware of the context in which the string is found. some bad examples (FullMetalCoder will be able to explain about NFA and DFA, I suggest you to read about Turing machines as well)
    Note to JayJay: That's (Non)Deterministic Finite Automata. And while Turing Machines are very interesting, I don't think they're relevant for this at all.

    Anyway, I think everyone here is pointing you in the right direction. You need to create a token-stream/list and go through them with a finite automaton. But how formal and elaborate you want to make your parser really depends on what kind of stuff you expect to find in these header files. Are they more predictable than elcuco's example?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  14. #14
    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: Parsing Files

    Quote Originally Posted by JayJay View Post
    Easy to read ... But how to write? You've got some examples or links?
    Hehe... That's just so true! Well, I've already suggested to look into Qt sources. The lexer/parsers used by the porting tool (just checked ), which are also used by the HEAD version of KDevelop BTW, are ready to use, though I've never tried to actually use them. If all you is "lesser" parsing (only symbol/tags) put into a tree then you can consider using QCodeModel 2. It's a small module I made for that very usage (for Edyuk) and which works pretty well turning full Qt headers into a tree in about 6 seconds... http://edyuk.svn.sf.net/svnroot/edyu...ty/qcodemodel2 (do a checkout... you can't dl the sources from there...)
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. XML Parsing in Qt 3.3
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2007, 18:54
  2. Replies: 5
    Last Post: 22nd September 2006, 08:04
  3. Parsing of Text files in Qt
    By shailesh in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2006, 15:35
  4. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28
  5. Visual Studio 2003 and .vcproj files
    By mbjerkne in forum General Discussion
    Replies: 2
    Last Post: 1st February 2006, 00:51

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
  •  
Qt is a trademark of The Qt Company.