Reputation: 77
I'm trying to build a lexer in Ocaml, version 4.02.5, I cannot seem to get a stream type to work and I can hardly find anything useful for Ocaml. Most everything that I read is overly complicated with no examples on taking char inputs from a file and lexing them.
The error I'm receiving is in my first stream declaration, the specific syntax location is on the '[<' part.
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
;;
Upvotes: 0
Views: 266
Reputation: 66818
Direct support for stream syntax was removed from OCaml in version 3.03, which was released at the end of 2001.
Since then, support for these syntax extensions has been provided by camlp4.
However, camlp4 itself has now been deprecated in favor of ppx. Since you are using an old version of OCaml (4.02 is from 2015), camlp4 may be available.
The manual for OCaml 4.02 says the following:
OCaml programs that use the stream parser syntax should be compiled with the -pp camlp4o option to ocamlc and ocamlopt. For interactive use, run ocaml and issue the #load "dynlink.cma";; command, followed by the #load "camlp4o.cma";; command.
Upvotes: 2