views:

62

answers:

1

Hi all -

I'd like to build a poker hand-range parser, whereby I can provider a string such as the following (assume a standard 52-card deck, ranks 2-A, s = suited, o = offsuit):

"22+,A2s+,AKo-ATo,7d6d"

The parser should be able to produce the following combinations:

6 combinations for each of 22, 33, 44, 55, 66, 77, 88, 99, TT, JJ, KK, QQ, AA
4 combinations for each of A2s, A3s, A4s, A5s, A6s, A7s, A8s, A9s, ATs, AJs, AQs, AKs
12 combinations for each of ATo, AJo, AQo, AKo
1 combination of 7(diamonds)6(diamonds)

I think I know parts of the grammar, but not all of it:

NM+ --> NM, N[M+1], ... ,N[N-1]
NN+ --> NN, [N+1][N+1], ... ,TT where T is the top rank of the deck (e.g. Ace)
NP - NM --> NM, N[M+1], ... ,NP
MM - NN --> NN, [N+1][N+1], ..., MM

I don't know the expression for the grammar for dealing with suitedness.

I'm a programming newbie, so forgive this very basic question: is this a grammar induction problem or a parsing problem?

Thanks,

Mike

+1  A: 

Well you should probably look at EBNF to show your grammar in a widely accepted manner.

I think it would look something like this:

S = Combination { ',' Combination } .
Combination = Hand ['+' | '-' Hand] . 
Hand = Card Card ["s" | "o"] .
Card = rank [ color ] .

Where {} means 0 or more occurences, [] means 0 or 1 occurence and | means either whats left of | or whats right of |.

So basically what this comes down to is a start symbol (S) that says that the parser has to handle from 1 to any number of combinations that are all separated by a ",".

These combinations consist of a description of a card and then either a "+", a "-" and another card description or nothing.

A card description consists of rank and optionally a color (spades, hearts, etc.). The fact that rank and color aren't capitalized shows that they can't be further divided into subparts (making them a terminal class).

My example doesn't provide the offsuite/suite possibility and that is mainly because in you're examples one time the o/s comes at the very end "AK-ATo" and one time in the middle "A2s+".

Are these examples your own creation or are they given to you from an external source (read: you can't change them)?

If you can change them I would strongly recommend placing those at one specified position of a combination (for example at the end) to make creating the grammar and ultimately the parsing a lot easier.

chrischu
Fixed the AK-ATo to be AKo-ATo...good catch, thanks. Would the grammar also need to do the following to recognize > 1 "Card" per "Hand":S = Combination {',' Combination}.Combination = Hand['+' | '-' Hand].Hand = Card{Card}['s'|'o'].Card = rank [ color ].
MikeRand
Ah yeah I forgot the second card in the hand. I fixed the grammar in my post to allow for (exactly) 2 cards per hand.I would not recommend using Card{Card} because that would mean between 1 and many cards so basically there could be 1, 3, or 5000 cards in a hand like this and I think that's not really what you want.
chrischu
Well, a hold'em hand may have 2 cards, a stud hand may have 3 cards, an Omaha hand may have 4 cards, and a draw hand may have 5 cards. Perhaps there's a smarted way to show this than Card Card {Card}(i.e. have HoldEmHand, StudHand, OmahaHand be three different non-terminal pieces). Maybe Card Card [Card] [Card] [Card] ['s'|'o']?
MikeRand
Yeah I think everything up from now is just a modeling thing that you have to decide for yourself.
chrischu
Perfect, thanks for your help.
MikeRand