【问题标题】:Bison - nonterminals useless in grammarBison - 语法中无用的非终结符
【发布时间】:2020-06-03 20:57:37
【问题描述】:

0

我正在做作业,但我的野牛文件中有一些错误。我收到错误,例如“blabla”没有声明的类型。我的代码和错误如下。我正在尝试做一个类型检查器,我有一个标题、flex 和一个野牛文件。 Flex 返回我需要的令牌。我写了一个函数来创建一个属性,然后我检查这两个属性类型是否相同。如果不一样,我会给出一个错误输出,但是当我编译它时,我得到了这些错误。我没弄明白。我做错了什么?

在野牛文件中:

    %{
 void yyerror(char *s);
 int yylex();
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 int symbols[52];
 int symbolVal(char symbol);
 void updateSymbolVal(char symbol, int val);
%}

%union {int num,char id}

%token DOGRULUK
%token PARANTEZ_AC
%token PARANTEZ_KAPA
%token ISE
%token NOKTA
%token EGER
%token YADA
%token YOKSA
%token YORUM
%token YORUM_AC
%token YORUM_KAPA
%token ESITTIR
%token VEYA
%token DEGIL
%token DEMEK
%token ANCAKVEANCAK
%token VE
%token KUCUKESIT
%token KUCUKTUR
%token BUYUKESIT
%token BUYUKTUR

%token EKRANAYAZ
%token TARA
%token DONGU
%token ARTI
%token EKSI
%token BOLU
%token CARPI
%token KALAN
%token CALISTIR
%token BOLUM
%token SAYI
%token YAZIDIZISI
%token conditional_expression
%token identifier
%token left_hand_side


%right ESITTIR BUYUKTUR KUCUKTUR BUYUKESIT KUCUKESIT
%nonassoc ANCAKVEANCAK VE VEYA DEGIL DEMEK

%type <id> identifier
%type <num>  assignment_expression relational_expression assignment conditional_expression left_hand_side expression

%%

type : YAZIDIZISI |
        SAYI



expression: assignment_expression ';' {$$ = $1;}
          | relational_expression ';' {$$ = $1;};

relational_expression: expression  ESITTIR expression  ';'      { $$ = $1 == $3;}
                      |expression  KUCUKTUR expression  ';'     { $$ = $1 < $3;}
                      |expression  KUCUKESIT expression  ';'    { $$ = $1 <= $3;}
                      |expression  BUYUKTUR expression  ';'     { $$ = $1 > $3;}
                      |expression  BUYUKESIT expression ';'     { $$ = $1 => $3;}
                      |expression  ANCAKVEANCAK expression ';'  { $$ = $1 <=> $3;}
                      |expression  DEMEK expression     ';'     { $$ = $1 => $3;}
                      |expression  VE expression    ';'         { $$ = $1 && $3;}
                      |expression  VEYA expression   ';'        { $$ = $1 || $3;}
                      |expression  DEGIL expression  ';'        { $$ = $1 != $3;};

assignment_expression: conditional_expression ';' {$$ = $1;}
                      |assignment           ';'   {$$ = $1;};


assignment: left_hand_side  ESITTIR assignment_expression ';'{$$ = $1 = $3};




%%
/* C Code*/

int computeSymbolIndex(char token)
{
    int idx = -1;
    if(islower(token)) {
        idx = token - 'a' + 26;
    } else if(isupper(token)) {
        idx = token - 'A';
    }
    return idx;
} 

int symbolVal(char symbol)
{
    int bucket = computeSymbolIndex(symbol);
    return symbols[bucket];
}

void updateSymbolVal(char symbol, int val)
{
    int bucket = computeSymbolIndex(symbol);
    symbols[bucket] = val;
}

int main (void) {
    /* init symbol table */
    int i;
    for(i=0; i<52; i++) {
        symbols[i] = 0;
    }

    return yyparse ( );
}

void yyerror(char *s) {
 fprintf(stderr, "%s\n", s);
 return 0;
}
int main(void) {
 yyparse();
 return 0;
} 

