Re: ocamlyacc and error messages

From: Jean-Christophe Filliatre (
Date: Tue Sep 07 1999 - 09:00:02 MET DST

From: Jean-Christophe Filliatre <>
Date: Tue, 7 Sep 1999 09:00:02 +0200 (MEST)
To: "David Mentr'e" <>
Subject: Re: ocamlyacc and error messages
In-Reply-To: <>

> Comment donner le numéro de ligne et le point d'échec dans une grammaire
> d'un parser généré par ocamlyacc ?
> What is the simplest way to report an error in a parser generated by
> ocamlyacc ?

The module Parsing of the ocaml standard library provides two functions

val symbol_start : unit -> int
val symbol_end : unit -> int

to get the start and end positions of the left-hand side of the
grammar rule that matched (with a ocamlyacc grammar). Those positions
are characters count from the beginning of the file. You can store
those informations in your syntax tree, together with the name of the
rule if you want.

Then, if you want to indicate the corresponding line in the file when
an error occurs, one possible solution is to re-scan the file from the
beginning, counting newline characters until the desired position is
found. It is the solution used by the ocaml parser. Have a look in
ocaml sources (directory parsing/) for more informations.

Another solution is to count the lines in your lexer, in a reference,
and to store that information in your syntax tree when building it.

Jean-Christophe FILLIATRE

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:25 MET