help-bison
[Top][All Lists]
Advanced

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

what about now ?! (was: handle shift/reduce conflict)


From: Fiordean Dacian
Subject: what about now ?! (was: handle shift/reduce conflict)
Date: Tue, 2 Jul 2002 15:54:33 +0200

Hi,
 
I've handled the shift/reduce conflict, and still my parser doesn't eat the input in the way I want. Could anyone sugest something?
 
The lexer:

%{
#include <string.h>
#include <stdlib.h>
#include "xx.cpp.h"
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
    { \
    int c = getchar(); \
    result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
    }
 
%}
 
/* macros */
BLANK       [ \t\n]
LETTER       [A-Za-z]
DIGIT       [0-9]
NATIONAL      address@hidden|\}\~\^]
ALPHA       {LETTER}|{DIGIT}|{NATIONAL}
WORD       ({ALPHA}|".")*{ALPHA}({ALPHA}|".")*
OTHER       [\?\&]
SPECIAL       [\+\-\%\!\/\>\*\(\)\"\,\=\:]
NQTEXT       {ALPHA}|{OTHER}|{SPECIAL}|[\.\_\ \;]
STRING       [^;]+
NAME       {WORD}("_"{WORD})*
%%
 
(l|L)(o|O)(o|O)(p|P)             {
                   return KW_LOOP;
                  }
(e|E)(n|N)(d|D)(l|L)(o|O)(o|O)(p|P)          {
                   return KW_ENDLOOP;
                  }
(b|B)(e|E)(g|G)(i|I)(n|N)            {
                   return KW_BEGIN;
                  }
(i|I)(n|N)(f|F)               {
                   return KW_INF;
                  }
{NAME} {
   xxlval.string_type = new char[strlen(xxtext) + 1];
   strcpy(xxlval.string_type, xxtext);
   return TK_NAME;
  }
.       {
        return xxtext[0];
       }
 
.. and the parser:

%{
#include <math.h>
#include <string.h>
%}
 
%{
extern int xxlex();
extern int xxerror(const char *s);
%}
 
%union
{
 char* string_type;
}
 
%token <string_type> TK_NAME
%token KW_LOOP
%token KW_ENDLOOP
%token KW_BEGIN
%token KW_INF
 
%type <string_type> possible_loop_boundary
%type <string_type> possible_inline_expr_identification
%type <string_type> possible_inf_natural
%type <string_type> inf_natural
%type <string_type> name
 
%right LOWER_THAN_KW_LOOP
%right KW_LOOP
 
/* Grammar follows */
%%
msc_body
  : msc_statement_list  %prec LOWER_THAN_KW_LOOP
  | /* empty */    %prec LOWER_THAN_KW_LOOP
  ;
 
msc_statement_list
     : msc_statement
     | msc_statement_list msc_statement
     ;
 
msc_statement
    : instance_event
    ;
 
instance_event
    : shared_inline_expr
    ;
 
shared_inline_expr
    : shared_loop_expr
    ;
 
shared_loop_expr
    : KW_LOOP possible_loop_boundary KW_BEGIN possible_inline_expr_identification end
     {
      printf("...\n");
     }
 
    msc_body
    {
     printf("...\n");
    }
    
    KW_LOOP end
    {
     printf("...\n");
    }
    ;
 
possible_loop_boundary
    : /*empty*/  { $$=0; }
    | '<' inf_natural possible_inf_natural '>' 
     {
      char *ret = new char[strlen($2) + strlen($3) + 3];
      sprintf(ret, "<%s%s>", $2, $3);
      $$=ret;
     }
    ;
 
possible_inline_expr_identification
    : /*empty*/  { $$=NULL; }
    | name  { $$=$1; }
    ;
 
possible_inf_natural
    : /*empty*/  { $$=NULL; }
    | ',' inf_natural
     {
       char *ret = new char[strlen($2) + 3];
       sprintf(ret, ", %s", $2);
       $$=ret;
     }
    ;
 
inf_natural
    : KW_INF 
     {
      char *s = new char[3];
      strcpy(s, "inf");
      $$=s;
     }
    | name  { $$=$1; }
    ;
 
name
 : TK_NAME
 ;
 
end
 : ';'
    ;
 

%%
 

/* programs */
 
extern "C"
{
 int xxwrap()
 {
  return 1;
 }
}
 
int xxerror(const char *s)
{
 return 0;
}
 
 
 
 

reply via email to

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