From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 74FFB7FA5E for ; Mon, 24 Apr 2017 22:30:36 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.37,246,1488841200"; d="scan'208";a="270299757" Received: from bou78-2-82-240-46-163.fbx.proxad.net (HELO MP-41019.local) ([82.240.46.163]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 24 Apr 2017 22:30:36 +0200 To: caml-list@inria.fr References: <2183504.2kfSNf9Hln@twitter> From: =?UTF-8?Q?Fran=c3=a7ois_Pottier?= Message-ID: <58FE606B.5000400@inria.fr> Date: Mon, 24 Apr 2017 22:30:35 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <2183504.2kfSNf9Hln@twitter> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] char/line-numbers in error-messages from data-structures bulit via ocamllex? Dear Chet, With ocamlyacc and menhir, just keep your type "token" unmodified -- no need to annotate tokens with positions. Then, if you are using ocamlyacc, the functions in the module Parsing can be used to extract position information; if you are using menhir, the position keywords $startpos, $endpos, etc. can be used for this purpose. (See Menhir's documentation for details.) You can then arrange for your semantic actions to decorate your AST with position information. In Menhir, you can define a parameterized nonterminal symbol to alleviate the pain of recording positions. For instance: located(X): x = X { (x, $startpos, $endpos) } Thus, if the nonterminal symbol X has a semantic value of type 'a, then the nonterminal symbol located(X) recognizes the same strings as X, but has a semantic value of type 'a * Lexing.position * Lexing.position. If your nonterminal symbols are expression, instruction, etc., then you can use located(expr), located(instruction), etc., to automatically obtain decorated ASTs. I hope this helps, -- François Pottier francois.pottier@inria.fr http://gallium.inria.fr/~fpottier/