【发布时间】: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
;;
【问题讨论】: