Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
camlp4 stream parser syntax
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-03-08 (15:07)
From: Matthieu Wipliez <mwipliez@y...>
Subject: Re : Re : [Caml-list] Re: camlp4 stream parser syntax

> And this is the part that I object to. I have quite a number of keywords and I 
> don't want to have a bunch of if statements or have a hash table mapping 
> lowercase to camel case. This would mean having to track the parser (camel case) 
> version in two places: the lexer and the parser.

Ahhh ok, I (finally) got it!
I believe there is a (partially acceptable) solution, if you are willing to accept having all your keywords in lower-case in the grammar (not in the lexer), ie you match against "buyorsell", "sellshort" etc.

Then you can change the functions match_keyword and keyword_conversion as follows:

let keyword_conversion tok is_kwd =
  match tok with
    SYMBOL s | IDENT s when is_kwd (String.lowercase s) -> KEYWORD s
  | _ -> tok

This will pass lower-cased identifiers to "is_kwd", so "BuyOrSell" becomes a valid keyword.

let match_keyword kwd = function
  KEYWORD kwd' when kwd = String.lowercase kwd' -> true
| _ -> false

Here kwd is the keyword from the grammar ("buyorsell") and kwd' is the content of the keyword produced by the lexer ("BuyOrSell"), and they match.