如何使用flex在每个列表的末尾插入一个变量值



原始样本数据如下所示:

neg      
test  
-1.0 1.0    
-2.1 3.45
我必须使用flex将其转换为可识别的ocaml输入列表,如下所示:
let test  
=[  
[ -1.0 ; 1.0 ; 1.0; -1.0 ];  
[ -2.1 ; 3.45 ; 1.0; -1.0 ]  
];;

我可以这样转换:

let test  
=[  
[ -1.0 ; 1.0 ; 1.0 ; ]; 
[ -2.1 ; 3.45 ; 1.0 ; ]
];;

我的主要问题是:

  1. 如何将每个列表中的最后一个元素作为-1,如果原始数据说负和作为1,如果原始数据说pos?

我认为您需要添加某种形式的解析。看看Bison,它与flex集成得很好。所以你得到了一些像这样的规则

let_expression: "neg" identifier vector_list
{
//Finalize by printing stored data and add -1
}
              |  identifier vector_list
{
//Finalize by printing stored data and add 1
}
vector_list: vector_list vector
{
  //Store your number pairs in a std::vector<std::pair<T,T>>
}
           | vector
{
  //Initialize the vector list.
}
vector: const_num const_num
{
  //Store your numbers, maybe in a std::pair
  $$ = std::make_pair($1,$2)
}

最新更新