当预先知道N时,flex / bison是否适合解析包含N个顶点的数据结构,这些顶点由表示x,y,z坐标的三个浮点组成?如果是这样,解析器/词法分析器的设置是什么样的?
struct Vertex
{
float x;
float y;
float z;
};
输入文字
Vertices: n
x1 y1 z1
x2 y2 z2
...
xN yN zN
%token VERTICES_IDENTIFIER
%token COLON
%token NUM
Vertices { return VERTICES_IDENTIFIER;}
: { return COLON; }
[0-9]+ |
[0-9]+"."[0-9]* {return NUM;}
答案 0 :(得分:2)
你没有把你的语法文件;所以,我猜是这样的:
%{
int num_vertices, idx;
%}
...
%%
file: VERTICES_IDENTIFIER ':' NUM { num_vertices = $3; idx = 0; }
| NUM NUM NUM { set_vertice(idx++, $1, $2, $3); }
;
..并编写函数set_vertice(no, x, y, z);
答案 1 :(得分:1)
你的解析器规则会像这样,适应@xtof pernaud。定义列表是递归定义的。
%{
int num_vertices;
int idx;
%}
...
program : count_definition vertex_definitions { /* check that the number of vertices
stored = num_vertices */
}
;
count_definition : VERTICES_IDENTIFIER COLON NUM { num_vertices = $3; idx = 0;
/* allocate data structure */
}
;
vertex_definitions : vertex_definitions vertex_definition
| vertex_definition
;
vertex_definition : NUM NUM NUM { /* check that idx does not exceed num_vertices */
store_vertex(idx++, $1, $2, $3);
}
当我第一次阅读这个问题时,我认为维度(3)的数量是可变的,以及点(顶点)的数量(N)。如果您发现需要将工具扩展到可变数量的维度,您可能会发现在您定义的顶点的每个浮点列表之间引入一个分隔符(如分号)很有用。
根据您的需要,您可能会发现可以使用随每次store_vertex()
调用动态增长的数据结构,然后您根本不需要在输入文件中声明num_vertices