Docsity

CS421 Lecture 6: Regular Expressions and Ocamllex - Prof. Elsa Gunter, Study notes of Computer Science

Partial preview of the text

Download CS421 Lecture 6: Regular Expressions and Ocamllex - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity!

CS421 Lecture 6CS

Lecture 6

Today’s class ▶

Today s class

▶

Regular Expressions ▶

Ocamllex ▶

Ocamllex

▶^

These slides are based on slides by Elsa Gunter, Mattox Beckman

Overview of OcamllexOverview

of Ocamllex

Regular Expression ExamplesRegular

Expression Examples

Regular Expression ExamplesRegular

Expression Examples

▶

Keywords

y

▶

Operators ▶

Identifiers ▶

Int literals

Regular Expression ExampleRegular

Expression Example

Float-point Literal ▶

Float point Literal

Regular Expression ExampleRegular

Expression Example

New-Style Comments (//) ▶

New Style Comments (//) ▶

Old-Style Comments (/* … */)

Lexing with Reg ExprsLexing

with Reg Exprs

Create one large RE: ▶

Create one large RE: Th

dd

i

Th

en add actions

(cont ) (cont

Ambiguous cases: ▶

Ambiguous cases: ▶

Two tokens found, one longer ▶

Two tokens found, the same length

Ocamllex InputOcamllex

Input

▶

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

MechanicsMechanics ▶

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

O

l^

d

f^

l^

i^

l

P

roduces Ocaml code for a lexical analyzer in file .ml

Ocamllex InputOcamllex

Input

▶

ml contains one lexing ▶

.ml contains one lexingfunction per

entrypoint

Name of function is name given for ▶

Name of function is name given forentrypoint

Each entry point becomes an Ocaml ▶

Each entry point becomes an Ocamlfunction that takes

n+1 arguments, the

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

Ocamllex Regular ExpressionOcamllex

Regular Expression

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 ▶

string

”: concatenation of sequence of

characters ▶

e

1

e

2

: choice

Ocamllex Regular ExpressionOcamllex

Regular Expression

▶

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

Ocamllex ManualOcamllex

Manual

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