Re: Matching paranthesis in YACC expression

Chris Clark USG <clark@quarry.zk3.dec.com>
20 Jan 1998 23:52:01 -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: Chris Clark USG <clark@quarry.zk3.dec.com>
Newsgroups: comp.compilers
Date: 20 Jan 1998 23:52:01 -0500
Organization: Digital Equipment Corporation - Marlboro, MA
References: 98-01-063
Keywords: parse, yacc

Par Mattsson asked:


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


%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 { .... }


You need to make certain that your nested parenthesis rule cannot be
reduced from your top rule except when appearing with another
operator. Following your example, something like this:


rule1: nested_expr ; /* handles parens at top level */
rule2: unnested_expr ; /* no parens at top level for this rule */
/* unnested_expr DOES NOT have a parens alternative */
unnested_expr: nested_expr PLUS nested_expr
| identifier ;
/* nested_expr adds the parens alternative */
nested_expr: LEFT_PAR nested_expr RIGHT_PAR
| unnested_expr ;


Now, I hope this wasn't a homework excercise. It seems like too
practical of a concern to be an exercise, but it does have a certain
simplicity to it.


-Chris Clark
************************************************************************
Compiler Resources, Inc. email: compres@world.std.com
3 Proctor St. http://world.std.com/~compres
Hopkinton, MA 01748 phone: (508) 435-5016
USA 24hr fax: (508) 435-4847
--


Post a followup to this message

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