Thread: Lexing file into binary search tree

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Lexing file into binary search tree

    Standard example. I have a large text file and I wish to lex it into words. I tell the program that all words are delimited by ' ' ';' ':' and '\n'.

    When I run the program it seems to be outputting the occurances of the letters and not the words. Im gobsmacked, I dont know what the hell is going on. Heres the function that lexes letters and not words. I want words dammit words!!

    First youll see I define root node and point it to null; This forms the base of the BST.
    Then keep munching one character at a time until EOF reached. If the character is not a delimiter, assign it to "word" string, character by character. If it is a delimiter, take the so-far-constructed "word" and chuck it in the BST, then clear the word string through .clear().
    Repeat until done. But its not working.

    Code:
    struct masternode* lexical_scanner(ifstream* inputfile) {
        string word;
        char c;
        struct masternode* root = NULL;
        while (c!=EOF){
            if((c = (*inputfile).get()) != ' ' && c != ':' && c!= ';' && c!='\n')
                word += c;
            root = insert(root, word);
            word.clear();
        }
        return root;
    }
    EDIT: All the other functions in the source file are just fine, I've tested them in other apps and they are purpose built.
    Last edited by Vespasian; 05-22-2013 at 04:47 PM.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You need to look at your if-clause, and remember if you leave out the brackets only the following line will be regarded as part of the if. Right now you are clearing the word-string every iteration.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Its done deliberately like that. The process is as follows:
    If the said delimiters are not encountered, execute the if clause, if they are, move on and load into the BST and clear yo' ass up for the next iteration!

    I need a solution. I am tired and need to go to bed and this silly problem is keeping me up. My IDE debugger is retarded aswell and cant even debug this thing!
    Last edited by Vespasian; 05-22-2013 at 04:56 PM.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Vespasian View Post
    Its done deliberately like that. The process is as follows:
    If the said delimiters are encountered, execute the if clause, if not, move on and load into the BST and clear yo' ass up for the next iteration!

    I need a solution. I am tired and need to go to bed and this silly problem is keeping me up. My IDE debugger is retarded aswell and cant even debug this thing!
    Well that won't work. Say you encounter 'hello', then h e l l o will be put into the tree as single letters. Don't you need an else clause around the insert and clear statements?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Dens korrekte, det fungerer!! Jeg er meget taknemmelig!!

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Its done deliberately like that.
    O_o

    Then you have deliberately written code with an error.

    If you consume only characters with your string, you will produce only characters for your tree.

    Soma

  7. #7
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Vespasian View Post
    Dens korrekte, det fungerer!! Jeg er meget taknemmelig!!

    I dont know why it works but it does :-) Ill think more about it tomorrow.
    Bonus info: When using Google Translate, be sure to get the grammar right. "It's correct" is correctly translated to "Det er korrekt" while "Its correct" becomes "Dens korrekte" which doesn't make sense at all and had me stumped for a while :-) I appreciate the gesture though.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Binary Search Tree in DLL
    By Silvio in forum C Programming
    Replies: 6
    Last Post: 05-10-2009, 11:39 AM
  3. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  4. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM