Login
Back to forumReply to this topicGo to last reply

Posted By

Dunric
on 2006-12-15
15:20:28
 Writing an effective parser (an example program)

A came up with a parser about 10 years back, but I'm not sure if it completed the goal I set back in 1996 to "write an effective parser". Here's the code to that parser:

10 MO=50:MW=10:VB=25
15 DIM NO$(MO),WD$(MW),VB$(VB):REM MO is max objects in game; MW is max words; VB is max verbs

...

40 REM *** Parser starts here ***
50 A$="":DT$="":OJ$=""OKE 19,0:INPUT ">";A$OKE 19,1
51 PT=1:NM=0
52 DT$=A$:FOR A=1TOLEN(DT$)
53 IF MID$(DT$,A,1)=" " THEN A$=MID$(DT$,PT,A-PT)T=A+1:NM=NM+1:WD$(NM)=A$
54 NEXT:NM=NM+1:A$=MID$(DT$,PT,A-PT):WD$(NM)=A$
55 IF A$="" THEN PRINT"Command not understood. Please try again.":GOTO 50
56 IF LEN(A$)>30 THEN PRINT"Command too complex. Please re-enter.":GOTO 50
57 OJ$=WD$(4):REM WD$(4) should be the container object, like a bag or backpack
58 IF NM=0 THEN NM=1
59 O=0:N=0:FOR X=1 TO NM
60 FOR Y=1 TO MO:IF WD$(X)=NO$(Y) AND X=2 THEN O=Y
61 IF WD$(X)=NO$(Y) THEN N=Y
62 NEXT:NEXT
63 V=0:FOR X=1 TO NM
64 FOR Y=1 TO VB
65 IF WD$(X)=VB$(Y) THEN V=Y
66 NEXT:NEXT
67 IF V=0 THEN PRINT "What? Verb not understood.":GOTO 50
68 IFLEFT$(A$,3)="exa"THENGOTO70
69 IF N=0 THEN PRINT "What? Noun not understood.":GOTO 50
70 ON V GOTO 100,110,120,130,140,150,160,170,180,190 ...
71 REM Rest of program starts here...

This parser breaks down each word entered into single words, stored in WD$(x). The object container, if present, is stored in WD$(4) and copied to string OJ$. An object container is anything that can hold an object, such as a bag or a backpack, etc.

Verbs are stored in VB$(y), but are not defined in this example program. You'll need to come up with your own list, READ them in via DATA statements, and store them in VB$(y). Same thing with the Nouns in the game, which are stored in NO$(y).

Once the parser finds a match for the verb and noun, it hops to Line 70, where (based on the value of variable "V"), it goes to the appropriate verb routine (also not supplied in this example; again, you'll have to come up with your own verb list and match the line numbers to the appropriate verb routine from your list of verbs).

I've used this parser in almost 95% of every adventure game that I have written -- thus far, well over 50. It is quite effective, though not nearly matching my original goal of "writing an effective parser." (See: Zork).

I hope this is helpful.

Paul

Posted By

Lavina
on 2006-12-16
11:42:23
 Re: Writing an effective parser (an example program)

I used my own parser in my latter adventure games, they can get up to 3 words out of the input line as far as I can remember.... I used it in 'Deepspace' and 'Parastorm'.



Back to topReply to this topic


Copyright © Plus/4 World Team, 2001-2024