2012-07-06 37 views
0

我使用JFLEX,byacc,JAVA解析声明它看起来像YACC /野牛鉴定结果在行动

where expr,expr having expr 

和语法看起来像

%token WHERE HAVING COMMA 
%% 

stmt : whereclause havingclause 

whereclause : WHERE conditionlist { move tmpList to whereClause}; 
havingclause : HAVING conditionlist { move tmpList to havingClause}; 

conditionlist : condition | condition COMMA conditionlist; 

condition : expr { tmpList.add($1); 
        /How do I know this is being executed for whereclause or havingclause /}; 

expr : .... 

我不能够区分如果条件是where条款或having条款的一部分,那么我将条件存储在临时列表中,然后移至正确的条款。 这样做的正确方法是什么?

回答

1

通常,您在规则操作中构建数据结构,将指针指向$$,然后在更高级别的规则中读取它们。喜欢的东西:

%token WHERE HAVING COMMA 
%union { 
    class List *listptr; 
    class Expr *exprptr; 
} 
%type<listptr> whereclasue havingclause conditionlist 
%type<exprptr> condition expr 
%destructor { delete $$; } <listptr> 
%destructor { delete $$; } <exprptr> 
%% 

stmt : whereclause havingclause { do something with $1 and $2... } 

whereclause : WHERE conditionlist { $$ = $2; }; 
havingclause : HAVING conditionlist { $$ = $2 }; 

conditionlist : condition { $$ = new List($1); } 
       | condition COMMA conditionlist { $$ = $2->prepend($1); }; 

condition : expr { $$ = $1; } 

expr : .... 

注意%destructor行动是野牛的扩展,并且需要以避免内存泄漏时有语法错误。