Related articles |
---|
Yacc precedence bug slv@seg.npl.co.uk (S Voogd10 Jan 91 11:20 GMT) (1991-01-10) |
Re: Yacc precedence bug megatest!djones@decwrl.dec.com (1991-01-11) |
Re: Yacc precedence bug corbett@road.Eng.Sun.COM (1991-01-11) |
Re: Yacc precedence bug S.R.Adams@ecs.southampton.ac.uk (Stephen Adams) (1991-01-11) |
Re: Yacc precedence bug thorinn@diku.dk (Lars Henrik Mathiesen) (1991-01-11) |
Re: Yacc precedence bug klaus%ipkima.hanse.de@RELAY.CS.NET (Klaus Thalhofer) (1991-01-14) |
Newsgroups: | comp.compilers |
From: | Lars Henrik Mathiesen <thorinn@diku.dk> |
Keywords: | yacc, parse, debug |
Organization: | Compilers Central |
References: | <7993.9101101030@guava.seg.npl.co.uk> |
Date: | Fri, 11 Jan 91 20:23:44 +0100 |
In article <7993.9101101030@guava.seg.npl.co.uk>, slv@seg.npl.co.uk (S
Voogd) writes:
>You can give a token a precedence and associativity by
>declaring this in the declaration-part of a YACC-file
>According to the manual you can also change the precedence
>and associativity of a token in the grammar-rules by
>using the '%prec'-mechanism.
This is not exactly correct, and therein lies your problem. The precedence
and associativity (p+a) feature of YACC resolves shift/reduce conflicts by
comparing the p+a of a production (grammar rule) with the p+a of the next
token on the input. Usually, a production has the p+a of its first
terminal (if any), but it can be specified explicitly with %prec.
>We enclose a simple desk calculator and reverse the usual
>precedence of '+' with '*' using the '%prec'-mechanism.
>It seems that the associativity of tokens can be changed.(?)
>But their precedence levels are now the same.
You have changed the p+a for the _productions_. Those of the tokens
themselves are unchanged. In "a*b+c", the p+a of "+" is compared to itself
to determine the parsing, and since it is left associative, the "wrong"
parsing results. (If it was right associative, "a+b*c" would go "wrong".)
>If it is a bug, is there a way of changing the prec. and
>ass. of tokens ? (We really need it!)
The p+a of tokens are determined by their declaration and cannot be
changed. Only the p+a of productions can be changed. You need to specify
%prec so that the p+a of each production stands in the correct relation to
the p+a of all relevant following tokens (those that cause shift/reduce
conflicts).
A very common way of doing this is to introduce some extra tokens whose
only purpose is to carry a needed p+a; see the use of UMINUS in Appendix A
of the YACC reference. You could also drop the precedence kludgery and
write your grammar out in full (it might become quite large, however).
--
Lars Mathiesen, DIKU, U of Copenhagen, Denmark [uunet!]mcsun!diku!thorinn
Institute of Datalogy -- we're scientists, not engineers. thorinn@diku.dk
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.