CVS: sml-dist/src/lexgen/src/BackEnds/Match match.sml,NONE,1.1

Matthias Blume <[email protected]>
Newsgroups gmane.comp.lang.sml.smlnj.commits
Message-ID <[email protected]>
Update of /cvsroot/smlnj/sml-dist/src/lexgen/src/BackEnds/Match
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30700/src/lexgen/src/BackEnds/Match

Added Files:
	match.sml 
Log Message:
ml-flex -> lexgen

--- NEW FILE: match.sml ---
(* match.sml
 *
 * COPYRIGHT (c) 2005 
 * John Reppy (http://www.cs.uchicago.edu/~jhr)
 * Aaron Turon ([email protected])
 * All rights reserved.
 *
 * A simple match "backend" that runs the produced state machine directly
 * on stdin.  Treats end of line as end of input.  Note that a match only
 * occurs if the machine is in an accepting state after consuming the 
 * complete input; in particular, the input is meant to represent a single
 * token, and the machine does not restart until the end of input.
 *)

structure Match : OUTPUT = 
  struct

    structure SIS = RegExp.SymSet
    structure LO = LexOutputSpec

    fun match (LO.State{id, label, final, next}, []) = final
      | match (LO.State{id, label, final, next}, sym::r) = let
	  fun goto [] = []
	    | goto ((syms, s)::r') = 
	        if SIS.member (syms, sym) 
		then match(s, r) 
		else goto r'
	  in
	    goto (!next)
	  end

    fun matchLoop states = (case TextIO.inputLine (TextIO.stdIn)
	  of NONE => ()
	   | SOME "\n" => ()
	   | SOME s => let
	       val chars = List.rev (List.tl (List.rev (String.explode s)))
	       val syms = List.map 
			    (Word32.fromInt o Char.ord) 
			    chars
	       val q0 as LO.State {label, ...} = List.hd states
	       val _ = case match (q0, syms)
			of [] => print "-- No match --\n"
			 | i::_ => 
			     (print "-- Match: ";
			      print (RegExp.toString (Vector.sub (label, i)));
			      print " --\n")
	       in
	       (* continue I/O loop *)
	         matchLoop states
	       end
	 (* end case *))

    fun output (LO.Spec {dfa, ...}, _) = 
	  matchLoop(dfa)

  end



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.