Fatal error: start character does not display any sentence

I am trying to develop a calculator program using lex and yacc. I save the following error:

calc.y: warning: 5 nonterminals useless in grammar [-Wother]
calc.y: warning: 8 rules useless in grammar [-Wother]
calc.y:8.1: fatal error: start symbol I does not derive any sentence
 I : E '\n' {printf("%d\n",$1);}

I looked at similar problems, but they had endless recursions, but that wasn’t.

calc.l

%{
#include"y.tab.h"
%}

digits [0-9]*

%%
{digits} {return DIGITS}
%%

int yywrap()
{
} 

calc.y

%{
    #include<stdio.h>   
%}

%token DIGITS

%%
I : E '\n' {printf("%d\n",$1);}
  ;
E : E '+' F {$$ = $1 + $3;}
  | E '-' F {$$ = $1 - $3;}
  ;
F : F '*' G {$$ = $1 * $3;}
  | F '/' G {$$ = $1 / $3;}
G :'('E')' 
  | DIGITS 
  ;
%%

int main()
{
    yyparse();
}
int yyerror()
{
}
+4
source share
1 answer

I do not know yacc, but:

  • To build I, you will need E:

    I : E '\n'
    
  • To build E, you will need E:

    E : E '+' F
      | E '-' F
    

Since there is no way to build E, if you don’t have it yet (and in the beginning you don’t have anything), there is no way to build I.

Or looking at him from the other side: Einfinitely recursive, because he always turns to himself.


lexer, DIGITS.

DIGITS G.

G, , (F '*' G F '/' G), F, F. , .

+3

Source: https://habr.com/ru/post/1666222/


All Articles