caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Ocamllex question
@ 2005-10-23 18:02 Matt Gushee
  2005-10-23 20:58 ` [Caml-list] " Michael Wohlwend
  2005-10-23 21:12 ` Another problem (was Re: [Caml-list] Ocamllex question) Matt Gushee
  0 siblings, 2 replies; 6+ messages in thread
From: Matt Gushee @ 2005-10-23 18:02 UTC (permalink / raw)
  To: caml-list

Hello, people--

In a lexer definition with two or more entry points, is there a way to
emit a lexeme and pass control to another entrypoint in one action?

The specific problem I am trying to deal with is a configuration file
format that includes comments denoted with an initial '#' character. I
would like to support the typical usage of '#', where a comment may
begin either at the beginning of the line, or after a declaration that I
want to capture, and in either case it extends to the end of the line.

So in general, anything after '#' up to the end of a line should be
ignored, which I think requires a separate 'comment' entrypoint. At the
end of the line, control returns to the main entry point. So my first
cut looks like this:

  rule dict = parse
      [' ']                             { dict lexbuf }
    | '#'                               { comment lexbuf }
    | word                              { WORD (Lexing.lexeme lexbuf) }
    | ':'                               { COLON }
    | '{'                               { DS }
    | '}'                               { DE }
    | ',' | '\n'                        { SEP }
    | eof                               { EOF }
  and comment = parse
      [ ^ '\n' ]                        { comment lexbuf }
    | '\n'                              { dict lexbuf }

So far so good. BUT, for the sake of simplicity (for users, not for me
;-)), my syntax has line endings as separators, and in order to support
comments following non-comments on the same line, a line ending after a
comment should be interpreted as a separator. So what I want to do is
something like:

  and comment = parse
      [ ^ '\n' ]                        { comment lexbuf }
    | '\n'                              { SEP; dict lexbuf }

But that doesn't work, of course. Maybe the solution is to push SEP back
onto the head of the buffer, but I don't see a way to do that.

Or would it be better to simply tag the comment text with, say, a
COMMENT symbol and pass it through to the parser?

--
Matt Gushee
Englewood, CO, USA


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

* Re: [Caml-list] Ocamllex question
  2005-10-23 18:02 Ocamllex question Matt Gushee
@ 2005-10-23 20:58 ` Michael Wohlwend
  2005-10-23 21:12 ` Another problem (was Re: [Caml-list] Ocamllex question) Matt Gushee
  1 sibling, 0 replies; 6+ messages in thread
From: Michael Wohlwend @ 2005-10-23 20:58 UTC (permalink / raw)
  To: caml-list

Hi,

On Sunday 23 October 2005 20:02, Matt Gushee wrote:
>
>   and comment = parse
>       [ ^ '\n' ]                        { comment lexbuf }
>
>     | '\n'                              { SEP; dict lexbuf }

I'm not quite sure whether I understood your problem, but... doesn' it help to 
just return a SEP after a comment:

rule dict = parse
 ...
 | ',' | '\n'   { SEP }
 | '#' [^ '\n']* '\n'  { SEP }
 ...


 Michael




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

* Another problem (was Re: [Caml-list] Ocamllex question)
  2005-10-23 18:02 Ocamllex question Matt Gushee
  2005-10-23 20:58 ` [Caml-list] " Michael Wohlwend
@ 2005-10-23 21:12 ` Matt Gushee
  2005-10-23 21:37   ` Michael Wohlwend
  1 sibling, 1 reply; 6+ messages in thread
From: Matt Gushee @ 2005-10-23 21:12 UTC (permalink / raw)
  To: caml-list

While we're on the subject of Ocamllex, there's another issue I'm
wondering about. My lexer needs to handle quoted strings, something like:

  | '"' [^ '"'] * as word '"'     { WORD word }

Not essential, but it would be nice to allow escaped quotes within such
strings:

  "The quick brown fox jumped over the \"lazy\" dog."

I've haven't actually tried to implement this yet, but thinking about it
it seems like it would make the lexer hugely more complex. Can anyone
suggest a reasonably simple way to deal with escape sequences?

--
Matt Gushee
Englewood, CO, USA


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

* Re: Another problem (was Re: [Caml-list] Ocamllex question)
  2005-10-23 21:12 ` Another problem (was Re: [Caml-list] Ocamllex question) Matt Gushee
@ 2005-10-23 21:37   ` Michael Wohlwend
  2005-10-24 19:50     ` Matt Gushee
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Wohlwend @ 2005-10-23 21:37 UTC (permalink / raw)
  To: caml-list

On Sunday 23 October 2005 23:12, Matt Gushee wrote:
> While we're on the subject of Ocamllex, there's another issue I'm
>
> wondering about. My lexer needs to handle quoted strings, something like:
>   | '"' [^ '"'] * as word '"'     { WORD word }
>
> Not essential, but it would be nice to allow escaped quotes within such
> strings:
>
>   "The quick brown fox jumped over the \"lazy\" dog."
>
> I've haven't actually tried to implement this yet, but thinking about it
> it seems like it would make the lexer hugely more complex. Can anyone
> suggest a reasonably simple way to deal with escape sequences?

you write a extra lexer-rule which gets called when a " is seen; in this rule 
you read single chars and append them to a string buffer. If you see a 
backslash you handle the next character special; example:

let char_for_backslash = function   
    | 'a' -> '\007'
    | 'v' -> '\011'
    | 'f' -> '\012'
    | 'n' -> '\n'
    | 't' -> '\t'
    | 'b' -> '\b'
    | 'r' -> '\r'
    | c   -> c

let bs_escapes = [ '\032' - '\255' ]

let string_buff = Buffer.create 256
let reset_string_buffer () = Buffer.clear string_buff  
let store_string_char c = Buffer.add_char string_buff c
let store_string s = Buffer.add_string string_buff s
let get_stored_string () = Buffer.contents string_buff

rule dict = parser
  | ...
   | [ '"'  ] as d 
        {
         reset_string_buffer(); 
         scan_str  lexbuf;
         let s = get_stored_string() in
         (* Printf.printf " String (%c) read:%s " d s; *)
         STRING(s) 
        }

and scan_str  = parse 
    | [ '"' ]  { () }
    | '\\' (bs_escapes as c)  
        { store_string_char (char_for_backslash c); scan_str lexbuf }
    | allowed_string_char as c {store_string_char c }
    | eof  { raise( Lexical_error("unterminated string") ) }


works well,

 Michael


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

* Re: Another problem (was Re: [Caml-list] Ocamllex question)
  2005-10-23 21:37   ` Michael Wohlwend
@ 2005-10-24 19:50     ` Matt Gushee
  2005-10-24 20:18       ` Michael Wohlwend
  0 siblings, 1 reply; 6+ messages in thread
From: Matt Gushee @ 2005-10-24 19:50 UTC (permalink / raw)
  To: caml-list

Michael Wohlwend wrote:

>>Not essential, but it would be nice to allow escaped quotes within such
>>strings:

> you write a extra lexer-rule which gets called when a " is seen; in this rule 
> you read single chars and append them to a string buffer. If you see a 
> backslash you handle the next character special; example:
> ....

Thank you! That was just the sort of example I needed ... and I
understand ocamllex a little better now.

Maybe you should post something like this on the Web. There's not much
in the way of practical examples for ocamllex.

--
Matt Gushee
Englewood, CO, USA


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

* Re: Another problem (was Re: [Caml-list] Ocamllex question)
  2005-10-24 19:50     ` Matt Gushee
@ 2005-10-24 20:18       ` Michael Wohlwend
  0 siblings, 0 replies; 6+ messages in thread
From: Michael Wohlwend @ 2005-10-24 20:18 UTC (permalink / raw)
  To: caml-list

On Monday 24 October 2005 21:50, Matt Gushee wrote:
> Michael Wohlwend wrote:
> Maybe you should post something like this on the Web. There's not much
> in the way of practical examples for ocamllex.

in some way this exists allready :-) I didn't invent all of it myself.  I 
looked  how the ocaml lexer (lexer.mll in dir lex of the source-tree) does 
this and it was quite easy to understand the relevant parts.


cheers
 Michael


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

end of thread, other threads:[~2005-10-24 20:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-23 18:02 Ocamllex question Matt Gushee
2005-10-23 20:58 ` [Caml-list] " Michael Wohlwend
2005-10-23 21:12 ` Another problem (was Re: [Caml-list] Ocamllex question) Matt Gushee
2005-10-23 21:37   ` Michael Wohlwend
2005-10-24 19:50     ` Matt Gushee
2005-10-24 20:18       ` Michael Wohlwend

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).