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};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Yacc / bison(实际上是大多数解析器生成器)都假定文件中第一个生成的目标是解析器应匹配的非终结符。但是您的第一个非终端是
type : YAZIDIZISI |
SAYI
哪个野牛/ yacc表示输入可以是令牌YAZIDIZISI
或令牌SAYI
,而不能是其他任何东西。这意味着您的其他非终端设备均无用,因此野牛会警告您。
如果您不希望野牛/ yacc猜出从哪里开始,则可以明确指定开始规则:
%start expression
否则,您可以移动规则,使顶级规则排在第一位。