【问题标题】:ocaml stream input type syntaxocaml 流输入类型语法
【发布时间】:2017-10-23 02:26:58
【问题描述】:

我正在尝试在 Ocaml 版本 4.02.5 中构建一个词法分析器,我似乎无法让流类型工作,而且我几乎找不到任何对 Ocaml 有用的东西。我阅读的大多数内容都过于复杂,没有从文件中获取字符输入并对其进行词法分析的示例。

我收到的错误是在我的第一个流声明中,具体的语法位置在 '[

open Printf;;
open Stream;;
type sign =
    Plus
    | Minus;;

type atom =

    T
    |   NIL
    |   Int of int
    |  Ident of string;;

type token =

Lparen
    |  Rparen
    |  Dot
    |  Sign of sign
    |  Atom of atom;;
Stream.t char s from "lines.txt";;
let rec spaces s=

match s with parser
    [< '' '|' '\t' | '\n' ; _ >] -> spaces s (* ERROR HERE ON '[<' *)
    |  [<  >]  -> ()

;;

let rec lexid str s =

    match s with parser

        [< ' 'a'..'z'| 'A'....'Z'| '0'...'9' as c; _ >] -> lexid (str ^     (Char.escaped c)) s

| [< >] -> str;;
let rec lexint v s=
    match s with parser
    [< ‘’0’..’9’ as n; _ >] -> lexint ((10*v)+(int_of_string(Char.escaped n))) s
    | [< >] -> v
;;

【问题讨论】:

    标签: ocaml lexer


    【解决方案1】:

    2001 年底发布的 OCaml 版本 3.03 中删除了对流语法的直接支持。

    从那时起,camlp4 提供了对这些语法扩展的支持。

    但是,camlp4 本身现在已被弃用,取而代之的是 ppx。由于您使用的是旧版本的 OCaml(4.02 是从 2015 年开始的),camlp4 可能可用。

    OCaml 4.02 的手册说明如下:

    使用流解析器语法的 OCaml 程序应使用 ocamlc 和 ocamlopt 的 -pp camlp4o 选项进行编译。对于交互式使用,运行 ocaml 并发出 #load "dynlink.cma";;命令,后跟 #load "camlp4o.cma";;命令。

    【讨论】:

    • 现代 ocaml 中最接近流式语法的东西是什么? (有没有一些自动转换程序?)
    • 最近在这里讨论过这个问题:discuss.ocaml.org/t/… 讨论中的一个建议是像这样的解析器组合库:github.com/murmour/mparser
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2018-05-22
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2018-10-07
    相关资源
    最近更新 更多