<assign> à<id> = <expr>
<id> àA | B | C
<expr> à<expr> + <expr>
| <expr> * <expr>
| ( <expr> )
| <id>
<S> à[ <B>, <S> ] | <B>
<B> à<C> | ( <B> )
<C> à a | b | c
Which of the following are valid sentences in this language? Give parse trees if possible.
a) (((b) ))
b) [ a, (b) ]
c) [ [c,c], [b,b] ]
a = 2b + 1;
b = a – 3 {b < 0}
Answer to the first question is provided below. Please ask other questions in a separate post.
Binary search algorithm:
# Iterative Binary Search
def binarySearch(arr, l, r, x):
while l <= r:
mid = int(l + (r - l)/2)
print(ord(arr[mid]),ord(x) )
if arr[mid] == x:
return mid
elif arr[mid] > x:
l = mid + 1
else:
r = mid - 1
return -1
arr = [ 'A', 'C', 'K', 'L', 'M' ]
x = 'K'
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index ",result)
else:
print("Element is not present in array")
# Recursive binary search.
def binarySearch (arr, l, r, x):
if r >= l:
mid = int(l + (r - l)/2)
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid+1, r, x)
else:
return -1
# Test array
arr = [ 'A', 'C', 'K', 'L', 'M' ]
x = 'K'
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index ",result)
else:
print("Element is not present in array")
OUTPUT:

In the recursive search algorithm, the number of comparisons is one while in the iterative algorithm of binary search, the number of comparisons is 3. Thus, the recursive is faster than iterative one in worst cases. But in bast cases, like if the element is present at the first index then the iterative algorithm will be faster.
(20 pts) To understand the value of recursion in a programming language: implement the binary search...
2. Consider the following grammar: <assign> à <id> = <expr> <id> à A | B | C <expr> à <id> + <expr> | <id> * <expr> | ( <expr> ) | <id> Show a parse tree and leftmost derivation for the following statements: (a) A = ( A + B ) * C (b) A = A * ( B + C ) 3. [10 Points] Show that the following grammar is...
Solve the following questions. All questions are mandatory. Q1: What’s wrong in the following grammar? S → ABC A → aA|aa B → bB|B C → cC|cccc Q2: Describe the strings generated by the following grammar S → aSa S → bSb S → aa S → bb Q3: Consider the following grammar: S → ABC A → aA|a B → bB|b C → cC|c Change the above grammar such that it generates L1={anbmck; n>=3,m>=3,k>=4} Q4: Use the following grammar...
Problem 4: (7 pts) To understand the value of recursion in a programming language, write a Program in C++ or Python that implements quicksort, first using recursion and then without recursion. Submit your program source code and screenshot(s) of the output in a separate file (pdf or .docx) Problem 5: (7 pts) To understand the value of counting loops, write a program in C++ or Python that implements matrix multiplication using counting loop constructs. Let the user input the size/dimensions of...
1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming languages according to Sebesta? a. Increased capacity to express ideas. b. Improved background for choosing appropriate languages. c. Increased ability to design new languages. d. Increased ability to learn new languages. 2. (p. 5-6.) What programming language has dominated scientific computing over the past 50 years? a. FORTRAN b. ALGOL c. SNOBOL d. PL/I 3. (p. 6.) What programming language has dominated artificial intelligence...
3 points) Question Three Consider the context-free grammar S >SS+1 SS 1a and the string aa Give a leftmost derivation for the string. 3 points) (4 poiots) (5 points) (3 points) sECTION IWOLAttcmpt.any 3.(or 2) questions from this.scction Suppose we have two tokens: (1) the keyword if, and (2) id-entifiers, which are strings of letters other than if. Show the DFA for these tokens. Give a nightmost derivation for the string. Give a parse tree for the string i) Is...