help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reduce if you can behavior wanted


From: Hans Aberg
Subject: Re: Reduce if you can behavior wanted
Date: Sat, 29 Apr 2006 19:50:26 +0200

Perhaps you might need actions that execute immediately in a split, which has been discussed. I do not know though if there is a hack around it.


On 29 Apr 2006, at 00:45, Derek M Jones wrote:

All,

I am trying to parse C declaration one at a time,
calling yyparse for each one in turn.  For this to
work I need yyparse to stop reading in tokens after
the last token of the declaration currently being processed.
The obvious solution is to set a flag in one of the
actions (ie, seen_decl in the cut down grammar below).

This works very well until there are multiple parse
stacks and the actions get deferred (so seen_decl is not set
at the same time as it would when there is a single parse stack).
The declaration 'int i;' is one such case (the i could be
typedef name or an identifier; the ambiguity problem
is solved by adding appropriate the %dprec).  The parser
reads one more token than I want it to (eg, given the
input 'int j; int k;' the reductions needed to collapse
the parse stacks don't happen until the second int is read).

Reading the yydebug output I see that the actions for
the two stacks are very different when the semicolon is
read, so they don't get chance to merge).

Does anybody have any suggestions?  Is there some
'reduce if you can' behavior I can cause to happen?


%glr-parser

%token
INT
IDENT

%{
#include <ctype.h>
#include <stdio.h>

static const char *input = "I j; I k;";

#define YYSTYPE char *
#define YYDEBUG 1

int seen_decl = 0;

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

static int yylex (void);
%}

%%

TU:
        translation_unit
        { }             ;

translation_unit:
              declaration
              {
              printf("seen declaration\n");
              seen_decl=1;
              }         ;

declaration:
              declaration_specifiers            ';' %dprec 1
              { }         |
              declaration_specifiers declarator ';' %dprec 2
              { }         ;

declaration_specifiers:
              type_specifier
              { }         |
              declaration_specifiers type_specifier
              { }         ;

type_specifier:
              INT
              { }         |
              typedef_name
              { }         ;

declarator:
              IDENT  %dprec 2
              { }         ;

typedef_name:
              IDENT  %dprec 1
              { }         ;

%%

static int yylex (void)
{
char c;

if (seen_decl)
   return 0; /* return 'EOF' if seen a declaration */

c = (*input ? *input++ : 0);

while (isspace(c))
   c = *input++;

asprintf (&yylval, "%c", c);
printf("> %c\n",  c);
return (c == 'I') ? INT : (isalpha(c) ? IDENT : c);
}

int main (void)
{
yydebug=1;

printf ("%i\n", yyparse ());
}



--
Derek M. Jones                              tel: +44 (0) 1252 520 667
Knowledge Software Ltd                      mailto:address@hidden
Applications Standards Conformance Testing    http://www.knosof.co.uk


_______________________________________________
address@hidden http://lists.gnu.org/mailman/listinfo/help-bison





reply via email to

[Prev in Thread] Current Thread [Next in Thread]