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

Error Eof Ocaml

I'm doing a compiler for class in Ocaml. I need to read a file with commands or expressions like "1" and then it returns Int 1. The same code worked with the whole class except for me and my friend. Everyone is using the same ocaml version and…
MDayrell
  • 63
  • 1
  • 8
1
vote
1 answer

Retrieve a part of parsing by making separate .mly and .mll

I am writing a front-end to parse a set of txt files, each file contains a set of procedures, for instance one txt file looks like: Sub procedure1 ... End Sub Sub procedure2 ... End Sub ... syntax.ml contains: type ev = procedure_declaration…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

A table of 2 directions (string <-> token) in parsing

I have defined a hash table keyword_table to store all the keywords of my language. Here is part of the code: (* parser.mly *) %token CALL CASE CLOSE CONST ... reserved_identifier: | CALL { "Call" } | CASE { "Case" } | CLOSE { "Close" } | CONST {…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

Ocamllex Syntactical issue

{ } rule translate = parse | "current_directory" { print_string (Sys.getcwd ()) } | _ as c { print_char c } | eof { exit 0 } { let main () = let lexbuf = Lexing.from_channel stdin in while true do translate lexbuf done let _ = Printexc.print main…
alpha42
  • 55
  • 6
1
vote
1 answer

token meaning dependent on context

I have a weird string syntax where the meaning of a delimiter depends on context. In the following sample input: ( (foo) (bar) ) the result is a list of two strings ["foo"; "bar"]. The outer pair of parenthesis enters list mode. Then, the next pair…
Philipp Gesang
  • 496
  • 1
  • 6
  • 16
0
votes
0 answers

Does ocamllex support non-greedy patterns?

Would be nice to be able to match everything inside a comment until the end-token "*/". Possible? Without creating a string buffer manually. Example: | "/**" (_* as s) "*/" { DOCBLOCK_AS_STR s }
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
0
votes
1 answer

Not able to compile MLY file using Dune

I am writing my first Ocamllex and Ocamlyacc program by following this tutorial My dune file looks like (executable (public_name Calculator) (name main)) (ocamllex lexer) (ocamlyacc parser) My lexer.mll file is { open Parser } rule read =…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
0
votes
1 answer

How to scan with ocamllex until end of file

I am trying to implement a parser that read regular expression. It ask the user to enter a valid input of string/integers/float. If is valid and the user press ctrl^d, then print the number. Otherwise shows an error. But the problem in the following…
0
votes
1 answer

What are these comment-like lines in ocamllex generated .ml?

When I generate an .ml file with ocamllex it has a bunch of lines like: # 21 "lib/myproj/example.ml" These look like comments, except AFAIK comments in OCaml are like (* this is a comment *) VS Code doesn't seem to treat them as comments…
Anentropic
  • 32,188
  • 12
  • 99
  • 147
0
votes
0 answers

Is there precedence to listing out tokens in a parser.mly file?

Like if I have some tokens listed at the beginning of my parser like so %{ open Ast %} %token SEMI LPAREN RPAREN LBRACE RBRACE COMMA PLUS MINUS TIMES DIVIDE ASSIGN %token NOT EQ NEQ LT LEQ GT GEQ AND OR %token RETURN IF ELSE FOR WHILE INT BOOL…
Shisui
  • 1,051
  • 1
  • 8
  • 23
0
votes
1 answer

Construction of a lexer with ocamllex without a parser module

I want to build a lexer with ocamllex before the construction of a parser with menhir. I have wrote the .mll file. The following command gave me this message: > ocamllex lexer.mll 71 states, 2138 transitions, table size 8978 bytes Then I typed the…
0
votes
2 answers

Identify what slows down the compilation of a lexer

I have a lexer and parser, built with sedlex and menhir in OCaml, to parse spreadsheet formulas. The following part of the lexer defines regular expressions for the path+workbook+worksheet part before a reference. For instance,…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
0
votes
1 answer

Why this syntax error? No given reason from the shell

I've just finished writing this piece of pattern matching (to be attached below). No matter how I tried fixing it up, there is still "Error: Syntax error" when I tried to compile it. The piece of code is here: let rec stmt (s:Ast.stmt) : X86.inst…
ZWu
  • 1
0
votes
1 answer

OCamllex syntax error

When defining some identifiers in the definition section of my lexer (as described at here), i'm trying to write something of the form: let op_char = ['+' '-' '*' '/'] let id_char = [^ ' ' '\r' '\n' '\t' op_char] To define id_char as every…
rochem
  • 303
  • 1
  • 5
0
votes
1 answer

Set a rule based on the value of a global variable

In my lexer & parser by ocamllex and ocamlyacc, I have a .mly as follows: %{ open Params open Syntax %} main: | expr EOF { $1 } expr: | INTEGER { EE_integer $1 } | LBRACKET expr_separators RBRACKET { EE_brackets (List.rev $2)…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292