具有多个元素的ANTLR规则

时间:2013-01-21 17:30:02

标签: antlr antlr3

我需要一些ANTLR的帮助。 我有以下规则:

datasource
@init{boolean joinexpr = false;}
:   
   (s1=datasourceelement (joinclause1=joinclause joinelement1=datasourceelement onclause1=onclause  (multijoinexpression)* {joinexpr=true;})?)
   -> {joinexpr}?  ^(JOINEXPRESSION 
                        ^(LEFTEXPR $s1?) 
                        $joinclause1?
                        ^(RIGHTEXPR $joinelement1?) $onclause1? multijoinexpression*
                    )                    
   -> $s1                   
;
multijoinexpression
:
  joinclause datasourceelement onclause 
  -> 
  ^(MULTIJOINEXPRESSION
                        joinclause
                        ^(RIGHTEXPR datasourceelement) onclause
                    )
;

用于解析连接表达式: 它吃了

  

(table / query)(on(table / query)on(field = field))*

但是我需要在每个连接周围处理“()”,所以它可以解析这样的事情:

  

((table1 JOIN table2 ON field1 = field2)JOIN table3 ON field2 = field3)JOIN ...)

没有括号它工作正常,但我无法弄清楚如何将左右括号添加到规则,因为 multijoinexpression *

1 个答案:

答案 0 :(得分:0)

这解决了这个问题。我也觉得它很难看,但它确实有用。

(
  //1-2
                (LPARAM simplejoin RPARAM) multijoinexpression?
 |//2-3
        (LPARAM (LPARAM simplejoin RPARAM) multijoinexpression RPARAM)  multijoinexpression? 
 |//3-4
(LPARAM (LPARAM (LPARAM simplejoin RPARAM) multijoinexpression RPARAM)  multijoinexpression RPARAM) 
multijoinexpression?
 |//4-5
(LPARAM 
(LPARAM (LPARAM (LPARAM simplejoin RPARAM) multijoinexpression RPARAM)  multijoinexpression RPARAM) 
multijoinexpression RPARAM) multijoinexpression?
 )  ->^(JOINEXPRESSION simplejoin multijoinexpression* )