ocaml流输入类型语法

时间:2017-10-23 02:26:58

标签: ocaml lexer

我正在尝试在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
;;

1 个答案:

答案 0 :(得分:1)

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

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

然而,camlp4本身现在已被弃用,转而支持ppx。由于您使用的是旧版本的OCaml(4.02是从2015年开始),因此可以使用camlp4。

OCaml 4.02的手册说明如下:

  

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

相关问题