OCamlLex 不区分大小写

标签 ocaml ocamllex

有没有办法在 Ocamllex 规范中使用区分大小写的 token ?
我已经尝试以这种方式制作不区分大小写的 token :

let token = parser
    ...
   | ['C''c']['A''a']['S''s']['E''e'] { CASE }
    ...

但我正在寻找其他东西,如果存在的话。

最佳答案

使用接受小写和大写的普通标记词法分析器,并在表中查找关键字,忽略大小写:

{
type token = Case | Test | Ident of string

let keyword_tbl = Hashtbl.create 64

let _ = List.iter (fun (name, keyword) ->
    Hashtbl.add keyword_tbl name keyword) [
    "case", Case;
    "test", Test;
  ]
}

let ident_char = ['a'-'z' 'A'-'Z' '_']

rule next_token = parse
  | ident_char+ as s {
      let canon = String.lowercase s in
      try Hashtbl.find keyword_tbl canon
      with Not_found ->
        (* `Ident canon` if you want case-insensitive vars as well
         * as keywords *)
        Ident s
    }

关于OCamlLex 不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068495/

相关文章:

types - 确定 OCaml 表达式类型的一般方法是什么?

ocaml - 指定 ocamllex 编码

ocaml - 创建一个简单的 camlp4 语法扩展

f# - 将 OCaml 转换为 F# : Converting OCaml quotation add and quotation expander to F#

ocaml - 扩展 ocamllex 以获取更大的词典

python - Python 到 CIL(C 中间语言)的翻译

compiler-construction - 将源代码位置信息添加到前端

ocaml - 为什么 OCaml 不支持记录子类型?

ide - OCaml 类型导向的 API 搜索