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

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

Post by Rattrapmax6 »

I DID IT!!! I FIXED IT!!!! :D

It was in that LEFT$ RIGHT$ extract like I thought!!

Here, Nathan, copy this back to QB.... Then check the parser SUB.. I have REMs telling you what you had wrong... :wink: or you can read 'em hear, :D but its all jumbled up..

Code: Select all

DECLARE FUNCTION CheckSpace! (Var AS STRING)
DECLARE SUB Parse (Answer$, Word1$, Word2$)
CLS
CONST True = 1, False = NOT True
Phrase1$ = "red vall."
Phrase2$ = "blue vall."

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


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$))
        IF Answer$ = "" OR Answer$ = " " THEN Word$ = Answer$
                                              
                                               
        Index = 0
        DO
         Index = Index + 1
         IF MID$(Answer$, Index, 1) = " " THEN EXIT DO: GOTO ParserHandler
                                                
                                               
        LOOP



ParserHandler:
        Word1$ = LEFT$(Answer$, Index - 1)
                                              
          num = LEN(Answer$)                   'You had this so close, you
        Answer$ = RIGHT$(Answer$, num - Index) 'did it backwards. RIGHT$
                                               'reads from the right, so
                                               'you have to subtract Index
        IF CheckSpace(Answer$) THEN            'from the total number of
        GOTO Parse                             'characters... that gives
        ELSE                                   'you the right calculation.
        Word2$ = LTRIM$(RTRIM$(Answer$))
        EXIT SUB
        END IF


END SUB
-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...Dude!

Did you read my last post?

Observe:

Code: Select all

IF MID$(Answer$, Index, 1) = " " THEN EXIT DO: GOTO ParserHandler 
Do you see the error?

Replace w/:

Code: Select all

IF MID$(Answer$, Index, 1) = " " THEN GOTO ParserHandler: EXIT DO 
I'm not sure if this is an error, but I can't think of any reason to put EXIT DO before the GOTO (twitch :wink: ) . You WILL NEVER GET TO THAT GOTO EVER! Unless you do that above^
"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 »

well, I copied his prog, the EXIT DO drops it into the Parser system, so I didn't mess with it for now...

The real problem I fixxed, he had the RIGHT$ backward,.. so

Nathan,. look at and copy the code a repaired above, for better words, you were trying to use the RIGHT$ to scan from the center, you needed to make it scan from the right side....

When you put it back in QB, my repaired version, I've remlined the changes and you'll find it processes higher amonts of characters than it used to.. totaly fixed exept for that bit Mitth is on, it ok 4 your curret code, but look into and repair it if you plan to do more with the code that you need that GOTO...

:wink: , good luck with this A.I., its brillant I must say!
-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 »

Or how about this, don't use GOTO! :wink:

I wonder why its dumped into the PraserHandler..... :idea: Wait....I know why!

The GOTO ParserHandler IS NOT being called, that much I was right, but when it impliments that piece of code (EXIT DO) it exits the DO and goes to the PraserHandler. Why you may ask? BECAUSE IT ONLY A FREAKIN LINE LABEL! :twisted:

So no matter what, you'll always go to that code, cause it's after the loop. :wink:

You see what I was on is not an error but REDUNDANCY (useless code).
You only need one: EXIT DO, or, GOTO PH.

Cause if you leave out one it will do the same thing. I garrentee it, unless the computer devolpes its own AI and doesn't feel like doing its logic for some reason. :lol:

Now which do you think I'm going to recommend taking out? The GOTO of course! Which is absouluely unneccessary! :P

One more question. What happens if Answer$ doesn't have a space in it?
You'll have an error on your hands my friend! Do some checking for that unless you can assume that there will be a space. 8)

Hope that helped! :D
"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 »

Mitth wrote: One more question. What happens if Answer$ doesn't have a space in it?
You'll have an error on your hands my friend! Do some checking for that unless you can assume that there will be a space.
His Answer$ comes from Phrase$ 1 & 2,. if there is no space, then its human error for forgetting to put one there!...

I have the repaired code at the top of this page Nathan,.. so remember to look up there. :wink:
-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 »

Here's Rattra's and My revisions combined!

Code: Select all

DECLARE FUNCTION CheckSpace! (Var AS STRING)
DECLARE SUB Parse (Answer$, Word1$, Word2$)
CLS
CONST True = 1, False = NOT True
Phrase1$ = "red vall."
Phrase2$ = "blue vall."

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


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 i
END FUNCTION

SUB Parse (Answer$, Word1$, Word2$)
Parse:
        Answer$ = LTRIM$(RTRIM$(Answer$))
        IF Answer$ = "" OR Answer$ = " " THEN Word$ = Answer$
        Index = 0
        DO
           Index = Index + 1
           IF MID$(Answer$, Index, 1) = " " THEN EXIT DO
        LOOP
        Word1$ = LEFT$(Answer$, Index - 1)
        num = LEN(Answer$)                   'You had this so close, you
        Answer$ = RIGHT$(Answer$, num - Index) 'did it backwards. RIGHT$
                                               'reads from the right, so
                                               'you have to subtract Index
        IF CheckSpace(Answer$) THEN            'from the total number of
                GOTO Parse                             'characters... that gives
        ELSE                                   'you the right calculation.
                Word2$ = LTRIM$(RTRIM$(Answer$))
                EXIT SUB
        END IF
END SUB
Try this and see fi it works! :wink:
"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 »

Hay Nathan, we fixed it..

I found your prob and Mitth knocked one of the GOTOs out,.

Our repaired version is above, it works! ^ ^ ^

Tells us what you think!!!
-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 »

:D
"But...It was so beutifully done"
Post Reply