我从 yacc 收到这些关于我语法中无用规则的警告:

corona.y: uyarı: 4 nonterminals useless in grammar [-Wother]
corona.y: uyarı: 15 rules useless in grammar [-Wother]
corona.y:57.107-116: uyarı: nonterminal useless in grammar: expression [-Wother]
 %type <num>  assignment_expression relational_expression assignment conditional_expression left_hand_side expression
                                                                                                           ^^^^^^^^^^
corona.y:57.36-56: uyarı: nonterminal useless in grammar: relational_expression [-Wother]
 %type <num>  assignment_expression relational_expression assignment conditional_expression left_hand_side expression
                                    ^^^^^^^^^^^^^^^^^^^^^
corona.y:57.14-34: uyarı: nonterminal useless in grammar: assignment_expression [-Wother]
 %type <num>  assignment_expression relational_expression assignment conditional_expression left_hand_side expression
              ^^^^^^^^^^^^^^^^^^^^^
corona.y:57.58-67: uyarı: nonterminal useless in grammar: assignment [-Wother]
 %type <num>  assignment_expression relational_expression assignment conditional_expression left_hand_side expression
                                                          ^^^^^^^^^^
corona.y:66.13-48: uyarı: rule useless in grammar [-Wother]
 expression: assignment_expression ';' {$$ = $1;}
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:67.13-48: uyarı: rule useless in grammar [-Wother]
           | relational_expression ';' {$$ = $1;};
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:69.24-81: uyarı: rule useless in grammar [-Wother]
 relational_expression: expression  ESITTIR expression  ';'      { $$ = $1 == $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:70.24-80: uyarı: rule useless in grammar [-Wother]
                       |expression  KUCUKTUR expression  ';'     { $$ = $1 < $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:71.24-81: uyarı: rule useless in grammar [-Wother]
                       |expression  KUCUKESIT expression  ';'    { $$ = $1 <= $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:72.24-80: uyarı: rule useless in grammar [-Wother]
                       |expression  BUYUKTUR expression  ';'     { $$ = $1 > $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:73.24-81: uyarı: rule useless in grammar [-Wother]
                       |expression  BUYUKESIT expression ';'     { $$ = $1 => $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:74.24-82: uyarı: rule useless in grammar [-Wother]
                       |expression  ANCAKVEANCAK expression ';'  { $$ = $1 <=> $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:75.24-81: uyarı: rule useless in grammar [-Wother]
                       |expression  DEMEK expression     ';'     { $$ = $1 => $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:76.24-81: uyarı: rule useless in grammar [-Wother]
                       |expression  VE expression    ';'         { $$ = $1 && $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:77.24-81: uyarı: rule useless in grammar [-Wother]
                       |expression  VEYA expression   ';'        { $$ = $1 || $3;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:78.24-81: uyarı: rule useless in grammar [-Wother]
                       |expression  DEGIL expression  ';'        { $$ = $1 != $3;};
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:80.24-60: uyarı: rule useless in grammar [-Wother]
 assignment_expression: conditional_expression ';' {$$ = $1;}
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:81.24-60: uyarı: rule useless in grammar [-Wother]
                       |assignment           ';'   {$$ = $1;};
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
corona.y:84.13-75: uyarı: rule useless in grammar [-Wother]
 assignment: left_hand_side  ESITTIR assignment_expression ';'{$$ = $1 = $3};
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

【问题讨论】:

    标签: bison


    【解决方案1】:

    Yacc/bison(实际上,大多数解析器生成器)假设文件中第一个产生式的目标是解析器应该匹配的非终端。但是你的第一个非终端是

    type : YAZIDIZISI |
           SAYI
    

    bison/yacc 表示输入可以是令牌YAZIDIZISI 或令牌SAYI,仅此而已。这意味着你的其他非终端都没有用,所以野牛会警告你。

    如果不想让 bison/yacc 猜测从哪里开始,可以明确指定开始规则:

    %start expression
    

    否则,您可以移动您的规则,让顶级规则排在第一位。

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多