我对此有点困惑......我对野牛的语法看起来像这样..
%%
program:
| program func_defn
;
datatype: INTEGER { $$ = currentType = Type::IntegerTy; }
| FLOAT { $$ = currentType = Type::FloatTy; }
| VOID { $$ = currentType = Type::VoidTy; }
| STRING_LITERAL { $$ = currentType = Type::StringTy; }
;
retlist : datatype
{
builder->pushDataType(&getType($1));
}
;
arglist: datatype IDENTIFIER
{
builder->pushDataType(&getType($1));
Symbol *sym = builder->addSymbol($2, getType($1));
builder->pushArgName(sym);
}
| arglist ',' datatype IDENTIFIER
{
builder->pushDataType(&getType($3));
Symbol *sym = builder->addSymbol($4, getType($3));
builder->pushArgName(sym);
}
|
;
//as and when we find the identifiers , we need to add them to a list and use them while constructing the prototype/func
func_defn: FUNCTION IDENTIFIER LSQUARE retlist RSQUARE '(' arglist ')' '{'
{
const std::string& string = $5;
FunctionProtoType* fp = builder->getProtoType(string);//use current dataTypeList
/*
if(fp == NULL) //find the prototype in the module. if not found, add a new one
builder->addProtoType(string, getType($1), &fp);
*/
IcErr err = builder->addFunction(*fp);
if(err != eNoErr)
yyerror(errMsg[err]);
}
;
%%
我正在尝试解析这一行......
function first_method [int] (int a, int b) {
然而,这在某种程度上似乎根本没有解析。我没有任何shift-reduce或reduce-reduce冲突。
Compiling file: ica_test\func_defn.icaStarting parse
Entering state 0
Reducing stack by rule 1 (line 80):
-> $$ = nterm program (1.1: )
Stack now 0
Entering state 1
Reading a token: Next token is token FUNCTION (1.1: )
Shifting token FUNCTION (1.1: )
Entering state 3
Reading a token: Next token is token IDENTIFIER (1.1: )
Shifting token IDENTIFIER (1.1: )
Entering state 5
Reading a token: _Next token is token IDENTIFIER (1.1: )
function first_method [int] (int a, int b) {
^
Error on Line 1, Column 9: syntax error, unexpected IDENTIFIER, expecting LSQUAR
EError: popping token IDENTIFIER (1.1: )
Stack now 0 1 3
Error: popping token FUNCTION (1.1: )
Stack now 0 1
Error: popping nterm program (1.1: )
Stack now 0
Cleanup: discarding lookahead token IDENTIFIER (1.1: )
Stack now 0
Stopping compilation as we found some syntax errors in ica_test\func_defn.ica
!Press any key to continue . . .
答案 0 :(得分:3)
我猜你的IDENTIFIER
词法分析器规则不能包含_
(下划线)字符,因此将first_method
标记为标识符first
,然后{ {1}}与任何规则都不匹配,因此会回显到stdout(输出中_
之前的杂散下划线)。然后它会获得另一个标识符(Next
),这会导致语法错误,因为它需要method
。
我说“猜”,因为你没有显示你的词法分析器代码,但似乎很可能。
您应该通过修复[
规则来解决此问题,但您还应添加最终的词法规则,例如:
IDENTIFIER
匹配任何单个字符并将其返回给解析器,而不是将其回显给输出。这样你就不会得到随机字符回显到输出并被忽略,你会得到更明智的语法错误 - 它们会在YYDEBUG文本输出中显示为标记,所以你会知道什么是继续。