I'm trying to implement the grammar of this exercise using lex & yacc, but it throws this conflict: warning: 5 shift/reduce conflicts [-Wconflicts-sr] and takes every string as invalid, I think it has to be something with the precedence: It is supposed to accept strings like: ba#ababadada#bad#dabbada and reject: dad#ad#abaadad#badadbaad This is what I've tried:
%{
#include <stdio.h>
int flag=0;
%}
%{
void yyerror();
%}
%token A B D NL
%%
str1 : Oracion NL { printf("\n Sequence accepted");return(0);}
;
Oracion : Oracion '#' Parrafo { }
| Parrafo { }
;
Parrafo : Silaba Parrafo Silaba { }
| Silaba { }
;
Silaba : Explosion Alto { }
| A Explosion { }
| A Alto { }
| Explosion { }
;
Explosion : Alto A { }
;
Alto : B { }
| D { }
;
%%
void main()
{
printf("\n write a sequence\n");
yyparse();
if(flag == 0)
printf("\n Valid sequence");
}
void yyerror()
{
printf("\n Invalid Sequence\n\n");
flag=1;
}
return (0)
in the actionstr1: Oracion NL { printf("\n Sequence accepted");return(0);}
is not correct. Did your instructor tell you to do that? Or did you get it from some on-line tutorial (which probably indicates that the tutorial isn't very good). – ricireturn 0
and a variable calledflag
deleted a question which I was in the process of answering. Perhaps they were a classmate of yours, I don't know. But it's super annoying. Anyway, I'd really like to know where that idiom comes from. – rici