

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


(a) Lexical errors - For instance, a symbol may be used inappropriately, e.g., x@ = 3, in C, or a keyword may be misspelled. (b) Syntax errors - For instance, malformed expressions, missing tokens, etc. (c) Semantic errors - Using undeclared variables, making assignments, without respecting types and so on. (d) Logical errors - The program logic does not correspond to what the programmer intended.
2
(a) Regular Expression - 0 · (0 + 1)∗^ · 1. (b) CFG -
S → 0 A 1 A → 0 A | 1 A |
2
Argue that every string in L(G) has an equal number of 0 s and 1 s. Solution: It is important to note that you cannot use induction in the usual manner, since the derived strings have even length. Once this fact has been established formally, we can use induction over the set of even integers!
Lemma 1.1 Let x be any terminal string derived from S; |x| is even.
Proof: We use induction on the number of steps used in the derivation of x from S. BASIS: x was derived from S in one step. In this case, x must be and hence |x| = 0. It follows that the base case is proven. Assume that the hypothesis is true for all terminal strings derived from S in exactly k steps. Now consider a terminal string y derived from S in (k + 1) steps. Without loss of generality, assume that the first step in the derivation of y is S ⇒ 0 S 1. It follows that y must be of the form: y = 0w 1 , where w is a terminal string which is derived from S. However, the derivation of w from S takes exactly k steps and hence |w| must be even, as per the inductive hypothesis. It follows that |y| = |w| + 2 is also even. Using the first principle of mathematical induction, we can conclude that all terminal strings derived from S have even length. 2 We are now ready to prove the main conjecture.
Theorem 1.1 Let x be a terminal string derived from S; x must have an equal number of 0 s and 1 s.
Proof: We use induction on |x|; since |x| is always even, the induction is on the set the set of even positive integers, i.e., { 0 , 2 , 4 ,.. .}. BASIS: |x| = 0. In this case, x must be and since has the same number of 0 s as 1 s, the basis is proven. Assume that for all terminal strings x which are derived from S and which have length 2 · k, where k is a positive integer, x has an equal number of 0 s and 1 s. Now consider a terminal string y of length 2 · k + 2 (the next even number!), which is derived from S. Without loss of generality, assume that the first step in the derivation is S ⇒ 0 S 1. It follows that y is of the form: 0 w 1 , where w is some terminal string derivable from S with length 2 · k. However, as per the inductive hypothesis, w must have an equal number of 0 s and 1 s. It follows that y also has the same number of 0 s as 1 s. Using the first principle of mathematical induction, we conclude that every string derived from S has an equal number of 0 s and 1 s. 2 2
S → S · S | a
Write the set equation corresponding to this grammar and give a solution to this equation. Solution: Let S denote the set of all strings derivable from S. Then the set equation for the above grammar is:
S = S · S ∪ {a}
A solution (indeed the least fixed-point solution) to the above set equation is:
S = {a, aa, aaa,... , }
2
#include <stdio.h>
int a, b;
int p(void) { int a, p;