RE: expressions ?! how ?
[email protected] ("Orton, Yves") Tue, 29 Jul 2003 10:55:14 +0100
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <71B318898201D311845C0008C75DAD1C08960EBA@defra1ex2> |
> On Monday, July 28, 2003, at 11:26 AM, Orton, Yves wrote:
>
> >> what seems to me as wrong is that i may have not only :
> >>
> >> '(' expression ')'
> >>
> >> but also just :
> >>
> >> expression
> >>
> >> but then it becomes left-recursive..
> >>
> >> brackExpr : expression | '(' expression ')' | comparison
> >
> > Use the <leftop> or <rightop> directives and youll be fine.
> >
> <leftop> and <rightop> are for left- and right-associative infix
> operators, so I don't see how they'll affect left recursion
> one way or another.
Well, isnt a common cause of left recursion infix operators? And hence the
existance of the <leftop> directive?
I could be wrong of course, I didnt look into it that deeply. But it seems
to me that the grammar
expression : <leftop: andExpr 'or' andExpr>
andExpr : <leftop: notExpr 'and' notExpr>
notExpr : 'not' brackExpr | brackExpr
brackExpr : '(' expression ')' | comparison
comparison : identifier compType identifier
compType : '=' | '>' | '<' | '>=' | '<='
identifier : fieldName | number
fieldName : /[a-z]+/
number : /\d+/
works fine. (At least off the top of my head I couldnt build an expression
that would break it.)
Anyway, the below link documents a mechanical means of eliminating left
recursion from a PRD grammar. And has lots of links to better articles on
the subject.
http://perlmonks.org/index.pl?node_id=153155
Yves