Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411488 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 03:06:56 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Parser with PHP
Pages: [1]
Print
Author Topic: Parser with PHP  (Read 1971 times)
kafka
Guest
« on: August 31, 2008, 11:46:42 PM »

Hi,

I have a text adventure project. I use PHP for the project and i'm thinking of creating a scripting language to make the creation of the game easier. So my problem is that if anyone has any experience in the art of writing a parser, I would like to hear some advices on how to really create the parser. I'm thinking that the syntax would look something like this:

Code:
//Object named lamp.
@lamp (item):
//If player types "look lamp", he/she will get this output:
%look:
/This is a lamp. Yea.

//Room named starting room.
//This is the room player starts from, because it is described first.
@starting_room (room):
%look:
//If player don't have item named "lamp"
? not lamp:
/You can't see anything. It's too dark.
else:
/Everything is bright, because you have a lamp.

%smell:
/Smells awful.

%feel:
/The walls feel all slimy and sticky...
/You can feel a metal object in the ground.

%take (metal object|object|lamp):
/You take the metal object.
#additem lamp

I have tried different techniques to write a parser, but none of them is flexible enough for the job. Undecided
« Last Edit: September 01, 2008, 12:13:40 AM by kafka » Logged
Gravious
Level 2
**


"Swedish meatballs"


View Profile WWW
« Reply #1 on: September 01, 2008, 12:45:38 AM »

The 2 immediate choices that spring to my mind is to switch to XML and use PHP 5 XML parser, or you're going to need to use Regular Expressions.

Personally, i'd use XML, but if its not compiled in, use the PCRE regex, its fun :D
Logged

One day I'll think about doing something to stop procrastinating.
kafka
Guest
« Reply #2 on: September 01, 2008, 01:31:04 AM »

The 2 immediate choices that spring to my mind is to switch to XML and use PHP 5 XML parser, or you're going to need to use Regular Expressions.

Personally, i'd use XML, but if its not compiled in, use the PCRE regex, its fun :D
Well, i tried using XML but the workflow in that is crap. Tongue
So I decided I should make my own scripting language. Yeah, regular expressions, i don't really have any clue on how I should use regular expression in the parser (I don't really master the art of regex anyways).  Huh?
Logged
muku
Level 10
*****


View Profile
« Reply #3 on: September 01, 2008, 02:08:16 AM »

I would try to use a parser generator; doing this kind of stuff by hand is very tedious and error prone. Here is something that might work:

http://greg.chiaraquartet.net/archives/138-PHP_ParserGenerator-and-PHP_LexerGenerator.html
Logged
kafka
Guest
« Reply #4 on: September 01, 2008, 02:50:49 AM »

I would try to use a parser generator; doing this kind of stuff by hand is very tedious and error prone. Here is something that might work:

http://greg.chiaraquartet.net/archives/138-PHP_ParserGenerator-and-PHP_LexerGenerator.html

I wasn't thinking of using generators for my purpose, because the language has limited amount of commands and I think those generators too massive and hard to understand to really suit the purpose. But if everyone thinks I should use those then maybe I should try those out. Btw, does anyone have any examples of their own parsers that I could look at...
Logged
muku
Level 10
*****


View Profile
« Reply #5 on: September 01, 2008, 03:13:28 AM »

Hm. Looking at your proposed grammar above, I'd say this is exactly the thing you could do with just a few lines of EBNF, so parser generators immediately sprung to my mind. You don't have to deal with their internal complexity, so they're usually pretty easy to use actually. But it's kind of annoying that there doesn't seem to be a good and mature one for PHP, the one I linked to seemed to just be in alpha or whatever.

Anyway, if you think this approach is too heavy-weight for what you're doing, then I'd suggest taking a look at YAML. It's like XML with all the annoying <s and >s and all the fluff stripped away! Wink
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic