Commit 55a43144 authored by POTTIER Francois's avatar POTTIER Francois

Defined ordering functions on symbols.

parent 2aff803b
......@@ -146,6 +146,13 @@ module type INSPECTION = sig
include SYMBOLS
(* Ordering functions. *)
val compare_terminals: _ terminal -> _ terminal -> int
val compare_nonterminals: _ nonterminal -> _ nonterminal -> int
val compare_symbols: xsymbol -> xsymbol -> int
val compare_words: xsymbol list -> xsymbol list -> int
(* The type ['a lr1state] is meant to be the same as in [INCREMENTAL_ENGINE]. *)
type 'a lr1state
......
......@@ -165,4 +165,37 @@ module Make
f (T.terminal i) accu
) accu
(* Ordering functions. *)
let compare_terminals t1 t2 =
(* Subtraction is safe because overflow is impossible. *)
t2i t1 - t2i t2
let compare_nonterminals nt1 nt2 =
(* Subtraction is safe because overflow is impossible. *)
n2i nt1 - n2i nt2
let compare_symbols symbol1 symbol2 =
match symbol1, symbol2 with
| X (T _), X (N _) ->
-1
| X (N _), X (T _) ->
1
| X (T t1), X (T t2) ->
compare_terminals t1 t2
| X (N nt1), X (N nt2) ->
compare_nonterminals nt1 nt2
let rec compare_words w1 w2 =
match w1, w2 with
| [], [] ->
0
| [], _ :: _ ->
-1
| _ :: _, [] ->
1
| symbol1 :: w1, symbol2 :: w2 ->
let c = compare_symbols symbol1 symbol2 in
if c <> 0 then c else compare_words w1 w2
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment