Docsity

Regular Expressions: Understanding Patterns in CS3240 Presentation - Prof. Santosh Pande, Study notes of Computer Science

Partial preview of the text

Download Regular Expressions: Understanding Patterns in CS3240 Presentation - Prof. Santosh Pande and more Study notes Computer Science in PDF only on Docsity!

CS 3240Presentation 3

Regular Expressions

(Scanners)

Parts of Compilers

1. Lexical Analysis2. Syntax Analysis3. Semantic Analysis4. Code Generation5. Optimization

Analysis Synthesis

Front

End

Back

End

Tokens

•^

Identifiers– start with letter or "_" continue with 0 or more letters or

numbers or "_"

•^

Keywords– reserved words that are part of language (if, else, return)– Note: A language doesn't have to have keywords

-^

Operators^ +

-


/

%

&

&&

|

||

•^

Literals– Numeric, string, etc.

-^

Special symbols^ ;

{

}

(

)

Regular Expressions

•^

Ways to represent strings of characters which followcertain rules– Regular Expression (a formula for matching strings that

follow some pattern

  • State Machine (Finite automata)– Computer Program -^

Two notations– Classical– Unix style (recognized by grep/vi/etc.)

Regular Expressions

•^

[ ] means a character class^ b[aeiou]d

matches

bad,

bed,

bid,

bod,

bud

Note: case sensitive [Bb][AaEeIiOoUu][Dd][a-z]

same as

[abcdefghijklmnopqrstuvwxyz]

[a-zA-Z][_a-zA-Z] A period:

.

or

[.]

How

do

you

describe

bracket

itself?

Match left hand bracket

[[]

Match right land bracket

[]]

Regular Expressions

•^

Metasymbols– Some same as used in BNF Grammar– Some have different meaning

.^

*****^

[^

]^

^^

$^

+^

/

  • (Must escape

**

if using as match character)

Regular Expressions

Some important UNIX style operators (used by grep,

awk, vi, perl etc.)

-^

^ means match at beginning

-^

$ means match at end

Regular Expressions

^

inside of a character class ([ ]) means complement of that character class^ ^a

: Match (word | line) starting with "a" [^a]

: Match any character except a small a

•^

$ means match line ending with whatever precedes $sign^ grep

the

myfile

will find all occurences of "the" in myfile

grep

-w

the

myfile

means match "the" as a word

grep

-w

^the

myfile

means match "the" as word only at

beginning of line grep

-w

the$

myfile

means match the "the" as word

only at end of line

Matches everything (except newline \n)!

Regular Expressions

•^

Write a regular expression to match an identifier [a-zA-Z_][a-zA-Z_0-9]*

-^

Write a regular expression for an unsigned integer [0-9][0-9]*

-^

Unsigned floating point [0-9]+[.][0-9]+

-^

What about [0-9][.][0-9]**

-^

Allows

( [0-9]+[.][0-9]* | [0-9]*[.][0-9]+ )

Regular Expressions

•^

Match any character not a vowel [^aeiouAEIOU]

-^

Match any uc or lc consonant [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]

-^

To include ^ put it somewhere other than thebeginning

-^

Regular Expressions are used in grep, sed, awk, perl,vi, shells, lex, yacc– Note: Each of these may have its own slightly different rules

Practice Regular Expressions•

Write a regular expression consisting of 0’s and 1’swhich

may

have a 0 but whenever it occurs it

must

be

followed by a 1

-^

An identifier that starts with a lower case letter orunderscore and followed by one or more of lowercase letters and digits and underscores. A letter or adigit must follow an underscore.

-^

A string that consists of at least 3 consecutive 0’s

Questions?

My Garage Door

The^ The ButtonButton

mmmm^ mmmm Motor Running^ Motor Running

L

L

My Garage Door

The^ The ButtonButton

"Click"^ "Click"

L

L