Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 49

Thread: java parser

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default java parser

    Hello everybody
    I've got a question. I want to know how can I write a parser
    "There is a table(dimension: 3 in 4 in 5 meters) in the room"
    in a way that the output would be this:
    Type: table
    Numbers: 1
    Size: 60 M^3
    Position: Room

    I tried substring and split method, but I didn't get the result that I wanted. Could you please help me with this?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: java parser

    Post your code and ask specific questions. Noboidy will provide ready made code.

  3. The Following User Says Thank You to PhHein For This Useful Post:

    Lotus2013 (October 21st, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    Quote Originally Posted by PhHein View Post
    Post your code and ask specific questions. Noboidy will provide ready made code.
    I'm not looking for ready made code. I just want some guidance on this. For example, which method can I use? What are the useful methods to write this code with?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    The String class has some useful methods for separating parts of a String into tokens. What were your problems with its methods?
    Also the StreamTokenizer has some useful methods for doing the same.
    The semantics of the tokens is up to the programmer.

    Start by defining the syntax of the statements you want to parse so the tokens can be organized.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 21st, 2013)

  7. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    I used the substring method and split method but there are some points which I should pay attention to. One is that the verb here can change. for example if we have two or more tables, then the verb will be are not is. So I really don't know what to do and also if there would be 3 tables, then "table", will change to "tables".

  8. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: java parser

    Does the position of these verbs and nouns change in the sentence?

  9. The Following User Says Thank You to PhHein For This Useful Post:

    Lotus2013 (October 21st, 2013)

  10. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    No, the position does not change. But the sentence itself may change. for example we may have: There are 67 whiteboards(dimension: 3 in 5 meters) in the class.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    Have you looked at defining a syntax for the statements that you are trying to parse?
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 21st, 2013)

  13. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    I guess I'd better use String tokenizer I think It's much more efficient than split method. What is your opinion?

  14. #10
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: java parser

    You cannot decide what to use until you have finalized the syntax. You haven't answered Norm's question.

  15. The Following User Says Thank You to PhHein For This Useful Post:

    Lotus2013 (October 21st, 2013)

  16. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    It all depends on the syntax and how the parsing is to be done. For example are all the tokens delimited by spaces?
    The split() method uses a regular expression that can be powerful for some parsing. The StringTokenizer can return delimiters which can be useful. The StreamTokenizer has several useful methods that can tell you about the next token it has found.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 21st, 2013)

  18. #12
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    Sorry I'm very new to the programming so I don't really get Norm's question

    --- Update ---

    Yes, all the tokens are delimited by space. The other thing is that the number may change, so the verb will also change. The object which the sentence is about can be a cube, a cone or spheroid . I should determine the volume of the object and the type of the volume e.g M^3 or M^2 ....

  19. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    Writing a parser for a statement is easier if that statement's contents are well defined.
    Consider the java language. It has several different types of statements that are all well defined. The parser uses the syntax rules as it reads the tokens in a statement to make decisions about where each token fits to make a valid statement.
    So far the statements you have posted all start with: "There"
    followed by either "is" or "are"
    and so on through the rest of the statement.

    Writing a parser is not a first year programming project.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 21st, 2013)

  21. #14
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: java parser

    find a method to generalize the input you parse. What is the general structure you're parsing? what is easy (the number of items, location)? what is difficult (dimensions)?

    EDIT: Norm's reply is better. Read that.
    Last edited by PhHein; October 21st, 2013 at 10:08 AM. Reason: being slow

  22. The Following User Says Thank You to PhHein For This Useful Post:

    Lotus2013 (October 21st, 2013)

  23. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    I haven't worked with writing parsers for years. In school we studied different language syntax rules and used BNF to define a language for a compiler writing project. I haven't done much with those techniques in a long time.
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 21st, 2013)

  25. #16
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    I think I understood what you mean. I'll try to define the syntax and the rules for the parser. Thank you so much This is my first time to ask a question in a forum and I'm really happy about it.

    Actually, I've worked with C language. but It has been a long time like a year and now I started to learn the Java language.

    --- Update ---

    Thank you So much PhHein

    --- Update ---

    WOW! 40 years of programming experience! really WOW

  26. #17
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

     import java.util.StringTokenizer;
     
    public class Parser {
    	public static void main(String args[]){
     
    		parser("There is a table (dimension: 4 in 5 in 3 meters) in the room.");
     
     
    	}
     
    	public static void parser(String str){
     
    		String type = new String("Type: ");
    		String number = new String("Number: ");
    		String size = new String("Size: ");
    		String position = new String("Position: ");
     
    		StringTokenizer strTok = new StringTokenizer(str, " ");
    		String[] wordList = new String[strTok.countTokens()];
     
     
    			if(str.substring(6, 8).equals("is")){
    				System.out.println(number + "1");
    			}
     
    		}
    	}
    Here I first declared the type, number, size and position. Then I used StringTokenizer to split the string based on spaces. Then I counted the number of the tokens ( I'm not sure that this works properly ) Then I tell that If the verb was "is" print out: Number: 1.
    Now I want to say that if the verb was "are" please print out the third token as number. How can I say that? Can I put all tokens in an Arraylist and then tell that print the third array? Or is it a good opinion at all?
    Last edited by Lotus2013; October 25th, 2013 at 08:58 AM. Reason: I made a mistake

  27. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  29. #19
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    I edited the code

  30. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    I want to say that if the verb was "is" please print out the third token as number.
    I'm not sure why you are counting the tokens. If the current token is: "is" then the next token needs to be "a"
    and the number of items would be set to 1.
    The parser walks through the statement token by token. The contents of a token will define what needs to come next. "is" is followed by "a', "are" is followed by a number.

    if(str.substring(6, 8).equals("is")){
    That statement makes no sense. "is" will be the whole token, not part of a larger String.
    If you don't understand my answer, don't ignore it, ask a question.

  31. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  32. #21
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    Sorry, I made a mistake there. I wanted to say that if the verb was "are" print out the third token (which is the number) as number. I'm so sorry. And the reason why I am counting the tokens is because you know, sometimes, The user may type sentences like this: "There are 5 reg tables ( dimension: 4 in 2 in 8 m) in the neighbor`s yard. " So, I thought maybe it would be a good idea to see how many tokens are there though now I think why did I do that

  33. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    I don't know what use counting the tokens will be to help parse the statement.

    If the current token is "are" then the next token must be a number.
    Notice I used current and next to describe the tokens, not second and third.
    (I'm assuming that is the syntax of a valid statement.)

    The syntax for a valid statement should show the relative position of the tokens, not their numeric location.
    If you don't understand my answer, don't ignore it, ask a question.

  34. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  35. #23
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    I think I got what you mean. I added this to the code and changed the verb to are, But it doesn't work

    if(strTok.equals("are")){
    			System.out.println(strTok.nextToken());
    		}

  36. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java parser

    But it doesn't work
    Please explain.

    What is strTok? Is it a String or is it an instance of a method that has a nextToken() method?
    If you don't understand my answer, don't ignore it, ask a question.

  37. The Following User Says Thank You to Norm For This Useful Post:

    Lotus2013 (October 25th, 2013)

  38. #25
    Junior Member
    Join Date
    Oct 2013
    Posts
    27
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: java parser

    StringTokenizer strTok = new StringTokenizer(str, " ");

    It's the StrinTokenizer in which the tokens are separated by a space

Page 1 of 2 12 LastLast

Similar Threads

  1. How to start a Java parser
    By Sharmeen in forum Algorithms & Recursion
    Replies: 10
    Last Post: October 6th, 2012, 01:41 PM
  2. java lexical analyzer and parser tool
    By swar sarkos in forum Java Theory & Questions
    Replies: 0
    Last Post: March 17th, 2011, 11:59 AM
  3. XMI parser in java
    By chiru in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2011, 11:54 PM
  4. Parser for java Class
    By duarte in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2010, 07:22 PM
  5. Java Parser
    By sid0009 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 20th, 2010, 11:06 AM

Tags for this Thread