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
1
vote
1 answer

ocamlmktop error: both define a module named Parser; both define a module named Lexer;

I am green man on ML, now study ocaml only hours to build my compiler with it. Lexer and the Parser is generated without any problem. But ther issue happen when the compiler code is build by ocamlmktop. The error message likes the following, but the…
1
vote
0 answers

Extract Token List from OCamllex lexbuf

I am writing a Python interpreter in OCaml using ocamllex, and in order to handle the indentation-based syntax, I want to tokenize the input using ocamllex iterate through the list of lexed tokens and insert INDENT and DEDENT tokens as needed for…
JAustin
  • 890
  • 10
  • 16
1
vote
1 answer

How do I preform 'lookahead' in an OCaml lexer / how do I rollback a lexeme?

Well, I'm writing my first parser, in OCaml, and I immediately somehow managed to make one with an infinite-loop. Of particular note, I'm trying to lex identifiers according to the rules of the Scheme specification (I have no idea what I'm doing,…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
1
vote
1 answer

Ocaml parser "unterminated action" error

I am new to OCaml and I am trying to create a Parser for a specific language using a parser generator - ocamllex, ocamlyacc. When i'm trying to compile my parser.mly file, I am getting the following error: Error (with mark at =) : File "parser.mly",…
denbuttigieg
  • 185
  • 1
  • 1
  • 12
1
vote
1 answer

OCAML Taking multiple arguments from stdin and operating on them one by one

I have written an interpreter using ocamllex and ocamlyacc, the lexer and the parser work correctly but currently they only parse the last .txt argument it receives as oppose to all of them in turn. For example, ./interpret one.txt two.txt three.txt…
1
vote
1 answer

Record tokens and their position to use them outside the front-end

I want to write a small beautifier for a specific language. In the beautifier, we will be able to indent one or several lines (ie, adding white-spaces on the left hand of each line); we will also be able to format the whole code (ie, alter…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
2 answers

Faithfully handle white-spacing in a pretty-printer

I am writing a front-end for a language (by ocamllex and ocamlyacc). So the frond-end can build a Abstract Syntax Tree (AST) from a program. Then we often write a pretty printer, which takes an AST and print a program. If later we just want to…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

Lexing and include directive with ocamllex

I'm making a compiler for a C-like language that has to support #include directive (only in the beginning of the file) A simple but inelegant approach would be to create a subprogram that finds every occurrence of the directive, and substitutes with…
Cris
  • 185
  • 1
  • 7
1
vote
4 answers

"Clang: error: no input files" in OS X

Following this solution, I use #include "...frontend/tokens.mll" in my lexer.mll, then I use cpp -P frontend/lexer.mll -o frontend/gen/lexer.mll to generate the complete mll file. This solution worked before under Ubuntu. Now, I try to do this in…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

OCamllex cannot import Core package (Go to Python compiler)

I am writing a Go to Python compiler with OCamllex and Menhir but my lexer is failing to import the Core package. Here is my lex.mll file: { (* Header *) open Core.Std open Lexing open Parser exception SyntaxError of string let…
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
1
vote
1 answer

wrong expression type on ocamlyacc

As part of a school project I have to recognize a .dot file and produce the corresponding parse tree. To accomplish this, I have to use ocamllex and ocamlyacc which whom I've difficulties... This is my ocaml .mli types file : type id = string and…
user3450044
  • 117
  • 9
1
vote
2 answers

In an OCaml scanner, why does the regex for negative integers not work?

I have the following ocamllex code: let flt = ['-' '+']?['0'-'9']+ ['.'] ['0'-'9']+ rule token = parse [' ' '\t' '\r' '\n'] { token lexbuf } (* Whitespace *) | ['0'-'9']+ as lxm { INTEGER(int_of_string lxm) } | flt as lxm {…
P.C.
  • 651
  • 13
  • 30
1
vote
2 answers

How can I convert a StringMap to a List in OCaml?

I am very new to OCaml and am trying to convert a StringMap to a List in OCaml. The map was generated from a list previously. let map = List.fold_left( ) StringMap.empty in StringMap.fold(fun w c newlist…
P.C.
  • 651
  • 13
  • 30
1
vote
2 answers

OCaml interpreter: evaluate a function inside a function

I'm trying to write an interpreter in OCaml and I have a problem here. In my program, I want to call a function like this, for example: print (get_line 4) // print: print to stdout, get_line: get a specific line in a file How can I do that? The…
Trung Bún
  • 1,117
  • 5
  • 22
  • 47
1
vote
1 answer

OCamllex: regular expression

Currently i'm trying to write an interpretter in Ocaml and this is my lexer.mll: { open Parser exception Eof } rule main = parse [ ' ' '\t' ] { main lexbuf } | [ '\n' ] { EOL } | ['0'-'9']+ as lxm {…
Trung Bún
  • 1,117
  • 5
  • 22
  • 47