problem with Bison

Stephan Houben <>
16 May 1999 15:23:29 -0400

          From comp.compilers

Related articles
problem with Bison (Stephan Houben) (1999-05-16)
| List of all articles for this month |

From: Stephan Houben <>
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);


    assignment '\n'
| expression '\n'

  '#' paramlist '=' expression

| '#' paramlist

| application

application: expression expression


int main()

    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:

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

You also need to fix your application rule which is extremely ambiguous,

application: '#' expression ;


Post a followup to this message

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