1
(***********************************************************************)
5
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
7
(* Copyright 2001 Institut National de Recherche en Informatique et *)
8
(* en Automatique. All rights reserved. This file is distributed *)
9
(* only by permission. *)
11
(***********************************************************************)
17
let string_of_lex�me = function
19
| Ident id -> "Ident " ^ id
20
| Entier i -> "Entier " ^ string_of_int i;;
22
let rec lire_entier accumulateur = parser
23
| [< ' ('0' .. '9' as c); flux >] ->
24
lire_entier (10 * accumulateur + int_of_char c - 48) flux
28
let tampon = String.make 10 '-';;
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
39
String.sub tampon 0 (min position (String.length tampon));;
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
47
String.sub tampon 0 (min position (String.length tampon));;
49
let rec lire_commentaire = parser
51
| [< 'c; flux >] -> lire_commentaire flux;;
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);;
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));;
62
let rec lire_lex�me table = parser
63
| [< ' (' ' | '\n' | '\r' | '\t'); flux >] ->
64
lire_lex�me table flux
66
lire_commentaire flux; lire_lex�me table flux
67
| [< ' ('A' .. 'Z' | 'a' .. 'z' |
68
'�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' |
69
'�' | '�' | '�' | '�' |
70
'�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' | '�' |
71
'�' | '�' | '�' | '�' as c); flux >] ->
73
mc_ou_ident table (lire_mot 1 flux)
74
| [< ' ('!' | '$' | '%' | '&' | '*' | '+' | '.' | '/' | ':' | ';' |
75
'<' | '=' | '>' | '?' | '@' | '^' | '|' | '~' as c); flux >] ->
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)
82
| [< ' ('0' .. '9' as c) >] ->
83
Entier(- (lire_entier (int_of_char c - 48) flux))
86
mc_ou_ident table (lire_symbole 1 flux)
89
mc_ou_erreur table c;;
91
let rec analyseur table flux =
95
| [< lex�me = lire_lex�me table >] -> Some lex�me
96
| [< >] -> raise Stream.Failure) flux);;
98
let construire_analyseur mots_cl�s =
99
let table_des_mots_cl�s = Hashtbl.create 17 in
101
(function mot -> Hashtbl.add table_des_mots_cl�s mot (MC mot))
103
analyseur table_des_mots_cl�s;;