~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to examples/demonstr/lexuniv.ml

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
(***********************************************************************)
2
 
(*                                                                     *)
3
 
(*                           Objective Caml                            *)
4
 
(*                                                                     *)
5
 
(*               Pierre Weis, projet Cristal, INRIA Rocquencourt       *)
6
 
(*                                                                     *)
7
 
(*  Copyright 2001 Institut National de Recherche en Informatique et   *)
8
 
(*  en Automatique.  All rights reserved.  This file is distributed    *)
9
 
(*  only by permission.                                                *)
10
 
(*                                                                     *)
11
 
(***********************************************************************)
12
 
type lex�me =
13
 
   | MC of string
14
 
   | Ident of string
15
 
   | Entier of int;;
16
 
 
17
 
let string_of_lex�me = function
18
 
  | MC s -> "MC " ^ s
19
 
  | Ident id -> "Ident " ^ id
20
 
  | Entier i -> "Entier " ^ string_of_int i;;
21
 
 
22
 
let rec lire_entier accumulateur = parser
23
 
  | [< ' ('0' .. '9' as c); flux >] ->
24
 
      lire_entier (10 * accumulateur + int_of_char c - 48) flux
25
 
  | [< >] ->
26
 
      accumulateur;;
27
 
 
28
 
let tampon = String.make 10 '-';;
29
 
 
30
 
let rec lire_mot position = parser
31
 
  | [< ' ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '\'' | 
32
 
          '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' |
33
 
          '�' | '�' | '�' | '�' | 
34
 
          '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' |
35
 
          '�' | '�' | '�' as c); flux >] ->
36
 
      if position < String.length tampon then tampon.[position] <- c;
37
 
      lire_mot (position + 1) flux
38
 
  | [< >] ->
39
 
      String.sub tampon 0 (min position (String.length tampon));;
40
 
 
41
 
let rec lire_symbole position = parser
42
 
  | [< ' ('!' | '$' | '%' | '&' | '*' | '+' | '-' | '.' | '/' | ':' | 
43
 
          ';' | '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' as c); flux >] ->
44
 
      if position < String.length tampon then tampon.[position] <- c;
45
 
      lire_symbole (position + 1) flux
46
 
  | [< >] ->
47
 
      String.sub tampon 0 (min position (String.length tampon));;
48
 
 
49
 
let rec lire_commentaire = parser
50
 
  | [< ''\n' >] -> ()
51
 
  | [< 'c; flux >] -> lire_commentaire flux;;
52
 
 
53
 
let mc_ou_ident table_des_mots_cl�s ident =
54
 
    try Hashtbl.find table_des_mots_cl�s ident
55
 
    with Not_found -> Ident(ident);;
56
 
 
57
 
let mc_ou_erreur table_des_mots_cl�s caract�re =
58
 
    let ident = String.make 1 caract�re in
59
 
    try Hashtbl.find table_des_mots_cl�s ident
60
 
    with Not_found -> raise (Stream.Error ("Illegal character " ^ ident));;
61
 
 
62
 
let rec lire_lex�me table = parser
63
 
  | [< ' (' ' | '\n' | '\r' | '\t'); flux >] ->
64
 
      lire_lex�me table flux
65
 
  | [< ''#'; flux >] ->
66
 
      lire_commentaire flux; lire_lex�me table flux
67
 
  | [< ' ('A' .. 'Z' | 'a' .. 'z' | 
68
 
          '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' |
69
 
          '�' | '�' | '�' | '�' | 
70
 
          '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' |
71
 
          '�' | '�' | '�' | '�' as c); flux >] ->
72
 
      tampon.[0] <- c;
73
 
      mc_ou_ident table (lire_mot 1 flux)
74
 
  | [< ' ('!' | '$' | '%' | '&' | '*' | '+' | '.' | '/' | ':' | ';' | 
75
 
          '<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' as c); flux >] ->
76
 
      tampon.[0] <- c;
77
 
      mc_ou_ident table (lire_symbole 1 flux)
78
 
  | [< ' ('0' .. '9' as c); flux >] ->
79
 
      Entier(lire_entier (int_of_char c - 48) flux)
80
 
  | [< ''-'; flux >] ->
81
 
      begin parser
82
 
      | [< ' ('0' .. '9' as c) >] ->
83
 
          Entier(- (lire_entier (int_of_char c - 48) flux))
84
 
      | [< >] ->
85
 
          tampon.[0] <- '-';
86
 
          mc_ou_ident table (lire_symbole 1 flux)
87
 
      end flux
88
 
  | [< 'c >] ->
89
 
      mc_ou_erreur table c;;
90
 
 
91
 
let rec analyseur table flux =
92
 
    Stream.from
93
 
      (function n ->
94
 
        (parser
95
 
         | [< lex�me = lire_lex�me table >] -> Some lex�me
96
 
         | [< >] -> raise Stream.Failure) flux);;
97
 
 
98
 
let construire_analyseur mots_cl�s =
99
 
    let table_des_mots_cl�s = Hashtbl.create 17 in
100
 
    List.iter
101
 
      (function mot -> Hashtbl.add table_des_mots_cl�s mot (MC mot))
102
 
      mots_cl�s;
103
 
    analyseur table_des_mots_cl�s;;