caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] char/line-numbers in error-messages from data-structures bulit via ocamllex?
@ 2017-04-24 20:18 Chet Murthy
  2017-04-24 20:30 ` François Pottier
  0 siblings, 1 reply; 2+ messages in thread
From: Chet Murthy @ 2017-04-24 20:18 UTC (permalink / raw)
  To: caml-list; +Cc: chetsky

Ages (decades) ago, I recall (perhaps incorrectly) that there was a
somewhat well-known way of using ocamllex and {ocamlyacc, PP streams}
together, in order to decorate a parse-tree with line-number
information -- so that later (e.g. type-checking) passes could emit
that line-number information as part of error-messages.

As I rack my brain trying to imagine what that method might be,
several approaches come to mind:

(1) with PP streams, I could define the token-type to be a tuple of
the real token-type and a lexing position, e.g.

type token = real_token * Lexing.position

(2) with camlyacc, I'd have to stuff the position into every branch of
the algebraic datatype, e.g.

type token =
| LIT of Lexing.position * literal
| ID of Lexing.position * identifier
| OP of Lexing.position * operator
...

Both of these seem workable, but I don't remember either of them as
being "the" way to do it, back in the day.

Does anybody have any memory of this "Echte" way of doing it?

Thanks,
--chet--


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] char/line-numbers in error-messages from data-structures bulit via ocamllex?
  2017-04-24 20:18 [Caml-list] char/line-numbers in error-messages from data-structures bulit via ocamllex? Chet Murthy
@ 2017-04-24 20:30 ` François Pottier
  0 siblings, 0 replies; 2+ messages in thread
From: François Pottier @ 2017-04-24 20:30 UTC (permalink / raw)
  To: caml-list


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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-04-24 20:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-24 20:18 [Caml-list] char/line-numbers in error-messages from data-structures bulit via ocamllex? Chet Murthy
2017-04-24 20:30 ` François Pottier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).