1

For a hobby project I am trying to write some poker application. Part of it's functionality is the ability to parse messges from poker forums with games descriptions. Here are plain text version of message examples:

EXAMPLE 1

    $0.02/$0.05 No-Limit Hold'em (8 handed) 

Known players:
 BB: $1.70   UTG2: $13.05   MP1: $2.89   MP2: $2.64   MP3 (Hero): $5.28   CO
: $5.00   BU: $5.00   SB: $11.37  

Preflop: Hero is MP3 with 8 
[http://resources.pokerstrategy.com/smileys/heart.png], 8 
[http://resources.pokerstrategy.com/smileys/club.png].
UTG2 folds, MP1 raises to $0.15, MP2 calls $0.15, Hero calls $0.15, CO folds, 
BU calls $0.15,  2 folds, BB folds.

Flop: ($0.67) 8 [http://resources.pokerstrategy.com/smileys/diamond.png], K 
[http://resources.pokerstrategy.com/smileys/club.png], 6 
[http://resources.pokerstrategy.com/smileys/diamond.png] (4 players)
MP1 checks, MP2 checks, Hero bets $0.47, BU folds, MP1 folds, MP2 calls $0.47.

Turn: ($1.61) A [http://resources.pokerstrategy.com/smileys/club.png] (2 
players)
MP2 checks, Hero checks.

River: ($1.61) Q [http://resources.pokerstrategy.com/smileys/club.png] (2 
players)
MP2 bets $0.60, Hero raises to $2.10, MP2 calls $1.42.

Final Pot: $5.73.  

EXAMPLE 2

Grabbed by Holdem Manager <http://www.holdemmanager.net>
 NL Holdem $0.05(BB) Replayer 
 SB ($5.02)
 BB ($4.78)
 UTG ($2)
 UTG+1 ($2)
 UTG+2 ($1.88)
 MP1 ($5.32)
CO ($10.36) (21/18 на 109 рук, С-бет Ф=88%(11), С-бет Т=33%(3), АФ=2,6 
(4,5/3,0/0,5), WTSD=41%, W$SD=71%)
 Hero ($10.98)

Dealt to Hero 9 [http://resources.pokerstrategy.com/smileys/spade.png] T 
[http://resources.pokerstrategy.com/smileys/spade.png] 

 UTG calls $0.05, fold, fold, fold, CO raises to $0.20, Hero calls $0.20, 
fold, fold, fold

 FLOP ($0.52) J [http://resources.pokerstrategy.com/smileys/club.png] 8 
[http://resources.pokerstrategy.com/smileys/spade.png] 4 
[http://resources.pokerstrategy.com/smileys/diamond.png] 

CO bets $0.35, Hero calls $0.35

 TURN ($1.22) J [http://resources.pokerstrategy.com/smileys/club.png] 8 
[http://resources.pokerstrategy.com/smileys/spade.png] 4 
[http://resources.pokerstrategy.com/smileys/diamond.png] 9 
[http://resources.pokerstrategy.com/smileys/heart.png] 

CO checks, Hero checks

 RIVER ($1.22) J [http://resources.pokerstrategy.com/smileys/club.png] 8 
[http://resources.pokerstrategy.com/smileys/spade.png] 4 
[http://resources.pokerstrategy.com/smileys/diamond.png] 9 
[http://resources.pokerstrategy.com/smileys/heart.png] 4 
[http://resources.pokerstrategy.com/smileys/spade.png] 

CO bets $1, Hero ??? 

Basically these two examples were generated by two different converters. And there are currently about 20 different converters out there.

What I need to do is to be able to parse these game descriptions for different converters and "translate" text description of a game into java object Game. I've already written some code with a lot of regexp. This code can parse approximately 70% of my tests correctly, but:

  • it's really hard to maintain
  • I want to teach myself something new and cool.

So, what are my other options besides regexp? I am currently looking into ANTLR, but I am not sure if it is a best choice for this task.

4

1 回答 1

1

ANTLR 绝对适合您的要求。使用正则表达式进行语言处理非常脆弱,与使用 ANTLR 或类似工具相比,从一个版本到另一个版本的任何更改都更有可能破坏您的解释器。您可以做的是编写一个 Lexer 和一个基本 Parser,然后可以通过更具体的解析器扩展它们,以获得转换器之间的特定差异。

一旦你制作了一个Lexer并知道你在用解析器做什么,在 ANTLR 中改变东西比在你自己的解决方案中快得多!我确实同意 ANTLR 文档不是最好的,但是有大量来自第三方的 ANTLR 3 好的教程(对于任何特定问题,您都会在 SO 上获得很好的帮助)。

我个人的偏好是制作相当简单的 Lexer/Parsers 来输出 AST 树,然后手动编写一个 tree-walker 来遍历 Parser 提供的节点。有些人会主张在 ANTLR 中也制作 tree-walker,但我发现这比它的价值更困难和耗时(因为它在很大程度上不能重用)。

习惯创建一个好的语法文件的思维方式可能需要一点时间,但是一旦你完成它就会非常令人满意,并且你会看到它在你第一次需要修改或扩展某些东西时有多好。;)

于 2012-11-27T00:23:12.947 回答