Parsing with Parse::RecDescent 
Author Message
 Parsing with Parse::RecDescent

Hello!

I'm trying tow rite a parser for orders for Atlantis (PBEM game).
Syntax is pretty simple: one line per command, each command
starts with name, followed by list of parameters. Basically it's
something like this (grammar for parsing one line):

Statement:              Comment | Command Comment(?)
Comment:                /;.*/
Command:                '#atlantis' <commit> FactionID String
Command:                'attack' <commit> Number(s)
....

However I have problems to make it work as I want:

1) In case of failed parsing (syntax error, not allowey keyword, ...)
        I want to store error messages in variable (not to be
        just printed), so I can process them later.
2) In case if user types "attack bastards" I want to give him
        error message that "list of numbers expected" instead
        of just saying the "cannot parse this line". The only
        thing that I came up with now was defining every command
        like this:
        Command:        Attack
        Attack:         'attack' AttackParams
        AttackParams:   Number(s) | <error>
        ...
        Any better solutions?

WBR, Andrew
--
  Andrew Zhilenko, AZ283-RIPE, System Manager
          Nextra Czech Republic s.r.o.
tel.+42-02-96355111         fax. +42-02-96355112

 Millenium Plaza, V Celnici 10, Praha 1 - 11721
                 Czech Republic



Thu, 21 Aug 2003 01:46:39 GMT  
 Parsing with Parse::RecDescent

in <URL::">
)) Hello!
))
)) I'm trying tow rite a parser for orders for Atlantis (PBEM game).
)) Syntax is pretty simple: one line per command, each command
)) starts with name, followed by list of parameters. Basically it's
)) something like this (grammar for parsing one line):
))
)) Statement:           Comment | Command Comment(?)
)) Comment:             /;.*/
)) Command:             '#atlantis' <commit> FactionID String
)) Command:             'attack' <commit> Number(s)
)) ....
))
)) However I have problems to make it work as I want:
))
)) 1) In case of failed parsing (syntax error, not allowey keyword, ...)
))      I want to store error messages in variable (not to be
))      just printed), so I can process them later.

I don't think Parse::RecDescent has a hook for that (Damian, something
for the todo list?), but you can always install a $SIG {__WARN__}
handler and process the generated warnings.

)) 2) In case if user types "attack bastards" I want to give him
))      error message that "list of numbers expected" instead
))      of just saying the "cannot parse this line". The only
))      thing that I came up with now was defining every command
))      like this:
))      Command:        Attack
))      Attack:         'attack' AttackParams
))      AttackParams:   Number(s) | <error>
))      ...
))      Any better solutions?

You can just do:

    Command:   '#atlantis' <commit> FactionID String
           |   'attack' <commit> Number(s)
           |   <error>

and when you try to parse "attack bastards", you will get:

    ERROR (line 1): Invalid Command: Was expecting Number but found
                    "bastards" instead

You might want to use <error?>, which will only print the error when
it saw '#atlantis' or 'attack' (because then you are committed).

Abigail
--
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()



Thu, 21 Aug 2003 06:29:07 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Parsing with Parse::RecDescent

2. Help: Problem with simple parsing and Parse::RecDescent

3. Text Parsing - Parse::RecDescent or another method?

4. Parse::RecDescent and parsing comments

5. Parse::RecDescent stops parsing.

6. Having Trouble with Parse::RecDescent on Solaris

7. Converting SQL89 YACC rule to Parse::RecDescent

8. ANNOUNCE: Parse::RecDescent 1.42

9. ANNOUNCE: Parse::RecDescent 1.41

10. ANNOUNCE: Parse::RecDescent 1.35

11. ANNOUNCE: Parse::RecDescent 1.30

12. ANNOUNCE: Parse::RecDescent 1.66

 

 
Powered by phpBB® Forum Software