Docsity

Formal Language Operations and Regular Expressions, Slides of Programming Languages

Toggle sidebar
bg1
Chapter 3 Excerpted Material
Introducing Basic Terminology
Token Sample Lexemes Informal Description of Pattern
const
if
relation
id
num
literal
const
if
<, <=, =, < >, >, >=
pi, count, D2
3.1416, 0, 6.02E23
“core dumped”
const
if
< or <= or = or < > or >= or >
letter followed by letters and digits
any numeric constant
any characters between “ and “ except
“
Classifies
Pattern
Actual values are critical. Info is :
1. Stored in symbol table
2. Returned to parser
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59

Partial preview of the text

Download Formal Language Operations and Regular Expressions and more Slides Programming Languages in PDF only on Docsity!

Chapter 3 Excerpted Material

Introducing Basic Terminology

Token Sample Lexemes Informal Description of Pattern const if relation id num literal

const if <, <=, =, < >, >, >= pi, count, D 3.1416, 0, 6.02E “core dumped”

const if < or <= or = or < > or >= or > letter followed by letters and digits any numeric constant any characters between “ and “ except “ Classifies Pattern

Actual values are critical. Info is :

  1. Stored in symbol table
  2. Returned to parser

Language Concepts

A language, L, is simply any set of strings

over a fixed alphabet.

Alphabet Languages {0,1} {0,10,100,1000,100000…} {0,1,00,11,000,111,…} {a,b,c} {abc,aabbcc,aaabbbccc,…} {A, … ,Z} {TEE,FORE,BALL,…} {FOR,WHILE,GOTO,…} {A,…,Z,a,…,z,0,…9, { All legal PASCAL progs} +,-,…,<,>,…} { All grammatically correct English sentences } Special Languages: ∅ - EMPTY LANGUAGE ∈ - contains ∈ string only Docsity.com

Formal Language Operations

Examples

L = {A, B, C, D } D = {1, 2, 3}

L ∪ D = {A, B, C, D, 1, 2, 3 }

LD = {A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, D3 }

L^2 = { AA, AB, AC, AD, BA, BB, BC, BD, CA, … DD}

L^4 = L^2 L^2 = ??

L* = { All possible strings of L plus ∈ } L+^ = L* - ∈ L (L ∪ D ) = ?? L (L ∪ D )* = ??

Language & Regular Expressions

• A Regular Expression is a Set of Rules /

Techniques for Constructing Sequences of

Symbols (Strings) From an Alphabet.

• Let Σ Be an Alphabet, r a Regular Expression

Then L(r) is the Language That is

Characterized by the Rules of R

EXAMPLES of Regular Expressions

L = {A, B, C, D } D = {1, 2, 3}

A | B | C | D = L

(A | B | C | D ) (A | B | C | D ) = L^2

(A | B | C | D )* = L*

(A | B | C | D ) ((A | B | C | D ) | ( 1 | 2 | 3 )) = L (L ∪ D)

Algebraic Properties of

Regular Expressions

AXIOM DESCRIPTION r | s = s | r r | (s | t) = (r | s) | t (r s) t = r (s t)

∈r = r r∈ = r r* = ( r | ∈ )*

r ( s | t ) = r s | r t ( s | t ) r = s r | t r

r** = r*

| is commutative | is associative concatenation is associative concatenation distributes over |

relation between * and ∈

∈ Is the identity element for concatenation

  • is idempotent

Finite Automata & Language Theory

Finite Automata : A recognizer that takes an input string & determines whether it’s a valid sentence of the language Non-Deterministic : Has more than one alternative action for the same input symbol. Can’t utilize algorithm! Deterministic : Has at most one action for a given input symbol.

Both types are used to recognize regular expressions.

NFAs & DFAs

Non-Deterministic Finite Automata (NFAs) easily represent regular expression, but are somewhat less precise.

Deterministic Finite Automata (DFAs) require more complexity to represent regular expressions, but offer more precision.

We’ll discuss both plus conversion algorithms, i.e., NFA → DFA and DFA → NFA

Example NFA

S = { 0, 1, 2, 3 } s 0 = 0 F = { 3 } Σ = { a, b }

start (^0) a 1 b 2 b 3

a

b

What Language is defined?

What is the Transition Table?

s t a t e

i n p u t 0 1 2

a b { 0, 1 } -- { 2 } -- { 3 }

{ 0 }

∈ (null) moves possible i ∈ j Switch state but do not use any input symbol

Epsilon-Transitions

• Given the regular expression: (a (b*c)) | (a (b

|c+)?)

  • Find a transition diagram NFA that recognizes it.

• Solution?

Example - DFA

start (^0) a 1 b 2 b 3

a

b

start (^0) a 1 b 2 b 3 b

a b

a a What Language is Accepted?

Recall the original NFA:

Regular Expression to NFA

We now focus on transforming a Reg. Expr. to an NFAConstruction

This construction allows us to take:

  • Regular Expressions (which describe tokens)
  • To an NFA (to characterize language)
  • To a DFA (which can be computerized) The construction process is componentwise Builds NFA from components of the regular expression in a special order with particular techniques. NOTE: Construction is syntax-directed translation, i.e., syntax of regular expression is determining factor for NFA construction and structure. Docsity.com

Construction Algorithm : R.E. → NFA

Construction Process : 1 st^ : Identify subexpressions of the regular expression ∈ Σ symbols r | s rs r*

2 nd^ : Characterize “pieces” of NFA for each subexpression

Piecing Together NFAs

  1. For a ∈ Σ in the regular expression, construct NFA

start (^) i^ a f (^) L(a)

  1. For ∈ in the regular expression, construct NFA

start (^) i ∈ f L(∈)