CS314 – Section 5 Recitation 1 Long Zhao (lz311@rutgers.edu) Basic Linux Command Rewrite System Regular Expressions Introduction Attendance Homework
Linux command List files and directories ls List the files in the working directory ls /bin List the files in the /bin directory (or any other directory you care to specify) ls -l List the files in the working directory in long format ls -l /etc /bin List the files in the /bin directory and the /etc directory in long format ls -la .. List all files (even ones with names beginning with a period character, which are normally hidden) in the parent of the working directory in long format Get your ilab account
pwd: Get current working directory cd: Change working directory Navigation: pwd: Get current working directory cd: Change working directory File/directory manipulation: cp: Copy file/directory cp file1 file2 Copies the contents of file1 into file2. cp -i file1 file2 If file2 exists, user is prompted before it is overwritten. cp file1 dir1 Copy the contents of file1 (into a file named file1) inside dir1. cp -R dir1 dir2 Copy contents of dir1. If dir2 does not exist, it is created. Otherwise, it creates a directory named dir1 within directory dir2. mv: move or rename file/directory mv file1 file2 If file2 does not exist, file1 is renamed file2. If file2 exists, its contents are replaced with the contents of file1. mv dir1 dir2 If dir2 does not exist, then dir1 is renamed dir2. If dir2 exists, the directory dir1 is created within directory dir2. mv file1 dir1 file1 is moved to directory dir1. dir1 must exist. mkdir: create directory rm: delete file/directory (-r)
Permission: chmod: change the permissions of a file or directory $ ls -l /bin/bash => -rwxr-xr-x 1 root root 316848 Jan 20 2017 /bin/bash The file "/bin/bash" is owned by user "root" The superuser has the right to read, write, and execute this file The file is owned by the group "root" Members of the group "root" can also read and execute this file Everybody else can read and execute this file chmod: change the permissions of a file or directory chmod xyz some_file ; xyz is a 3-digit octal number, e.g: rwx = 111 in binary = 7 rw- = 110 in binary = 6 r-x = 101 in binary = 5 r-- = 100 in binary = 4 chown: change file ownership chown [new_user] file_name You must be superuser (su) to change owner. chgrp: change group ownership
Compile C program with GCC: Create (text) file: touch filename cat filename ; CTRL-D to end editing Use ‘>’ to create a text file from any command Use editor: nano, vi, etc. less filename: display content of text file Compile C program with GCC: There are 3 source files: “myprint.c” includes “myprint.h”; “main.c” includes “myprint.h” $gcc -c main.c myprint.c main.o myprint.o $gcc main.o myprint.o -o exe_file
Rewrite system $ 0 1 1 0 1 # Rules: rule 1 : $ 1 => 1 & rule 5 : $ # => A rule 6 : & # => B Rewrite: $01101# 0$1101# by rule 2 01&101# by rule 1 011$01# by rule 3 0110$1# by rule 2 01101&# by rule 1 01101B by rule 6
Rewrite system $ 1 0 1 0 0 # Rules: rule 1 : $ 1 => 1 & rule 5 : $ # => A rule 6 : & # => B Rewrite: $10100# 1&0100# by rule 1 10&100# by rule 4 101$00# by rule 3 1010$0# by rule 2 10100$# by rule 2 10100A by rule 5
Rewrite system The final value produced by a rewrite system is called the normal form of the input. Does every rewrite system result in a normal form? If yes, why? If no, give an example. Can there be more than one possible result from a rewrite system, for the same input? If yes, give an example. If no, why?
Rewrite system $110# Rules: 1 ⇒ 11 0 ⇒ 00 The final value produced by a rewrite system is called the normal form of the input. Does every rewrite system result in a normal form? If yes, why? If no, give an example. $110# Rules: 1 ⇒ 11 0 ⇒ 00
Rewrite system $110# Rules: 11 ⇒ 0 10 ⇒ 1 Can there be more than one possible result from a rewrite system, for the same input? If yes, give an example. If no, why? $110# Rules: 11 ⇒ 0 10 ⇒ 1
Rewrite system What does the following rewrite system (the one from lecture 1) produce on input $0101# ? Construct a rewrite system to determine whether a binary number (e.g., $0101#) is even or odd. $1 ⇒ 1& $0 ⇒ 0$ &1 ⇒ 1$ &0 ⇒ 0& $# ⇒ A &# ⇒ B
Rewrite system What does the following rewrite system (the one from lecture 1) produce on input $0101# ? Rules: $1 ⇒ 1& $0 ⇒ 0$ &1 ⇒ 1$ &0 ⇒ 0& $# ⇒ A &# ⇒ B $0101# ⇒ 0$101# ($0 ⇒ 0$) 0$101# ⇒ 01&01# ($1 ⇒ 1&) 01&01# ⇒ 010&1# (&0 ⇒ 0&) 010&1# ⇒ 0101$# (&1 ⇒ 1$) 0101$# ⇒ 0101A ($# ⇒ A)
Rewrite system Construct a rewrite system to determine whether a binary number (e.g., $0101#) is even or odd. Rules: 1# ⇒ -O 0# ⇒ -E 1- ⇒ - 0- ⇒ - $- ⇒ ϵ $0101# ⇒ $010-O (1# ⇒ -O) $010-O ⇒ $01-O (0- ⇒ -) $01-O ⇒ $0-O (1- ⇒ -) $0-O ⇒ $-O (0- ⇒ -) $-O ⇒ O ($- ⇒ ϵ)
00# ⇒ 0# 10# ⇒ 0# 01# ⇒ 1# 11# ⇒ 1# $0# ⇒ E $1# ⇒ O Rewrite system Construct a rewrite system to determine whether a binary number (e.g., $0101#) is even or odd. Rules: 00# ⇒ 0# 10# ⇒ 0# 01# ⇒ 1# 11# ⇒ 1# $0# ⇒ E $1# ⇒ O $0101# ⇒ $011# (01# ⇒ 1#) $011# ⇒ $01# (11# ⇒ 1#) $01# ⇒ $1# (01# ⇒ 1#) $1# ⇒ O
Regular expressions Token: shortest string of characters with individual meaning. E.g. keywords, identifier, constant, operator, … Language has few to hundreds of tokens. C language: keyword (int, float, return), identifier (var1, func), constant (0x01, 0.01), operator (+, -, …) Regular expression: a pattern used to specify a set of strings required for a particular purpose.
Regular expressions (From Scott book) A regular expression is one of 1. A character 2. The empty string, denoted ϵ 3. Two regular expressions next to each other, meaning any string generated by the first one followed by (concatenated with) any string generated by the second one 4. Two regular expressions separated by a vertical bar ( | ), meaning any string generated by the first one or any string generated by the second one 5. A regular expression followed by a Kleene star, meaning the concatenation of zero or more strings generated by the expression in front of the star
Regex exercises Find regex representing the language Language Regex {0} {0,1} {0,01} {0, ϵ}{001} {1}*{10} {10,11,1100}*
Regex exercises Find regex representing the language Language Regex {0} {0,1} 0|1 {0,01} 0|01 {0, ϵ}{001} (0|ϵ)001 {1}*{10} 1*10 {10,11,1100}* (10|11|1100)*
Regex exercises Construct a regular expression for binary numbers of length two. Construct a regular expression for binary numbers with even length. Find regular expressions over {0, 1} that determine the language which consists of all strings with even number of 1s.
Regex exercises (0|1)(0|1) or (00|01|10|11) Construct a regular expression for binary numbers of length two. (0|1)(0|1) or (00|01|10|11)
Regex exercises ((0|1)(0|1))* or (00|01|10|11) * Construct a regular expression for binary numbers with even length. ((0|1)(0|1))* or (00|01|10|11) *
Regex exercises What would be the regular expression corresponding to the language which consists of all strings of 0s and 1s that have odd length?
Regex exercises What would be the regular expression corresponding to the language which consists of all strings of 0s and 1s that have odd length? Based on the previous example, we can add only 1 digit to the front or the end of the string. Thus, the regular expression can be (0|1)(00|01|10|11)* or (00|01|10|11)*(0|1).
Regex exercises (0*10*10*)* Find regular expressions over {0, 1} that determine the language which consists of all strings with even number of 1s. (0*10*10*)*
Regex exercises Find regular expressions over {0, 1} that determine the language which consists of all strings with odd number of 0s.
Regex exercises (1*01*)(1*01*01*)* Find regular expressions over {0, 1} that determine the language which consists of all strings with odd number of 0s. (1*01*)(1*01*01*)*