用于收集参数的 Bison 语法
我有一个用于收集函数参数的野牛语法。到目前为止就是这样:
args: arg {$$ = intArray($1);} //pseudo-code function
| args arg {$$ = $1 + $2;} //pseudo-code array addition
arg : NUM {$$ = $1;}
| exp {$$ = $1;}
如何创建一个没有固定长度的整数数组,可以像 Bison 中的普通 C 数组一样使用?
I have a bison grammar for collecting arguments of a function. This is what it is so far:
args: arg {$ = intArray($1);} //pseudo-code function
| args arg {$ = $1 + $2;} //pseudo-code array addition
arg : NUM {$ = $1;}
| exp {$ = $1;}
How can I create an array of integers without a fixed length that can be used like a normal C array in Bison?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。
但是,您可以使用数据结构创建类似的东西。
如果您知道数据是什么类型,则可以使用
int*
或任何您想要的。然后只需使用realloc
来扩展它。 “size”是数组的当前大小,capacity是分配空间的大小。 (我们这样做是为了不再重新分配 1 个区块 - 它
最好一次分配大块的块)。
您还可以使用所谓的链接列表,它看起来像这样:
同样,如果您已经知道数据,则可以使用
int
(我在示例中使用了指针,因为所有指针都是相同的)长度(以字节为单位)。通常添加到列表的开头会更有效。但是,我发现使用 bison 拥有另一个结构时很好:
因为这样您就可以添加结尾而不会出现性能问题并保持正常顺序。
在野牛中,我们可以做类似的事情
(这里的所有代码都未经测试,可能需要稍作修改)
您可以在这里执行向量方法,其方式与我扩展链表的方式类似 - 只需重新分配并添加最后一个值。我认为向量可能更有效,但链表也可能效果很好。
链表的添加和删除元素的时间复杂度为
O(1)
,并且比向量更快(当空间不足时,无需不断移动块)。它的迭代速度也同样快,因为两者都是O(n)
。访问特定元素时必须小心,因为这需要迭代,这在链表中是O(n)
。向量可以在O(1)
中访问元素。You can't.
However, you can create something similar using data structures.
If you know what type data is, you can use
int*
or whatever you want. Then just userealloc
to expand it. "size" is the current size of the array, and capacity is thesize of the allocated space. (We do this so we don't keep reallocing 1 more block - Its
better to alloc chunks of blocks at a time).
You can also use what is called a linked list, which looks something like this:
Again, you could use
int
if you already know the data (I used a pointer in the example because all pointers are the same length in bytes).Usually its more efficient to add to the beginning of a list. However, I find it nice when using bison to have another struct:
Because then you can add the end without performance issues and keep the normal order.
In bison, we can do something like
(All code here is untested and might need slight modifications)
You can do the vector method here in a similar way to how I expanded the linked list - Just realloc and add in the last value. I think vectors might be more efficient, but a linked list might work well too.
Linked List has
O(1)
for adding and removing elements and is faster at that than a vector (no need to continually move blocks around when you run out of space). Its also just as fast to iterate, as both areO(n)
. You have to be careful about accessing a particular element, as that requires iteration, which isO(n)
in a linked list. A vector can access an element inO(1)
.