Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 =
   parse
     | ""          { match !global_var with
	                | Xlex -> xlex lexbuf
                        | Ylex -> ylex lexbuf
                   }

and xlex =
   parse 
      ....


and ylex =
   parse
      ....


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


Bye,

Hendrik

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners