REGULAR EXPRESSIONS Module 04 3 COP 4020 Programing

  • Slides: 14
Download presentation
REGULAR EXPRESSIONS Module 04. 3 COP 4020 – Programing Language Concepts Dr. Manuel E.

REGULAR EXPRESSIONS Module 04. 3 COP 4020 – Programing Language Concepts Dr. Manuel E. Bermudez

TOPICS Define Regular Expressions Conversion from Right. Linear Grammar to Regular Expression

TOPICS Define Regular Expressions Conversion from Right. Linear Grammar to Regular Expression

REGULAR EXPRESSIONS • A compact, easy-to-read language description. • Use operators to denote the

REGULAR EXPRESSIONS • A compact, easy-to-read language description. • Use operators to denote the language constructors described earlier, to build complex languages from simple atomic ones.

REGULAR EXPRESSIONS Definition: A regular expression over an alphabet Σ is recursively defined as

REGULAR EXPRESSIONS Definition: A regular expression over an alphabet Σ is recursively defined as follows: 1. ø denotes language ø 2. ε denotes language {ε} 3. a denotes language {a}, for all a Σ. 4. (P + Q) denotes L(P) U L(Q), where P, Q are r. e. ’s. 5. (PQ) denotes L(P)·L(Q), where P, Q are r. e. ’s. 6. P* denotes L(P)*, where P is a r. e. To prevent excessive parentheses, we assume left associativity, and the following operator precedence: * (highest), · , + (lowest)

REGULAR EXPRESSIONS Examples: (O + 1)*: any string of O’s and 1’s. (O +

REGULAR EXPRESSIONS Examples: (O + 1)*: any string of O’s and 1’s. (O + 1)*1: any string of O’s and 1’s, ending with a 1. 1*O 1*: any string of 1’s with a single O inserted. Letter (Letter + Digit)*: an identifier. Digit*: an integer. Quote Char* Quote: a string. † # Char* Eoln: a comment. † {Char*}: another comment. † † Assuming that Char does not contain quotes, eoln’s, or }.

REGULAR EXPRESSIONS Additional Regular Expression Operators: • a+ = aa* (one or more a’s)

REGULAR EXPRESSIONS Additional Regular Expression Operators: • a+ = aa* (one or more a’s) • a? = a + ε (one or zero a’s, i. e. a is optional) • a list b = a (b a )* (a list of a’s, separated by b’s) – Examples: – Syntax for a function call: Name '(' Expression list ', ' ')' – Identifier: – Floating-point constant:

REGULAR EXPRESSIONS Conversion from Right-linear grammars to regular expressions S → a. S R

REGULAR EXPRESSIONS Conversion from Right-linear grammars to regular expressions S → a. S R → a. S S → a. S means L(S) ⊇ {a}·L(S) → b. R S → b. R means L(S) ⊇ {b}·L(R) →ε S→ε L(S) ⊇ {ε} means Together, they mean that L(S) = {a}·L(S) + {b}·L(R) + {ε}, or S = a. S + b. R + ε Similarly, R → a. S means L(R) = {a} ·L(S), or R = a. S. Thus, S = a. S + b. R + ε R = a. S System of simultaneous equations. The variables are the nonterminals.

REGULAR EXPRESSIONS Solving a system of simultaneously equations. S = a. S + b.

REGULAR EXPRESSIONS Solving a system of simultaneously equations. S = a. S + b. R + ε R = a. S Back substitute R = a. S: S = a. S + ba. S + ε S = (a + ba) S + ε What to do with equations of the form X = X + β ?

REGULAR EXPRESSIONS Equations of the form: X = X + β β L(x), so

REGULAR EXPRESSIONS Equations of the form: X = X + β β L(x), so αβ L(x), αααβ L(x), … Therefore, L(x)=α*β. In our case, S = (a + ba) S + ε S = (a + ba)*

REGULAR EXPRESSIONS Conversion from Right-linear grammars to regular Expressions: 1. Set up equations: A

REGULAR EXPRESSIONS Conversion from Right-linear grammars to regular Expressions: 1. Set up equations: A = α 1 + α 2 + … + αn if A → α 1 → α 2. . . → αn

REGULAR EXPRESSIONS 2. If equation is of the form X = α, and X

REGULAR EXPRESSIONS 2. If equation is of the form X = α, and X does not appear in α, then replace every occurrence of X with α in all other equations, and delete equation X = α. 3. If equation is of the form X = αX + β, and X does not occur in α or β, then replace the equation with X = α*β. Note: Some algebraic manipulations may be needed to obtain the form X = αX + β. Important: Catenation is not commutative!!

REGULAR EXPRESSIONS Example: S→a R → aba. U U → a. S → b.

REGULAR EXPRESSIONS Example: S→a R → aba. U U → a. S → b. U →U →b → b. R Equations: S = a + b. U + b. R R = aba. U + U = (aba + ε) U U = a. S + b Back substitute R: S = a + b. U + b(aba + ε) U U = a. S + b

REGULAR EXPRESSIONS S = a + b. U + b(aba + ε) U U

REGULAR EXPRESSIONS S = a + b. U + b(aba + ε) U U = a. S + b Back substitute U: S = a + b(a. S + b) + b(aba + ε)(a. S + b) = a + ba. S + bb + babaa. S + babab + ba. S + bb = a + ba. S + bb + babaa. S + babab = (ba + babaa) S + (a + bb + babab) and therefore S = (ba + babaa)*(a + bb + babab) repeats

REGULAR EXPRESSIONS Summarizing: RGR RGL Minimum DFA RE NFA Done Coming Up … DFA

REGULAR EXPRESSIONS Summarizing: RGR RGL Minimum DFA RE NFA Done Coming Up … DFA