Questions tagged [menhir]

Menhir is a parser generator for OCaml

Menhir is a LR(1) parser generator for the Objective Caml programming language. That is, Menhir compiles LR(1) grammar specifications down to Objective Caml code. It is mostly compatible with ocamlyacc, and can be used with ocamllex.

References:

See also:

76 questions
1
vote
1 answer

Generate a parser enabling incremental API and inspection API

I have a big project built by menhir and traditional makefile. First, I wanted to add a mechanism of error handling like this project to my project. By following the dune of the sample project, I managed to generate .mly, .mli, .ml, .cmi and .cmo of…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
1 answer

Add a module by makefile or manually

I have a big project built with traditional makefile. I would like to add a mechanism of error handling like this project to my project. In the sample project, a module UnitActionsParser is generated by the following rules of dune: ;; The following…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
1
vote
2 answers

In OCaml Menhir, how to write a parser for C++/Rust/Java-style generics

In C++, a famous parsing ambiguity happens with code like x a; Is it if T is a type, it is what it looks like (a declaration of a variable a of type x, otherwise it is (x < T) > a (<> are comparison operators, not angle brackets). In fact,…
andrepd
  • 587
  • 6
  • 20
1
vote
1 answer

How do I extract useful information from the payload of a GADT / existential type?

I'm trying to use Menhir's incremental parsing API and introspection APIs in a generated parser. I want to, say, determine the semantic value associated with a particular LR(1) stack entry; i.e. a token that's been previously consumed by the…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
1
vote
1 answer

Ambiguity when Parsing Preceding List

While writing parser code in Menhir, I keep coming across this design pattern that is becoming very frustrating. I'm trying to build a parser that accepts either "a*ba" or "bb". To do this, I'm using the following syntax (note that A* is the same…
Checkmate
  • 1,074
  • 9
  • 16
1
vote
1 answer

Why do I always have a shift/reduce conflict with my EOF / linebreak nonterminals?

So, I'm still pretty junior when it comes to putting together parsing grammars. I need help dissecting conflicts reported by Menhir when I have shift-reduce conflicts. Take this little grammar as an example: (* {2 Tokens } *) %token EOF %token COLON…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
1
vote
1 answer

Parser: Distinguish parenthesis and function declaration in JavaScript like grammar

I'm working on a compiler using OCaml and Menhir as parser and lexer. When I'm writing a JavaScript like Grammar, with (a, b) => a + b like lambda function definition and also arithmetics (a + b) * c with parenthesis prioritizing sub-expressions, I…
Liby Lee
  • 13
  • 2
1
vote
1 answer

Separating a list by eol when parsing ocaml

I have defined some statements followed by a list of expressions in my compiler. I am using Menhir for the parsing. Typically when lexing EOL it does this: | eol { incr_linenum lexbuf; read lexbuf } However, I would like to be able to parse…
pleasehalp
  • 136
  • 7
1
vote
1 answer

ParserErr generates "index out of bounds exception"

I am creating a compiler and am trying to extract line information from the parser. I wish to attach this to the AST node as metadata so that any error at a later point can be reported easily. I was successfully able to extract the line information…
pleasehalp
  • 136
  • 7
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

How to parse list of expression using menhir?

Here are my current lexer and parser for Andrew Appel's Tiger language (ocaml). I'm currently trying to support mutual recursive functions, but the following parser code doesn't work: decs : | l = list(dec) { l } dec : | t =…
Vinz
  • 5,997
  • 1
  • 31
  • 52
1
vote
2 answers

Semantic actions on Menhir

I am building a small compiler for an assignment language. Consider the following rule: var_block : | LPAREN var_decl+ RPAREN { var_scope := var_scope + 1 }; Is the semantic action triggered when var_block is first recognized or is it triggered…
1
vote
1 answer

How does a LR(1) parser handles empty rules?

I have worked with a few parsers (Yacc, Bison and Menhir). If I remember correctly, all of them allow for a rule to be empty. Here is an example of what I mean using Menhir, it is the one I have used the most. some_list: | {[]} |…
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
1
vote
1 answer

menhir - associativity rules for reducing sequences of expressions

writing a parser for lambda expressions, data expr = Symbol of string | Lambda of string * expr | App of expr * expr When writing the .mly file how can I express the idea that a sequence of expressions e1 e2 e3 e4 should be parsed as App ((App…
beoliver
  • 5,579
  • 5
  • 36
  • 72
1
vote
1 answer

Get the input string that raises parsing error inside the parser

I have a frontend written in menhir which tries to parse an expression: from a string to an expression AST. The entry point of the frontend Parser_e.main is called in several different places in my OCaml code. So I would like to be able to catch…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292