野牛拾取 C 函数指针作为函数调用
Bison picking up C function pointer as a function call?
有没有办法指定如果前瞻令牌是给定值,Bison规则不应匹配?
我目前有以下野牛语法(简化):
var_decl:
type ident
{
$$ = new NVariableDeclaration(*$1, *$2);
} |
type ident ASSIGN_EQUAL expr
{
$$ = new NVariableDeclaration(*$1, *$2, $4);
} |
type CURVED_OPEN STAR ident CURVED_CLOSE CURVED_OPEN func_decl_args CURVED_CLOSE
{
$$ = new NVariableDeclaration(*(new NFunctionPointerType(*$1, *$7)) /* TODO: free this memory */, *$4);
} |
type CURVED_OPEN STAR ident CURVED_CLOSE CURVED_OPEN func_decl_args CURVED_CLOSE ASSIGN_EQUAL expr
{
$$ = new NVariableDeclaration(*(new NFunctionPointerType(*$1, *$7)) /* TODO: free this memory */, *$4, $10);
} ;
...
deref:
STAR ident
{
$$ = new NDereferenceOperator(*$<ident>2);
} |
...
type:
ident
{
$$ = new NType($<type>1->name, 0, false);
delete $1;
} |
... ;
...
expr:
deref
{
$$ = $1;
} |
...
ident
{
$<ident>$ = $1;
} |
...
ident CURVED_OPEN call_args CURVED_CLOSE
{
$$ = new NMethodCall(*$1, *$3);
delete $3;
} |
...
CURVED_OPEN expr CURVED_CLOSE
{
$$ = $2;
} ;
...
call_args:
/* empty */
{
$$ = new ExpressionList();
} |
expr
{
$$ = new ExpressionList();
$$->push_back($1);
} |
call_args COMMA expr
{
$1->push_back($3);
} ;
问题是在解析时:
void (*ident)(char* some_arg);
它看到 void (*ident) 并推断它一定是函数调用而不是函数声明。 有没有办法告诉野牛,它应该倾向于向前看以匹配var_decl,而不是将 *识别和无效减少为 deref 和 exprs?
任何标识符都可以是类型
这正是问题所在。类 C 语言的 LALR(1) 语法(或类型类语法的语言)需要在令牌级别区分类型和其他标识符。也就是说,您需要 IDENT 和 TYPEIDENT 是两个不同的令牌。(您必须将有关标识符的数据从编译器反馈回分词器)。这是消除歧义的最标准方法,否则模棱两可的语法。
更新 例如,请参阅Yacc的ANSI C语法。
相关文章:
- 使用共享指针的函数调用,其对象应为 const
- 返回指向对象的指针的函数调用是否为 prvalue?
- VkSurfaceKHR 指针的值在函数调用后更改,无需任何显式赋值
- 指针在函数调用后更改其地址
- C++:这是使用整数变量作为函数调用指针的正确方法吗
- 使用函数指针时,ESP 未在函数调用中正确保存
- 为什么分配了 nullptr 的指针可以调用成员函数?
- 函数调用歧义(用户定义的转换和 Derived2Base 指针转换)
- 创建指针时是否没有构造函数调用
- 在父指针上调用虚函数
- 如何从另一个类调用指向成员的指针函数
- 在数组函数指针中调用void
- 这些函数调用的返回指针的区别是什么?
- 为什么我不能在插入的函数调用中直接使用返回的指针作为参数
- C++ 通过函数指针错误调用成员函数
- 函数调用中的 void 指针在 C++ 与 C 中
- 析构函数在与 STL 的共享指针中调用两次
- C++ 指针 - 函数调用中的重新分配
- C++递归指针函数调用
- 如何编写一个可以处理对象或指针函数调用的模板函数