Questions tagged [ocamllex]

ocamllex is a Lexer generator for OCaml

ocamllex is the lexer generator for OCaml. It is based on lex (now better known as flex).

It is used within the OCaml compiler, and thus, it is installed by the official OCaml distribution. ocamllex is powerful and nicely integrated within OCaml.

For simple uses, a simple lexer generator is also available in the Genlex module of the standard library, and allows to lex files with a syntax similar to OCaml ( (..) for comments, same escape sequences in strings, etc.).

References:

See also:

94 questions
2
votes
1 answer

Using ocamlyacc with sedlex

I am trying to figure out how to use ocamlyacc with sedlex. lexer.ml (using sedlex): let rec lex (lexbuf: Sedlexing.lexbuf) = match%sedlex lexbuf with | white_space -> lex lexbuf (* ... other lexing rules ... *) | _ -> failwith…
Flux
  • 9,805
  • 5
  • 46
  • 92
2
votes
1 answer

OCaml warning 31, compiler-libs, and ppx

I'm porting my application from OCaml 4.02.3 to 4.03.0. Say you have the following in lexer.ml: type t = T [@@deriving sexp] let () = sexp_of_t |> ignore; print_endline "hai" I'm trying to run it as following: ocamlbuild -use-ocamlfind -pkg…
Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
2
votes
1 answer

Ocamllex refill-handler example

In Chapter 12 Lexer and parser generators, I fail to compile the following example : { type token = EOL | INT of int | PLUS module Make (M : sig type 'a t val return: 'a -> 'a t val bind: 'a t -> ('a…
Pierre G.
  • 4,346
  • 1
  • 12
  • 25
2
votes
3 answers

Ocaml if-then-else Syntax Error

Why is this Ocaml statement giving me a syntax error? let a = 0;; if a = 0 then let b = 0;; Do if then else statements always have to return a value? EDIT: Here is the code I am struggling with. I want to apply this function over a list with the…
andrewhunter
  • 183
  • 9
2
votes
1 answer

Print tokenization of a string

I'm currently working on a programming language as a hobby. It would make lexing errors massively easier to debug if it was possible to have ocamllex print out the tokens it matches as it finds them, I occasionally just add print statements to my…
Joshua Snider
  • 705
  • 1
  • 8
  • 34
2
votes
1 answer

expand ocamllex for a bigger lexicon

is there a way to get ocammlex work with more keywords? i've written an interpreter and parser for the german language, which "compiles" german text into latex-pictures for sake of language analysis. it works really fine and is really new in the…
2
votes
1 answer

Add source code position information to a frontend

I am writing a static analyser including a frontend for programs in a specific language. The front-end succeeds to generate an AST from a program, on which the analyser works well: it either proves the program correct (for some specific properties),…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
2
votes
2 answers

External definitions for ocamllex regular expressions

I have implemented the usual combination of lexer/parser/pretty-printer for reading-in/printing a type in my code. I find there is redundancy among the lexer and the pretty-printer when it comes to plain-string regular expressions, usually employed…
Nikos
  • 219
  • 1
  • 8
1
vote
2 answers

How to properly tokenize over whitespaces with ocamllex/Menhir?

I am writing a simple shell in OCaml. My grammar looks a little like this at the moment /* program: command* command: WORD WORD* redirection* redirection: NUMBER? > FILENAME | < FILENAME */ Simple enough. I have a lexer file that looks like…
Lou Falkes
  • 73
  • 6
1
vote
1 answer

How to override a module method in Ocaml

Say I have a module A which overrides some methods from module B. I would like module A to prevent such overriding in module A by overriding B or in some other way. I need this to get line numbers from YoJSON. I tried using other parsers but they…
1
vote
1 answer

Any character other than

I have defined a token for ocamllex let non_line_termination_character = [^ '\x0D' '\x0A'], which represents any character other than '\x0D' and '\x0A'. Now, I would like to translate it to sedlex. Does anyone know how to translate it correctly?…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

Lexing strings in ocamllex

I have been having some trouble trying to find a good example to go off of for being able to handle strings in ocamllex. I found the desktop calculator example to be somewhat useful but haven't really found a way to implement it in a similar fashion…
C H
  • 25
  • 5
1
vote
1 answer

Some special characters are allowed only if they are preceded by an escape character

I want to construct a regular expression (in the style of lex, with a more OCaml-like syntax) for a class of strings, where 4 characters [, ], #, ' are allowed only if they are preceded by an escape character '. Here are some valid…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

Transition table overflow, automaton is too big

I want to add the support of structured references with Excel tables to my lexer & parser of Excel formulas. I added the following regular expressions to lexer_structref.mll: let lex_table_name = "DeptSales" let lex_column_header = "Sales…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

How to generate a proper makefile for an Ocaml project

I am learning how to compilers work. I read a tutorial on how to use Ocamllex and Ocamlyacc to read an input from a source code, generate the tokens and generate a syntatic tree in order to compute the execution of the program later. I had to…
Sev
  • 195
  • 3
  • 10