Parser Problems

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Parser Problems

Post by {Nathan} »

Almost done with this parser. It works fine with red ball, blue ball, but not with other things. If you wanna help, heres the source code.

Code: Select all

DECLARE FUNCTION CheckSpace! (Var AS STRING)
DECLARE SUB Parse (Answer$, Word1$, Word2$)
CLS                                             'clear the screen
CONST True = 1, False = NOT True                'set true and false constants
Phrase1$ = "red vall."                           'set the parsed word
Phrase2$ = "blue vall."                          'set the second word

Parse Phrase1$, Word1$, Word2$
Parse Phrase2$, Word3$, Word4$


'You can set it for more words, but for now 2 words are good.

IF Word1$ = Word2$ THEN PRINT "Match!"
IF Word1$ = Word3$ THEN PRINT "Match!"
IF Word1$ = Word4$ THEN PRINT "Match!"
IF Word2$ = Word3$ THEN PRINT "Match!"
IF Word2$ = Word4$ THEN PRINT "Match!"
IF Word3$ = Word4$ THEN PRINT "Match!"

FUNCTION CheckSpace (Var AS STRING)
        FOR i = 1 TO LEN(Var)
                IF MID$(Var, i, i - 1) = " " THEN CheckSpace = True
        NEXT

END FUNCTION

SUB Parse (Answer$, Word1$, Word2$)
Parse:
        Answer$ = LTRIM$(RTRIM$(Answer$))       'Gets rid of all the spaces
        IF Answer$ = "" OR Answer$ = " " THEN Word$ = Answer$
                                                'If the first word is null or
                                                'a space, we return it.
        Index = 0                               'Set the index to 0
        DO                                      'start the loop
         Index = Index + 1                      'start the heart of the loop
         IF MID$(Answer$, Index, 1) = " " THEN EXIT DO: GOTO ParserHandler
                                                'Check for the space and if
                                                'it is it goes to the handler
        LOOP                                    'loop da loop!



ParserHandler:
        Word1$ = LEFT$(Answer$, Index - 1)       'just puts the first word
                                                'into the FirstWord$ string
        Answer$ = RIGHT$(Answer$, Index + 1)    'puts Word$ out of answer
      
        'Now is the time that you want to store/use the first word
        'becuase it will be written over nex time.

        IF CheckSpace(Answer$) THEN             'if there are spaces
        GOTO Parse                              'parse again
        ELSE                                    'otherwise
        Word2$ = LTRIM$(RTRIM$(Answer$))        'Set the last word
        EXIT SUB                                'exit the function
        END IF                                  'exit the if


END SUB
I commented just about every line (that really hurt!) and I don't know why its not working... I really don't know why. Also, try not to get into that I used GOTO at 2 places.
Image
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post by Mitth'raw'nuruodo »

:roll: Now I have no clue what your talking about....*cough*Gotos*cough* :D
"But...It was so beutifully done"
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

In another thread, you guys haad a huge fight about using gotos. I only used two and it is still very well organized!
Image
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

:) Umm, what is this suppose to do??
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

It seperates the words (only 2 right now) and tests them for a match. The only problem is it only works with a certian amount of chars
:evil:
Image
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Nathan1993 wrote:In another thread, you guys haad a huge fight about using gotos. I only used two and it is still very well organized!
He wouldn't be on you and me Mitth?? Hehe,. we got a reputation now. :D :wink: and Z!re..

Well, I've never tried prog like that,.. no help here.. :)
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post by Mitth'raw'nuruodo »

Ummm...ya I guess! :D

Nate: PLEASE be more specific on whats this suppose to do.
Try to use as little pronouns as possible!
"But...It was so beutifully done"
Nathan1993 at school

Post by Nathan1993 at school »

:oops: My friend toldme too...imat school goofing off right now and spacebar dosnt work! But it seperates 2 words in 1 string to 2 seperate strings...I am using it for a AI engine. Here is Nixons original pos, witch I have answered too.
This is something I really like to study and work on. I think I found a good way to make and AI learn words when starting with none. If you programme it to look for patterns and not for words then it can learn different languages and different patters. I have only run into 1 problem so far, which effects the whole thing. I cant get it to find patterns because of my shoddy code. Can someone show me some code that compares two words and find the patterns in them and show the patterns that are the same in both words. e.g

Red ball.
Blue ball.

both have " ball."

I need the code in qb
My other friend told me to do :twisted:
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post by Mitth'raw'nuruodo »

Ok, so your saying, that you are comapring 2 strings and you want to find the common words between them?
(Yes or No will do, if no plese elaberate).
"But...It was so beutifully done"
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

A learning AI? That sounds scary? or supose to, hehe,. sounds like fun.... and this red ball blue ball, this is just part of it? comparo system?
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post by Mitth'raw'nuruodo »

Well, if thats it (what I wrote above) then thats VERY EASY....1 word....MID$.
"But...It was so beutifully done"
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Yeah, I know. This is it... but there is a bug that I cannot fix! For some odd reason, if I put in "Hi everybody!" to be parsed, it will return "Hi everybody!" and dosn't parse it right... I have NO IDEA why. :oops:
Image
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Whats it suppuse to return?, all I see is "Match" in your IF..THEN,...

