






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A set of lecture slides from cs421, a course on compilers at the university of x. The slides cover the topics of regular expressions and ocamllex, a lexer generator for the ocaml programming language. Examples of regular expressions and their implementation in ocamllex, as well as explanations of the mechanics of using ocamllex to create lexical analyzers.
Typology: Study notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Today’s class ▶
Today s class
▶
Regular Expressions ▶
Ocamllex ▶
Ocamllex
▶^
These slides are based on slides by Elsa Gunter, Mattox Beckman
▶
Keywords
y
▶
Operators ▶
Identifiers ▶
Int literals
Float-point Literal ▶
Float point Literal
New-Style Comments (//) ▶
New Style Comments (//) ▶
Old-Style Comments (/* … */)
Create one large RE: ▶
Create one large RE: Th
dd
i
Th
en add actions
Ambiguous cases: ▶
Ambiguous cases: ▶
Two tokens found, one longer ▶
Two tokens found, the same length
▶
header and
trailer contain arbitrary
▶
header and
trailer contain arbitrary
ocaml code put at top an bottom of
l
.ml ▶
let
ident =
regexp ... Introduces
ident
g
p
for use in later regular expressions
Put table of regular expressions and ▶
Put
table of regular expressions and
corresponding actions (written in ocaml)into a fileinto a file
.mll
Call ▶
Call
ocamllex .mll
P
d
l^
d
f^
l^
i^
l
roduces Ocaml code for a lexical analyzer in file .ml
▶
ml contains one lexing ▶
.ml contains one lexingfunction per
entrypoint
Name of function is name given for ▶
Each entry point becomes an Ocaml ▶
Each entry point becomes an Ocamlfunction that takes
extra implicit last argument being of typeextra implicit last argument being of typeLexing.lexbuf
▶
arg
a
rgn are for use in
action
▶
arg1... a
rgn are for use in
action
Single quoted characters for letters:
a
Single
quoted characters for letters: a
_: (underscore) matches any character
f^
i l
d
f fil
k
eof: special
end_of_file
marker
Concatenation: concatenation ▶
”: concatenation of sequence of
characters ▶
1
2
: choice
▶
e #
e : the characters in
e
but not in
▶
e
1
e
: the characters in 2
e
1
but not in
e
2
;
e
1
and
e
2
must describe just sets
f^
h
t
of characters ▶
ident: abbreviation for earlier reg exp
g
p
in let
ident =
regexp
▶
e as
id: binds the result of
e
to
id to
▶
e
1
as
id: binds the result of
e
1
to
id to
be used in the associated
action
More details can be found at ▶
More details can be found at h
l i
i^
f /
b/d
l
http://caml.inria.fr/pub/docs/manual-
ocaml/manual026.html