problem with Bison

Stephan Houben <stephan@pcrm.win.tue.nl>
16 May 1999 15:23:29 -0400

          From comp.compilers

Related articles
problem with Bison stephan@pcrm.win.tue.nl (Stephan Houben) (1999-05-16)
| List of all articles for this month |

From: Stephan Houben <stephan@pcrm.win.tue.nl>
Newsgroups: comp.compilers
Date: 16 May 1999 15:23:29 -0400
Organization: Eindhoven University of Technology, The Netherlands
Keywords: parse, yacc, question
X-Public-Domain: Yes



Hello group,


I have a problem with the following Bison grammar.


----------------begin test.y----------------
%{
#include <stdio.h>


int yylex()
{
    return getchar();
}


void yyerror(char *msg)
{
    printf("%s\n", msg);
}
%}


%%


phrase:
    assignment '\n'
| expression '\n'
;


assignment:
  '#' paramlist '=' expression
;


paramlist:
/*empty*/
| '#' paramlist
;


expression:
  '#'
| application
;


application: expression expression
;


%%


int main()
{
    yyparse();


    return 0;
}
----------------end test.y----------------


This is supposed to handle both "expressions", which have the
form "#\n", "##\n", "###\n", etc., and "definitions", which have
the form "#=##\n", "##=###\n", etc..


However, the resulting program gives a parse error on "##\n", i.e.
basically on any expression except for "#\n".


I suppose the problem is that Bison can't tell by looking at the
start of the input whether it is an expression or a definition.
The only solution I see is to have definitions start with some
other symbol, say'!', but I think that's ugly, so I'd rather avoid that.


Has anyone an idea to get Bison to do what I want?


Thanks in advance,


Stephan Houben
[Well, the two shift-reduce conflicts that yacc gets should give you a
starting place. The easiest fix is to flatten the grammar, that is, get
rid of the assignment rule:


phrase:
  '#' paramlist '=' expression '\n'
| expression '\n' ;


You also need to fix your application rule which is extremely ambiguous,
e.g:


application: '#' expression ;


-John]





Post a followup to this message

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