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

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

Pre 2010

Uploaded on 03/16/2009

koofers-user-epy
koofers-user-epy 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS421 Lecture 6
CS421
Lecture
6
Today
s class
Today s
class
Regular Expressions
Ocamllex
Ocamllex
These slides are based on slides by Elsa Gunter, Mattox Beckman
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

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