开发者

ANTLR tree construction problem

If I got a grammar rule like

a: A (C|D|E)

I can create AST for the rule by attaching rewrite rules for each alternative(C, D, E) like this:

a: A (C -> ^(A C)
    | D -> ^(A D)
    | E ->开发者_运维技巧 ^(A E))

But, if I got another slightly different grammar rule like

a: (A|B) (C|D|E)

how do I create AST for every possible match? I first tried like this:

a: (A|B) (C|D|E) -> ^((A|B) (C|D|E))

but, it did not work.

Is there a simple way to solve this problem?

Thanks in advance. :)


You have two options:

1

a : (left=A | left=B) (right=C | right=D | right=E) -> ^($left $right)
  ;

or:

2

a : left right -> ^(left right)
  ;

left 
  :  A 
  |  B
  ;

right
  :  C
  |  D
  |  E
  ;

Personally, I prefer the 2nd option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