Results 1 to 14 of 14

Thread: java parser

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default java parser

    Hello,
    I must write a descendant recursive parser in Java. Do you know some good resources, please?

    Thanks,
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java parser

    The descendant recursive parser IMO is the most intuitive and easy one, compared to other parsers like LL or LR, as long as you have a correct grammar :-)

    I think you can find it in almost all the text book teaching compiler principle.

    You can also use something like bison/yacc to implement the parser too.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    I need a LL1 grammar and that generators create a complex parser for my aims. I need a basic parser...
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java parser

    Well, here's a compiler I wrote years ago, hope it would be useful:
    http://www.supnate.com/qianhaib/cminus_0.4.zip
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    thanks, but it's much complex for me. I'd like something like the parser in c++ stroustrup book. Very simple but it isn't in java and not complete...
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java parser

    then how would you like us to help you?
    maybe you can post the grammer here
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    Quote Originally Posted by bood View Post
    then how would you like us to help you?
    maybe you can post the grammer here
    hoping it's LL1: the parser must accept things like this below
    Qt Code:
    1. <send>
    2. <target>
    3. <string name="find"> Primer <string>
    4. </target>
    5. </send>
    To copy to clipboard, switch view to plain text mode 
    The grammar should be something like this (I write it in Coco/r manner; how can I write it with "Dragon book" sintax?):
    Qt Code:
    1. Xpml = Send
    2. Send = "<send>" Target { Target } "<send>"
    3. Target = "<target>" (String | Number) { String | Number } "</target>"
    4. String = <string>" "name" "=" "\"" Char "\"" ">" Char "</string>"
    5. Number = "<integer>" "name" "=" "\"" Char "\"" ">" Char "</integer>"
    6. Char = char - '"'
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 18th September 2008 at 16:11.
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java parser

    Yes, I believe it could be converted to a LL1 grammer
    however, LL1 does not allow any two sentences have the same beginning, so a trick is made here (actually should be done in your lexer) to treat "<send>" "<target>" "<string" "<integet" as a whole (they all begin with "<").
    Xpml = Send
    Send = Send_Start Target { Target } Send_End
    Target = Target_Start (String | Number) { String | Number } Target_End
    String = String_Start name="string" > string String_End
    Number = Integer_Start name="string" > number Integer_End
    Char = char - '"'
    Then the parser is easy to implement, take Target for example:
    Qt Code:
    1. parseTarget()
    2. {
    3. expectToken(Target_Start);
    4. token=getToken();
    5. while(token!=Target_End) {
    6. if(token==String_Start) parseString();
    7. else if(token==Number) parseNumber();
    8. else error();
    9. }
    10. expectToken(Target_End);
    11. }
    To copy to clipboard, switch view to plain text mode 
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    Yes, I forget the '<' at begin; but isn't this fine too?
    Qt Code:
    1. Xpml = Send
    2. Send = "<send>"
    3. "<target>"
    4. ("<string>" "name" "=" '"' Char '"' ">" CharText "</string>"
    5. | "<integer>" "name" "=" '"' Char "\"" ">" CharText "</integer>")
    6. "</target>"
    7. { "<target>"
    8. ("<string>" "name" "=" '"' Char "\"" ">" CharText "</string>"
    9. | "<integer>" "name" "=" '"' Char '"' ">" CharText "</integer>")
    10. "</target>" }
    11. "</send>"
    12. Char = char - '"'
    13. CharText = char - '<' //maybe it's necessay tihs too
    To copy to clipboard, switch view to plain text mode 
    It's very ugly but it should be ok...
    * for the parser: I wonder how can implement a sysmbol table in a very simple way? What do you suggest, please?
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java parser

    Yes, I forget the '<' at begin; but isn't this fine too?
    Well, basically no difference to the parser, only concern is the lexer

    for the parser: I wonder how can implement a sysmbol table in a very simple way? What do you suggest, please?
    You can just use the std::map as the symbol table. But why, I don't see anywhere can reference a string or number in your grammar.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    isn't Char and CharText two strings??
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: java parser

    Quote Originally Posted by mickey View Post
    isn't Char and CharText two strings??
    lexer will treat them as the same token though, I don't think lexer can tell the difference.
    btw, why don't you just make them as tokens that only can be made of alphabet, numbers and underlines? just as ids in C language.
    Last edited by bood; 19th September 2008 at 23:28.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    Quote Originally Posted by mickey View Post
    hoping it's LL1: the parser must accept things like this below
    Qt Code:
    1. <send>
    2. <target>
    3. <string name="find"> Primer </string>
    4. </target>
    5. </send>
    To copy to clipboard, switch view to plain text mode 
    Hello,
    here I did something like this:
    Qt Code:
    1. Token nextToken() {
    2. while ( done ) {
    3. char = (char) _data[_pos++]; // I read the entire file into a block of memory
    4. switch (state) {
    5. case 0 : if (ch == '<') state = 1; break;
    6. case 1 : if (ch == 's') state = 2; break;
    7. case 2 : if (ch == 'e') state = 3; break;
    8. case 3 : if (ch == 'n') state = 4; break;
    9. case 4 : if (ch == 'd') state = 5; break;
    10. case 5 : if (ch == '>') state = 6; return OpenSend;
    11. case 6: .......// all other
    12. case n: if (ch == '<') state = n+1; break;
    13. case n+1 : if (ch == '/') state = n+2; break;
    14. //and so on to recognize "send" and ">"
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    I wonder: Must I to reconize in the scanner two time the string "send". Is there any tricks to avoid this? ( I mean, I'd like to avoid to write states that recognizes the same "string". I have the same problem with <string></string> and <target></target>).
    Hope it's clear...
    thanks,
    Regards

  14. #14
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: java parser

    sorry, Could anybody get me any suggestions, please?
    Regards

Similar Threads

  1. java reflection
    By mickey in forum General Programming
    Replies: 1
    Last Post: 24th July 2008, 21:17
  2. Sending string from QT client to Java server
    By seanmu13 in forum Newbie
    Replies: 3
    Last Post: 3rd July 2007, 12:20
  3. Qt4 open src mingw from a PC to another
    By nvictor in forum Installation and Deployment
    Replies: 11
    Last Post: 1st May 2007, 17:41
  4. Parser in Qt.... HELP!
    By deepusrp in forum Newbie
    Replies: 6
    Last Post: 23rd January 2007, 21:18
  5. Embedding Java in Qt
    By learnerplts in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 15:45

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.