Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

CS314 Recitation #1

Important Information
● Contact Info: Chun
○ Email: larryl@cs.rutgers.edu
○ Office hours: Thursday 1PM to 3PM at CoRE 305
● Contact Info: Xinyu
○ Email: xz653@rutgers.edu
○ Office hours: Tuesday 10AM to 12PM at CoRE 305
● Contact Info: Zining
○ Email: zf140@scarletmail.rutgers.edu
○ Office hours: Wednesday 10AM to 12PM at CoRE 305

● For questions, please use Piazza!


● Recitation slides will be posted on Canvas.
Today’s Topics
● How to use ilabs

● Rewrite Systems

● Regular Expressions
How to use ilabs
Access iLab machines with WebLogin
● Go to services.cs.rutgers.edu/accounts/activate/activate to active your iLab account
and set your CS password.
● Go to weblogin.cs.rutgers.edu and enter your Rutgers NetId and CS password in the
login screen.
● For iLab machines status check, use
report.cs.rutgers.edu/nagiosnotes/iLab-machines.html
Today’s Topics
● How to use ilabs

● Rewrite Systems

● Regular Expressions
Rewrite Systems
● A rewrite system is a set of rules which we can apply to transform a string.
● In lecture, we discussed a rewrite system with these characteristics:
○ Input is a binary string, bracketed by $ and # characters. (e.g. $0101#)
○ There are six rewrite rules:
■ $1 => 1&
■ $0 => 0$
■ &1 => 1$
■ &0 => 0&
■ $# => A
■ &# => B
Applying Our Rewrite System
● Given input $01001# let's apply the rewrite system from
lecture.

● $01001#
Rules:
1. $1 => 1&
● => 0$1001# by rule 2
2. $0 => 0$
● => 01&001# by rule 1
3. &1 => 1$
● => 010&01# by rule 4 4. &0 => 0&
● => 0100&1# by rule 4 5. $# => A
● => 01001$# by rule 3 6. &# => B
● => 01001A by rule 5
Another Rewrite System
● Suppose the input remains the same: the input is a binary string, bracketed by
$ and # characters.

● Suppose we want to remove all 0’s from the input.

● How can we achieve this?


Another Rewrite System
● Suppose the input remains the same: the input is a binary string, bracketed by
$ and # characters.

● Suppose we want to remove all 0’s from the input.

● New rules:
○ $0 -> $
○ $1 -> 1$
○ $# -> ε
Applying Our Rewrite System
● Given input $10110# let's apply our rewrite system to
remove all 0’s.

● $10110#
● 1$0110# by rule 2 Rules:
● 1$110# by rule 1 1. $0 -> $
● 11$10# by rule 2 2. $1 -> 1$
● 111$0# by rule 2 3. $# -> ε
● 111$# by rule 1
● 111 by rule 3
Different Rule Application Order
● Suppose inputs are binary strings. A rewrite system is
given below.
● Given input 10110 let's apply our rewrite system to
remove all 0’s.

Rules:
● 10110 1. 01 -> 1
● 1011 by rule 2 2. 10 -> 1
● 111 by rule 1 3. 00 -> 0

We could’ve used a different rule sequence.


Different Rule Application Order
● Suppose inputs are binary strings. A rewrite system is
given below.
● Given input 10110 let's apply our rewrite system to
remove all 0’s.

Rules:
● 10110 1. 01 -> 1
● 1110 by rule 1 2. 10 -> 1
● 111 by rule 2 3. 00 -> 0
Rewrite Systems: Questions
● Can we construct a rewrite system that loops infinitely?
○ Yes! Suppose we had a rule that stated 0 -> 00 and 1 -> 11.

● Can we get different “results” for the same input string?


○ Yes. Suppose we have the following rules:
■ 0 -> 1
■ 0 -> 11

● Is there an order constraint among the set of rules applied?


○ No!
Today’s Topics
● How to use ilabs

● Rewrite Systems

● Regular Expressions
Regular Expressions
● Regular expressions are a way of recognizing regular languages.
● They use these rules:
○ (x) accepts x
○ x* accepts 0 or more copies of x
○ x+ accepts 1 or more copies of x
○ xy accepts x followed by y
○ x|y accepts either x or y

● The above rules are listed in descending order of precedence.


○ For example: a|bc accepts either "a" or "bc", not "ac"

● Note that it's common to use a-z as shorthand for (a|b|...|z)


Regular Expressions
● Describe the languages of the following regular expressions in English.
○ (0|1)*
■ The set of all binary strings
○ 1(0|1)*
■ The set of all binary strings that start with a 1.
○ (a-z|A-Z)+
■ The set of all non-empty alphabetic strings.
○ (a-z|A-Z)(a-z|A-Z|0-9)*
■ The set of all alphanumeric strings that start with a letter.

Visualization tool https://regexper.com/


Regular Expressions
● Write regular expression that produce the following languages. (There
might be more than one solution for each one.)
○ All binary strings that are divisible by 4.
■ (0|1)*00|0
○ All binary strings that contain exactly one 0.
■ 1*01*
○ All binary strings that contain at least one 0.
■ (0|1)*0(0|1)*
○ All alphanumeric strings that do not have any adjacent digits.
■ (0-9|ε)((a-z|A-Z)(0-9|ε))*
Specification of Languages
● True or False: The larger the language, the harder it is to formally specify the
language, i.e. it gets more difficult for each i: L1 ⊂ L2 ⊂ L3 . . . ⊂ Li ⊂ …

● False. This is not necessarily the case.


○ Suppose inputs to our rewrite systems are non-empty binary strings bracketed by $ and #.
○ Suppose L1 consists of the language generated by the rewrite system where we replace 00
with 0. Example: $10011001# -> 101101
○ Suppose L2 consists of the language generated by the rewrite system where we return the
binary string. Example: $10011001# -> 10011001

Then, L1 ⊂ L2, but L1 would be more difficult to formally specify because of an


extra condition.
Specification of Languages
● True or False: The larger the language, the harder it is to formally specify the
language, i.e. it gets more difficult for each i: L1 ⊂ L2 ⊂ L3 . . . ⊂ Li ⊂ …

● False. This is not necessarily the case.


○ The size of the language is not a good indicator of the difficulty of formulating it.
○ A variety of grammars, like context-free grammar, can specify a larger set of languages than
regular expression grammar, but for each particular language the size may vary.
ε edge is an edge that could transit without inputs,
used to represent multiple possible states.

Regex to NFA
1. Literal String (sequential), e.g., ab 3. “*” (0 or more copies), e.g, A*
ε

a b ε ε
0 1 2 0 A 2

2. “|” (parallel), e.g., A | B 4. “+” (1 or more copies), e.g, A+

ε A ε
ε
0 2 ε ε
0 A 2
ε B ε
Regex to NFA
Example: a(b|c)* Example Match Transitions:
- a ➡ 01278
- abc ➡ 01234725678

Tool: https://cyberzhg.github.io/toolbox/min_dfa
NFA to DFA
(4)
(1)

(2)
(5)

(3)

Use the set of all possible numbered


state to identify each DFA state.

You might also like