Reputation: 825
I am using Jflex, byacc, java for parsing statement which looks something like
where expr,expr having expr
and the grammer looks like
%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 : ....
I am not able to differentiate if condition is part of whereclause or havingclause, So I am storing conditions in temporary list and then moving to the right clause. What is the correct way of doing this?
Upvotes: 0
Views: 108
Reputation: 126378
Most commonly, you build the data structures in the rule actions, assigning pointers to them to $$
and then read them in the higher level rules. Something like:
%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 : ....
Note the %destructor
actions are a bison extension, and are needed to avoid memory leaks when there are syntax errors.
Upvotes: 1