0
votes

here CR is create SP is space RE is replace iam getting the output correctly for create or replace but not for just create. could anyone pls tell what is wrong with code but iam still getting this warning and hence not working p.y:10.5-6: warning: rule useless in parser due to conflicts

    %token CR TRI SP RE OR BEF AFT IOF INS UPD DEL ON OF 
    %%
    s:e '\n' { printf("valid variable\n");f=1; };
    e:TPR SP TRI;
    TPR:CR
    |CR SP OR SP RE;
1
Why are you passing spaces to your parser?rici

1 Answers

0
votes

It's rarely a good idea to pass whitespace to the parser. It only complicates the grammar, providing little or no additional value.

It is also always a good idea to adopt a single convention for the names of terminals and non-terminals. If you are going to use ALL CAPS for terminals (which is the normal convention), then don't use it also for non-terminals such as TPR. Also, the use of meaningful names and literal strings will make your grammar much more readable.

The "rule useless in parser due to conflicts" warning is always accompanied by one or more shift/reduce or reduce/reduce conflicts. Normally, the solution is to fix the conflicts. In this case, you could do so by simply not passing the whitespace to the parser.

Here is your grammar, I think: (I'm guessing what your abbreviations mean)

%token CR "create" OR "or" RE "replace"
%token TABLE_IDENTIFIER
%%
statement: expr '\n'  { /* Some action */ }
expr: table_producer TABLE_IDENTIFIER
table_producer
    : "create"
    | "create" "or" "replace"

Written this way, without the whitespace, the grammar does not have any conflicts. If we reintroduce the whitespace:

%token CR "create" OR "or" RE "replace"
%token TABLE_IDENTIFIER SPACE
%%
statement: expr '\n'  { /* Some action */ }
expr: table_producer SPACE TABLE_IDENTIFIER
table_producer
    : "create"
    | "create" SPACE "or" SPACE "replace"

then there is a shift/reduce conflict after create is recognized. The lookahead will be SPACE, but the parser cannot know whether that SPACE is part of the second table_producer production (create or...) or part of the expr production (create table_name).

There must be some punctuation between two words, otherwise they would be recognized by the lexer as a single-word. So the fact that the words are separated by whitespace is not meaningful; if the lexer simply keeps the whitespace to itself, as is normal, then the conflict disappears.