Compare commits

...

2 Commits

Author SHA1 Message Date
Francesco Magliocca 77a0831254 Add new example 2022-05-18 11:50:44 +02:00
Francesco Magliocca 430e9ca140 Improve parser errors 2022-05-18 11:50:36 +02:00
2 changed files with 59 additions and 1 deletions

View File

@ -0,0 +1,58 @@
datatype regex {
Empty,
Lit of string,
Cat of (regex, regex),
Alt of (regex, regex),
Star of regex
}
datatype 'a list {
Empty,
Cons of ('a, 'a list)
}
# Check if a regex matches the null string
typecheck nullable? : regex -> bool
def nullable? <Empty> = false
| nullable? <Lit s> = string_null? s
| nullable? <Cat l r> = logical_and (nullable? l) (nullable? r)
| nullable? <Alt l r> = logical_or (nullable? l) (nullable? r)
| nullable? <Star _> = true
# Check if a regex represents the empty language
typecheck unsatisfiable? : regex -> bool
def unsatisfiable? <Empty> = true
| unsatisfiable? <Lit _> = false
| unsatisfiable? <Cat l r> = logical_or (unsatisfiable? l) (unsatisfiable? r)
| unsatisfiable? <Alt l r> = logical_and (unsatisfiable? l) (unsatisfiable? r)
| unsatisfiable? <Star _> = false
# Regex derivative with respect to a character
typecheck derive : char -> regex -> regex
def derive c <Empty> = Empty
| derive c <Lit s> =
match string_null? s {
case true -> Empty
case false ->
match equal (string_head s) c {
case true -> Lit (string_tail s)
case false -> Empty
}
}
| derive c <Alt l r> = Alt (derive c l) (derive c r)
| derive c <Star r> = Cat (derive c r) (Star r)
| derive c <Cat l r> =
match nullable? l {
case true -> Alt (Cat (derive c l) r) (derive c r)
case false -> Cat (derive c l) r
}
# Let us leverage the regex derivative algorithm to define a regex matcher
typecheck match_regex : string -> regex -> bool
def match_regex str regex =
let
def match_regex1 <Empty> r = nullable? regex
| match_regex1 <Cons c cs> r = match_regex1 cs (derive c cs)
in
match_regex1 (string_to_list str) regex

View File

@ -175,7 +175,7 @@ static void parse_datatype_constructor(struct parser *p, struct constructor_list
static struct decl *parse_datatype_decl(struct parser *p) {
struct var_list *params = parse_def_var_list(p);
if (cur_tok(p) != tok_ident) {
report_error(p, "Invalid datatype name, expected an identifier.\n");
report_error(p, "Invalid datatype name `%s`, expected an identifier.\n", token_descr(cur_tok(p)));
}
char *datatype_name = cur_lexeme(p);
consume(p);