简体   繁体   English

扑克手范围解析器......如何编写语法?

[英]Poker hand range parser … how do I write the grammar?

I'd like to build a poker hand-range parser, whereby I can provide a string such as the following (assume a standard 52-card deck, ranks 2-A, s = suited, o = offsuit): 我想构建一个扑克手范围解析器,我可以提供如下字符串(假设标准的52张牌,排名为2-A,s =适合,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 basic question: is this a grammar induction problem or a parsing problem? 我是一个编程新手,所以请原谅这个基本问题:这是语法归纳问题还是解析问题?

Thanks, 谢谢,

Mike 麦克风

Well you should probably look at EBNF to show your grammar in a widely accepted manner. 那么你应该看看EBNF以广泛接受的方式展示你的语法。

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 | 其中{}表示0或更多出现,[]表示0或1出现和| 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 ",". 所以基本上归结为起始符号(S)表示解析器必须处理从1到任何数量的组合,这些组合都被“,”分隔。

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+". 我的例子没有提供offsuite / suite的可能性,这主要是因为在你的例子中,一次o / s出现在最后的“AK-ATo”和一次中间的“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. 如果你可以改变它们,我强烈建议把它们放在一个组合的一个指定位置(例如在最后),以便创建语法,最终解析更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM