Hand parser poker ... how to write a grammar?

I would like to create a poker hand parser in which I can provide a string such as the following (suppose a standard 52-card deck ranks 2-A, s = fits, o = offsuit):

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

The parser should be able to create 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:

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 do not know the expressions for grammar to deal with convenience.

I am new to programming, so goodbye to this basic question: is it a grammar induction problem or a parsing problem?

Thank,

Mike

+3
source share
1 answer

Well, you should take a look at EBNF to show your grammar in a common way.

, :

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

{} 0 , [] 0 1 | , | |.

, , (S), , 1 , ",".

, "+", "-", .

, , (, ..). , , , ( ).

offsuite/suite, , o/s "AK-ATo" "A2s +".

(: )?

, (, ), , , .

+3

Source: https://habr.com/ru/post/1754510/


All Articles