If this code has been done, or if its posible, keep looking, it will be the most simplest 4 sure, where you would lest look,..

.. trust me, I've had bugs alot and still get 'em, and when I find 'em, I always kick myself for not seeing it b4... :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

The parser... it should return "Hi" and "everybody!" when you put "Hi everybody!" into it... for some odd reason, the sub returns "Hi everybody!" then, but it works fine with red ball and blue ball. Ohh... and ritt... the "Match!" things are for the AI engine...

:roll:
Image
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Tell me how you put "Hi Everybody!" into it.. I did this and it came back a macth...

Code: Select all

Phrase$ = "Hi Everybody!"
Phrase$ = "Hi Everyone!"
How did you put it in? maybe I might could look over it,. once I see the codes pattern I can figure what its doing, then I might can find the prob, and then help you with the prob, long shot and no promises.. :) ,. I'm smart but I have limits.. :(
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post by Mitth'raw'nuruodo »

My suggestion...Trap your vars and see when they get "bad".
I can't debugg it for you: GOTO (twitch :wink: ) thing.
"But...It was so beutifully done"
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

I found something thats sure to help!
QB Help File wrote:MID$ Statement Details

Syntax
MID$(stringvariable,start[,length])=stringexpression

Argument Description
stringvariable The string variable being modified.
start A numeric expression giving the position in
stringvariable where the replacement starts.
length The length of the string being replaced. The
length is a numeric expression.
stringexpression The string expression that replaces part of the
stringvariable.

The arguments start and length are integer expressions. The argument
stringvariable is a string variable, but stringexpression can be a
string variable, string constant, or string expression.

The optional length refers to the number of characters from the
argument stringexpression that are used in the replacement. If length
is omitted, all of stringexpression is used. However, regardless of
whether length is omitted or included, the replacement of characters
never goes beyond the original length of stringvariable.
that last little bit might be why your prog has probs past 4 characters on the unmatched words like red and blue,. green is past 4 and doen't seem to work as well,. :? or at all,.

Hope this helps, I might try to look over the code somemore 2.. :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Okay,.. I look over it debug style like Mitth suggested, and I strungly suggest you do to,. have it lay out all your varible where you can see it, this was my result...

Code: Select all

'Parser Sub first phrase..
Word1$ = Red
Answer$ = Vall
Word2$ = Vall

'Parser Sub second phrase..
Word1$ = Blue
Answer$ = _Vall
Word2$ = Vall
Look what happens when I add a letter to Blue..

Code: Select all

'Parser Sub first phrase..
Word1$ = Red
Answer$ = Vall
Word2$ = Vall

'Parser Sub second phrase..
Word1$ = Bluee
Answer$ = e_Vall
Word2$ = eVall
or basicaly what it does,. after the 3 character it carries over to the next word.. on your first it carried the space since it was the forth after blue,.. Your prob seems to be it only work past 3 chars , it carries every thing behind it over to the next... hope this helps and you can figure it out, I'll look at it some more latter when I find time, but run some debuging yourself... :wink:
Last edited by Rattrapmax6 on Wed Feb 02, 2005 3:44 pm, edited 1 time in total.
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Re: Parser Problems

Post by Mitth'raw'nuruodo »

Nathan1993 wrote: Index = 0 'Set the index to 0
DO 'start the loop
Index = Index + 1 'start the heart of the loop
IF MID$(Answer$, Index, 1) = " " THEN EXIT DO: GOTO ParserHandler
'Check for the space and if
'it is it goes to the handler
LOOP 'loop da loop!
[/code]
OK, I gave it a glance (I though what the heck), hmmm....Do you see an error in the code above?
If this was C++ I would call it a Warning (the program can run like it is but might have undesiered results).

Ok on this statement:

Code: Select all

IF MID$(Answer$, Index, 1) = " " THEN EXIT DO: GOTO ParserHandler
Ty making it:

Code: Select all

IF MID$(Answer$, Index, 1) = " " THEN GOTO ParserHandler: EXIT DO
You see, your not going to your GOTO (twitch :wink: ) line. Your exiting do before you get to that line of code: This is what your doing:

Code: Select all

IF MID$(Answer$, Index, 1) = " " THEN
   EXIT DO
   GOTO ParserHandler
END IF
Now if you EXIT DO before you call the GOTO (twitch :wink: ) you won't get to it...

I had this a lot when I started out prog. but now when ever I use EXIT DO or FOR, or anyting else, I check to see if its in the right order.
I hope that that was the prob. :D [/code]
"But...It was so beutifully done"
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

It seems to narrow down here Mitth and Nathan...

Code: Select all

Word1$ = LEFT$(Answer$, Index - 1)       'just puts the first word 
                                                'into the FirstWord$ string 
        Answer$ = RIGHT$(Answer$, Index + 1)    'puts Word$ out of answer 
Looking at it and running it it looks fine,. but when I removed the -1 and +1 and moving them around I got rid of the carrying past 3 chars, but at three chars like red took out the v in vall...

Somehow this is not calculating right,. look at what my debuging returned in one of my post above. why? debuging is not yet telling me... :?

I've come to understand 90% of your prog and its brillant.. but somthing is messing up the calculations.. and can't yet see it, maybe you do, or can take a look since you know basicaly 100% of what every thing does.. :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Post Reply