Search code examples
bisonyacc

Fatal Error : start symbol START does not derive any sentence


I am trying to develop a mini C compiler, and here is the code of the yacc program , kindly assist me where am I wrong and how do I improvise it.

The grammar must be able to recognize : The C program structure ie the preprocessor directives, the main function , the variable declarations, expressions, while loop , if and if else statements.

    %{
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    void yyerror(char* s);
    int yylex();
    extern int yylineno;
    %}
    
    %token T_INT T_DOUBLE T_FLOAT T_CHAR T_WHILE T_IF T_ELSE T_DO T_INCLUDE T_MAIN T_STRLITERAL T_EQCOMP T_NOTEQUAL T_GREATEREQ T_LESSEREQ T_NUM T_HEADER T_ID
    
    %start START
    
    %%
    START   :   PROG {printf("Valid syntax\n"); YYACCEPT;}
        ;
    PROG    :   T_INCLUDE'<'T_HEADER'>'PROG
        |   MAIN PROG
        |   DECLR';'PROG
        |   ASSGN';'PROG
        ;
    DECLR   :   TYPE LISTVAR
        ;
    LISTVAR :   LISTVAR','T_ID
        |   T_ID
        ;
    TYPE    :   T_INT
        |   T_FLOAT
        |   T_DOUBLE
        |   T_CHAR
        ;
    ASSGN   :   T_ID'='EXPR
        ;
    EXPR    :   EXPR REL_OP E
        |   E
        ;
    REL_OP  :   T_LESSEREQ
        |   T_GREATEREQ
        |   '<'
        |   '>'
        |   T_EQCOMP
        |   T_NOTEQUAL
        ;
    E   :   E'+'T
        |   E'-'T
        |   T
        ;
    T   :   T'*'F
        |   T'/'F
        |   F
        ;
    F   :   '('EXPR')'
        |   T_ID
        |   T_NUM
        ;
    
    MAIN    :   TYPE T_MAIN'('EMPTY_LISTVAR')''{'STMT'}'
            ;
    EMPTY_LISTVAR   :   LISTVAR
            |
            ;
    STMT    :   STMT_NO_BLOCK STMT
        |   BLOCK STMT
        |
        ;
    
    
    %nonassoc T_IFX;
    %nonassoc T_ELSE;
    
    STMT_NO_BLOCK   :   DECLR';'
            |   ASSGN';'
            |   T_IF COND STMT %prec T_IFX 
            |   T_IF COND STMT T_ELSE STMT
            |   WHILE
            ;
    
    BLOCK   :   '{'STMT'}';
    WHILE   :   T_WHILE'('COND')' WHILE_2;
    COND    :   EXPR
        |   ASSGN
        ;
    WHILE_2 :   '{'STMT'}'
        |';'
        ;
    %%
    void yyerror(char *s)
    {
        printf("Error : %s at %d\n",s,yylineno);
    }
    
    main(int argc,char* argv[])
    {
        yyparse();
        return 0;
    }

These are the errors I am getting :

48 rules never reduced
parser.y: warning: 20 useless nonterminals and 48 useless rules
parser.y:13.8-12: fatal error: start symbol START does not derive any sentence

Solution

  • Every production for PROG is recursive. So the recursion can never end, and PROG cannot derive a finite sentence.

    Since the only production for START is PROG, that also applies to START.