Document 10805465

advertisement
Long
Quiz2 (45mins)
Name: Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex Character
Description
.
a single character
\s
a whitespace character (space, tab, newline) \S
non-whitespace character
\d
a digit (0-9)
\D
a non-digit
\w
a word character (a-z, A-Z, 0-9, _)
\W
a non-word character
[aeiou]
matches a single character in the given set
[^aeiou]
matches a single character outside the given set
(foo|bar|baz) matches any of the alternatives specified
Quantifiers can be used to specify how many of the previous thing you
want to match on, where "thing" means either a literal character, one
of the metacharacters listed above, or a group of characters or
metacharacters in parentheses.
Character
*
+
?
{3}
{3,6}
{3,}
Description
zero or more of the previous thing
one or more of the previous thing
zero or one of the previous thing
matches exactly 3 of the previous thing
matches between 3 and 6 of the previous thing
matches 3 or more of the previous thing
The ^ metacharacter matches the beginning of the string and the $
metasymbol matches the end of the string.
Please write down R.E. (most of them will using the symbol in the table except the last one ) to match following patterns: 1) a three digits, each followed by a white space character (eg “3 4 5) /(\d\s){3}/ 2) matches a string in which every odd-­‐numbered letter is a ( eg “abacadaf” ) /(a.)+/ 3) string starts with one or more digits /^\d+/ 4) string that ends with one or more digits /^d+/ 5) nothing in the string /^$/ Problem 2. (20pts) Consider the following grammar G: " XY S !!
" aX | bX | a X !!
" Y a | Y b | a Y !!
1) Give a leftmost derivation of abaabb. S XY aXY abXY abaY abaYb abaYbb abaabb 2) Build the derivation tree for the derivation in part (1). Y X Y X Y a b X Y a a 3) What is L(G)? L(G) = ( a + b )* a a ( a + b ) *
b b Problem 3. (20pts) Given the following term: (λx.λz.x(λu.λy.y)(xzz))((λu.λv.u(uv))(λw.λz.wz))(λx.λy.yx) reduce this term as much as possible. Ans: Notice the answer could be represented in different ways, so it is not the only symbolic solution. Firstly, Identify first level lists (λx.λz.x(λu.λy.y)(xzz)) ((λu.λv.u(uv))(λw.λz.wz)) (λx.λy.yx) Secondly, reduce in each list = (λx.λz.xzz) (λv. (λw.λz.wz) ((λw.λz.wz)v) ) (λx.λy.yx) = (λx.λz.xzz) (λv. (λw.λz.wz) (λz.vz) ) (λx.λy.yx) Thirdly, reduce the 2nd list = (λx.λz.xzz) (λw.λz.wz) (λz. (λx.λy.yx)z) = (λx.λz.xzz) (λw.λz.wz) (λz.λy.yz ) = (λw.λz.wz) (λz.λy.yz ) (λz.λy.yz ) Problem 4. (20pts) Below is a C language program #include<stdio.h> #include<conio.h> int factorial(int); int factorial (int i) { int f; if(i==1) return 1; else f = i* factorial (i-1); return f; } void main() { int x; printf("Enter any number to calculate factorial :"); scanf("%d",&x); printf("\nFactorial : %d", factorial (x)); } It is a C program calculating factorial number, so please do : 1) Since Fortran77 does not support recursive calculation, please write an equivalent Fortran77 program. Here are the keyword you can use : program, Integer, read, if, elseif , else, endif, stop, end, function, return, write. program main integer x, answer read(*,*) x answer = factorial(x) write(*,*) answer stop end INTEGER FUNCTION factorial(n) INTEGER n, i factorial = 1 DO i = 2,n factorial = factorial * i END DO END FUNCTION 2) Write equivalent MIPS program and show how stack changes slti $t0, $a0, 2 # if i < 2 (i.e i == 1) beq $t0, $zero, cont # if i >= 2 go to cont addi $v0, $zero, 1 # else make the resturn value 1 jr $ra cont: # OPERATION 1: save into stack main: addi $sp, $sp, -­‐8 # make space in the stack sw $ra, 0($sp) # save the return address sw $a0, 4($sp) # save the argument value # OPERATION 2: compute fact(n -­‐ 1) addi $a0, $a0, -­‐1 jal fact # OPERATION 3: restore from stack lw $ra, 0($sp) # get old return address from stack lw $a0, 4($sp) # get old argument value from stack addi $sp, $sp, 8 # return stack pointer to original value, # thus erasing all values ECS 120 Handout
Exam
4
MT: Midterm
# OPERATION 4: finally n * fact(n -­‐ 1) mult $v0, $a0 # multiply n * fib(n -­‐ 1) 2 Short Answer
mflo $v0 # gets the result of the multiplication from # the low register 1. Complete the
following
sentence,
being
mathematically
precise
and
following the conven jr $ra tions of your text: A DFA is a five-tuple M = (Q, Σ, δ, q0 , F ) where Q is a finite set, Σ
is an alphabet, q0 ∈ Q, F ⊆ Q, and δ is a function with domain
and range
.
2. Carefully explain what it means if I say: “the context-free languages are closed under
intersection.” Don’t indicate if the statement is true or false—just provide a precise mathematical translation of the meaning of the claim.
3. List the first five strings, in lexicographic order, of the language
L = {x �= y : x, y ∈ {0, 1}∗ are unequal strings}
Here “�=” is a formal symbol, just like 0 and 1.
0 < 1 <�=.
!
!
Assume that characters are ordered
!
!
!
"#!!!$%&'()*!+,-&./0!123!-2.4,51//0*!)46!1!789!.4,!
4. Sketch, briefly and
informally, how a PDA for
!
L = {x �= y : x, y ∈ {0, 1}∗ are unequal strings}
!
L = {x �= y : x, y ∈ {0, 1}∗ are unequal strings}
ere “�=” is a formal symbol, just like 0 and 1. Assume that characters are ordered
< 1 <�=.
3) informally,
Sketch, briefly informally, ketch, briefly and
howand a PDA
for how a PDA for ould work.
L = {x �= y : x, y ∈ {0, 1}∗ are unequal strings}
would work. ANS: Recall in Lecture 7 Example 3
Construct PDA accepting {x "{a, b} | #b ( x) <#a ( x) ! 2#b ( x)}.
Idea
Give an example ofToahave
claim
proved
using
thecancel
product
#b ( xthat
) <# a ( xwe
), we
must have
a b which
two a' s. construction. Don’t prove
he claim—just make
precise
that was proven with the product construction.
Let theafirst
b do the claim
job.
and Solution
a,b / !
a, b / b'
a,b' / !
a, z / z
! ,! / s
a, b / b'
1!
z ! b,b'
b, a / !
!,a /!
! , ! / b'
! ,! / a
!, z / z
z!a
b, a / !
b, a / !
b, z / z
z!a
!,a /!
!, z / z
! ,! / b
z!a
! , ! / b'
1
a, z / z
z ! b,b'
! ,! / a
!,s /!
1
b, z / z
z!a
! ,! / b
"/')*-'#
Now we remove the part to cancel 2b’s, we have : Solution
a,b / !
a, b / b'
a,b' / !
a, z / z
! ,! / s
a, b / b'
1!
z ! b,b'
b, a / !
!,a /!
! , ! / b'
! ,! / a
!, z / z
z!a
b, a / !
b, a / !
b, z / z
z!a
!,a /!
!, z / z
! ,! / b
z!a
! , ! / b'
1
a, z / z
z ! b,b'
! ,! / a
!,s /!
1
b, z / z
z!a
! ,! / b
Problem 5. (20pts ) The amount of usable space on a DVD devote the same amount to data(2048bytes per sector). Calculate the size of a Double-­‐Sided, 2,295,072 sectors DVD. Size = 2048bytes per sector * 2,295,072 sectors * Double-­‐Sided = 4700307456 * 2 = 9400614912 = 8.5 Gb 
Download