Matching paranthesis in YACC expression

Par Mattsson <Par.Mattsson@iar.se>
16 Jan 1998 23:34:46 -0500

          From comp.compilers

Related articles
Matching paranthesis in YACC expression Par.Mattsson@iar.se (Par Mattsson) (1998-01-16)
Re: Matching paranthesis in YACC expression clark@quarry.zk3.dec.com (Chris Clark USG) (1998-01-20)
| List of all articles for this month |

From: Par Mattsson <Par.Mattsson@iar.se>
Newsgroups: comp.compilers
Date: 16 Jan 1998 23:34:46 -0500
Organization: IAR Systems AB
Keywords: yacc, parse, question



I have a YACC problem which I am afraid I cannot solve
myself. I can understand why it behaves as described below but I
cannot find a solution to the problem:


How do I differ between an expression with or without surrounding
MATCHING paranthesis?


I am trying something like this:


%nonassoc LEFT_PAR RIGHT_PAR
%nonassoc HIGHER_PREC
rule1 : LEFT_PAR expr RIGHT_PAR { .... }
rule2 : expr { .... }
expr : sub_expr %prec HIGHER_PREC { .... }
  ... (several rules) ...
sub_exprX: LEFT_PAR sub_expr RIGHT_PAR { .... }




With this attempt, ALL expressions starting with left paranthesis (on
top level) are caught by rule1 even though there is no matching
parantesis at the end. All other expressions with rule2.


Examples:
A + B OK rule2
(A + B) OK rule1
(A) + B SYNTAX ERROR
((A) + B) OK rule1
A + (B) OK rule2
(A) + (B) SYNTAX ERROR


If I remove '%prec HIGHER_PREC', rule1 is never activated
and all examples are caught by rule2.


Any suggestions? Is there a solution to this problem?


Thanks in advance,
Per Mattsson
[Your problem is excess faith in precedence declarations. If you declare
the precedence of some tokens, yacc will use that to resolve shift/reduce
conflicts due to precedence conflicts. Unfortunately, yacc isn't very
smart and will also use them to resolve shift/reduce conflicts due to
errors and ambiguities in the grammar, which is what is happening here.
Take out all the precedences and rewrite your grammar to say what you
want it to say. -John]




--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.