Hi Joel, why are you using stream parsers instead of Camlp4 grammars ? This: > let rec parse_primary = parser > > | [< 'INT n >] -> Int n > | [< 'FLOAT n >] -> Float n > | [< 'STRING n >] -> Str n > | [< 'TRUE >] -> Bool true > | [< 'FALSE >] -> Bool false > > | [< >] -> raise (Stream.Error "unknown token when expecting an expression.") could be written as: expression: [ [ (i, _) = INT -> Int i | (s, _) = STRING -> Str s ... ] ]; Note that Camlp4 will automatically raise an exception if the input cannot be parsed with the grammar given. Also if you have input that is syntactically correct but is not semantically correct, and you want to raise an exception with the error location during parsing, you might want to use Loc.raise as follows: expression: [ [ e1 = SELF; "/"; e2 = SELF -> if e2 = Int 0 then Loc.raise _loc (Failure "division by zero") else BinaryOp (e1, Div, e2) ] ]; By the way, do you need you own tailor-made lexer? Camlp4 provides one that might satisfy your needs. Otherwise, you can always define your own lexer (I had to do that for the project I'm working on, see file attached). Your parser would then look like (* functor application *) module Camlp4Loc = Camlp4.Struct.Loc module Lexer = Cal_lexer.Make(Camlp4Loc) module Gram = Camlp4.Struct.Grammar.Static.Make(Lexer) (* exposes EOI and other stuff *) open Lexer (* rule definition *) let rule = Gram.Entry.mk "rule" (* grammar definition *) EXTEND Gram rule: [ [ ... ] ]; END (* to parse a file *) Gram.parse rule (Loc.mk file) (Stream.of_channel ch) This should be compiled with camlp4of. I hope this helps you with what you'd like to do, Cheers, Matthieu ----- Message d'origine ---- > De : Joel Reymont > À : O'Caml Mailing List > Envoyé le : Samedi, 7 Mars 2009, 23h52mn 52s > Objet : [Caml-list] Re: camlp4 stream parser syntax > > > Where can I read up on the syntax of the following in a camlp4 stream parser? > > > > | [<' INT n >] -> Int n > > > > For example, where are [< ... >] described and why is the ' needed in between? > > > To be more precise, I'm using camlp4 to parse a language into a non-OCaml AST. > > I'm trying to figure out the meaning of [<, >], [[ and ]] > > My ocamllex lexer is wrapped to make it look like a stream lexer (below) and I'm > returning a tuple of (tok, loc) because I don't see another way of making token > location available to the parser. > > Still, I'm how to integrate the reporting of error location into ?? in something > like this > > | [< 'Token.Kwd '('; e=parse_expr; 'Token.Kwd ')' ?? "expected ')'" >] -> e > > Would someone kindly shed light on this? > > Thanks in advance, Joel > > P.S. ocamllex wrapper to return a' Stream.t > > { > let from_lexbuf tab lb = > let next _ = > let tok = token tab lb in > let loc = Loc.of_lexbuf lb in > Some (tok, loc) > in Stream.from next > > let setup_loc lb loc = > let start_pos = Loc.start_pos loc in > lb.lex_abs_pos <- start_pos.pos_cnum; > lb.lex_curr_p <- start_pos > > let from_string loc tab str = > let lb = Lexing.from_string str in > setup_loc lb loc; > from_lexbuf tab lb > > } > > --- > http://tinyco.de > Mac, C++, OCaml > > > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs