开发者

What is wrong with this yacc file?

开发者 https://www.devze.com 2023-02-02 08:34 出处:网络
I\'m getting this error tema4.y:13.19-26: syntax error, unexpected typetype on the following code, please help me!

I'm getting this error tema4.y:13.19-26: syntax error, unexpected typetype on the following code, please help me!

%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
%}
%union {
int intval;
char* strval;
char* charval;
}
%token <charval>SHR <intval>NR
%token CMP
%type <strval>str <intval>expr
%nonassoc CMP '|'   
%left '+' '-'
%left '*' '`' '#'

%start s
%%
s:str {printf("%s \n",$<strval>$);}
 | expr {printf("%s \n",$<intval>$)}
 ;
str : str '+' str {
                   char* s=malloc(strlen($1)+strlen($3)+1);
                   strcpy(s,$1);
                   strcat(s,$3);
                   $$=s;
                   }
    | str '-' str {
                   char *s=malloc(strlen($1));
                   char *sir=malloc(strlen($1)-strlen($3));
                   char *pt,*ps;
                   int i;
                   strcpy(s,$1);
                   pt=strstr(s,$3);   
                   if(pt) {
                           ps=pt+strlen($3);
                           strncpy(pt,ps,strlen(ps)); 
                           ps+=strlen(ps);
                           strncpy(sir,s,strlen($1)-strlen($3));
                          }
                  $$=sir;
                  }
    | str '*' NR {
                   int i;
                   char* s=malloc(strlen($1)*NR+1);
                   for(i=0;i<$3;i++)
                   {
                      strcat(s,$1);
                   }
                   $$=s;
                 }
    | str '#' NR {
                   char *s=malloc($3+1);
                   strcpy(s,$1);
                   if($3>strlen($1)) {printf("Nr prea mare\n");exit(1);}
                   else {s=s+strlen(s)-$3;
                        $$=s;}
                 }
    | NR '`' str {
                  char *s=malloc($1+1); 
                  strncpy(s,$3,$1);
                  $$=s;
                 }

    | '(' str ')' {
                  $$=$2;
                  }
    | SHR {
         char* s=malloc(strlen($1));
         strcpy(s,$1);
         $$=s;
         }
    | expr
    ;
expr : str CMP str {
                开发者_如何学Python  if(strcmp($1,$3)) $$=0;
                  else $$=1;
                  }
    | '|' str '|' {
                  $$=strlen($2);
                  }
 ;
%%
int main(){
   yyparse();
}


John has the answer for your first error -- only one type in a %type or %token declaration. For the second error (type clash), the problem is your rule str: expr ; -- str is a <strval> while expr is an <intval>, so the default action ({ $$ = $1; } effectively) has a type clash.

You also have a reduce/reduce conflict somewhere -- you'll need to run yacc with -d and look at the resulting y.output file for more detail on what the conflict is.


As best I can tell you can only have one <type> in a %token or %type declaration. You can have multiple tokens or non-terminals but only one type name. Try splitting those lines:

%token <charval> SHR
%token <intval> NR
%token CMP
%type <strval> str
%type <intval> expr
0

精彩评论

暂无评论...
验证码 换一张
取 消