2012-07-06 94 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行動是野牛的擴展,並且需要以避免內存泄漏時有語法錯誤。