0
votes

I'm trying below YACC grammar part but it gives me, what is the wrong with this? and how can I solve it? are there some rules to define grammar in Bison/ YACC ?

C:\Users\Dilan\Documents\LexProgram\miniProject>bison -dy videostore.y
conflicts: 1 shift/reduce

and my YACC code is :

%start VideoStore
%token title
%token type
%token name
%token days


%%

VideoStore  :   MOVIES CUSTOMERS
        |  CUSTOMERS MOVIES  
        |  MOVIES
        |  CUSTOMERS
        ;


MOVIES      :   MOVIES MOVIE
        |   MOVIE
        ;

MOVIE       :   title type RENTALS2
        ;

RENTALS2    :   RENTALS2 RENTAL2
        |   RENTAL2
        ;

RENTAL2     :   CUSTOMER1
        ;


CUSTOMER1   :   name days
        ;

CUSTOMERS   :   CUSTOMERS CUSTOMER
        |   CUSTOMER
        ;

CUSTOMER    :   name days RENTALS
        ;
RENTALS     :   RENTALS RENTAL
        |   RENTAL
        ;

RENTAL      :   MOVIE1
        ;

MOVIE1      :   title type
        ;

%%
int trace=0;

My problem is VideoStore.y: conflicts: 2 shift/reduce and how can wrote LEX for this ?

1
Do you have an example input file for this? Basically, the warning means your language has ambiguities. It may or may not work. The larger problem will be that you're not using keywords and your lexer would need to identify wheter a string is a customer name or a movie title. Suggestion: Use XML for this instead.PMF
What are the keywords lexer would need? and. I have gone through cis.uab.edu/softcom/GrammarInference/publications/scesm2005.pdf this research sheet and I want to try this grammar, not all the research sheet. I'm learning thisuser3781725
The lexer doesn't need keywords, the parser does. The lexer only sees sequences of characters, so any text would probably be parsed as a "string" (instead of either a title or a name), which would give more ambiguities still. As noted in the answer, you have to introduce some terminal tokens into your file, such as delimiters or parenthesis around a group of things.PMF

1 Answers

2
votes

Use the -v option to bison, which gives you a .output file showing all the states and where any conflicts are. In you case, you have conflicts in states 16 and 20:

state 16

    7 MOVIE: title type RENTALS2 .
    8 RENTALS2: RENTALS2 . RENTAL2

    name  shift, and go to state 15

    name      [reduce using rule 7 (MOVIE)]
    $default  reduce using rule 7 (MOVIE)

    RENTAL2    go to state 24
    CUSTOMER1  go to state 18

This tells you that the problem is that the parser can't figure out where the end of a MOVIE rule is with a single token of lookahead -- the MOVIE ends with a sequence of one or more RENTAL2 rules, but since a RENTAL2 begins with a name and things that come after a MOVIE (such as a CUSTOMER rule) also start with name it can't tell whether it should be trying to match more RENTAL2s or not.

Most commonly, you would fix this by using some sort of delimiter at the end of your RENTALS2 list, such as a ';'