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
[Caml-list] Calling ocamlyacc from ocamlyacc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-09-03 (10:28)
From: Hendrik Tews <tews@t...>
Subject: Re: [Caml-list] Calling ocamlyacc from ocamlyacc
katre writes:
   Right, but is there a way, in a ocamlyacc action, to switch which lexer
   rule you're using?  That seems to be the main part I am missing.  Or if
   I could access the lexbuf directly, I could also use that.
In the lexer you can do 

rule lexer =
     | ""          { match !global_var with
	                | Xlex -> xlex lexbuf
                        | Ylex -> ylex lexbuf

and xlex =

and ylex =

You can set the global_var from the actions in the grammar. 

However, there is probably a better solution: First note that
ocamlyacc generated functions expect a (Lexing.lexbuf -> token)
function. So you can write your own master lexer:

let lexer lexbuf = match !global_var with
  | Xlex -> Lexer.xlex lexbuf
  | Ylex -> ....

with a bit of hacking you can also combine ocamllex lexers with
other lexers.

In both approaches the problem is the lookahead token: In some
cases yacc fetches the next token and decides on that token
whether to shift or reduce. If the action taken on reduce changes
the lexer then have used the wrong lexer for the next token.

You can examine the grammar.output file and the OCAMLRUNPARAM=p
trace to find out if ocamlyacc needs the lookahead token for a
given rule. (I can give examples on that if you are interested.)



To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: