[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Use of %prec breaks associativity
From: |
Jeroen Ketema |
Subject: |
Use of %prec breaks associativity |
Date: |
Fri, 14 Nov 2008 11:04:11 +0100 |
User-agent: |
Thunderbird 2.0.0.17 (X11/20080922) |
Dear all,
When using %prec to change the precedence of a terminal symbol
bison changes the associativity of the terminal symbol both
in version 2.3 and 2.4. For an example see the attached file.
In the file you see:
%nonassoc '+'
%nonassoc PLUS_PREC
and later on:
proc '+' proc %prec PLUS_PREC
Hence, I would assume '+' is dealt with in a non-associative way.
At least the manual doesn't mention the associativity is changed
when precedence is used.
However, the generated parser accepts the following as input:
a+a+a
and gives as output:
((a+a)+a)
The same output is given when I replace both %non-assoc's by
either %left's or %right's. However, when I write instead:
%nonassoc '+' PLUS_PREC
The behaviour changes:
a+a+a
is no longer accepted as a valid string.
Regards,
Jeroen
%{
#include <stdio.h>
#include <string.h>
int yylex();
void yyerror(char const *s);
%}
%union{
char *str;
}
%type <str> proc
%nonassoc '+'
%nonassoc PLUS_PREC
%%
input: proc {printf("%s\n", $1); } ;
proc:
proc '+' proc %prec PLUS_PREC
{
$$ = malloc(strlen($1) + strlen($3) + 4 * sizeof(char));
strcpy($$, "(");
strcpy($$ + 1, $1);
strcpy($$ + 1 + strlen($1), "+");
strcpy($$ + 1 + strlen($1) + 1, $3);
strcpy($$ + 1 + strlen($1) + 1 + strlen($3), ")");
}
| 'a'
{
$$ = malloc(2 * sizeof(char));
strcpy($$, "a");
}
;
%%
int yylex(void)
{
char c = getchar();
if (c == '\n')
return 0;
/* Return a single char. */
return c;
}
void yyerror(char const *s)
{
fprintf(stderr, "%s\n", s);
}
int main(void)
{
return yyparse();
}
- Use of %prec breaks associativity,
Jeroen Ketema <=