Binyam Programming Language Assignment
Binyam Programming Language Assignment
STREAM COMPUTER
INDIVIDUAL ASSIGNMENT
SECTION 1
NAME ID NUMBER
1
2.Grammar Breakdown:
`<S>` is the start symbol.
`<S> → a <S> c <B> | <A>`: This rule states that `<S>` can be derived in two ways: either "a" followed by
`<S>`, "c", and `<B>`, or simply `<A>`.
`<A> → c <A> | c`: This rule indicates that `<A>` can be derived as one or more "c"s.
`<B> → d`: This rule states that `<B>` can only be derived as "d".
a. abcd: This sentence cannot be generated by the grammar. The grammar requires at least one 'c' after
the 'a' if it starts with 'a'.
3. `<S>` → a a `<A>` c `<B>` c `<B>` (using the second rule for `<S>`)
Therefore, the final derivation is: `a a c c c d`. This matches `acccbd` if we correct a minor typo
(assuming it should be `acccbd` instead of `abccd`).
c. acccbcc: This sentence cannot be generated. The grammar only allows a single 'd' at the end, derived
from `<B>`.
3. `<A>` → c
2
4. `<B>` → d
Resulting in: `a c c d`
1. `<S>` → `<A>`
2. `<A>` → c `<A>`
3. `<A>` → c c `<A>`
4. `<A>` → c c c
13.The general problem with static scoping (also known as lexical scoping) are
Unintended Variable Capture: In statically scoped languages, a nested function "captures"
variables from its surrounding lexical environment (its enclosing functions). If the enclosing
function's variables are modified after the nested function is created but before it's executed,
the nested function might operate on outdated values, leading to unexpected results. This is
often referred to as the "closure problem," but it's a consequence of static scoping, not
necessarily a problem with closures themselves.
Reduced Flexibility: Static scoping can sometimes limit the flexibility of code. The scope of a
variable is fixed at compile time, meaning you can't easily change it dynamically at runtime
based on the program's execution path. This can be a constraint in certain programming
paradigms.
Debugging Challenges: Debugging can be more challenging in statically scoped languages
because the runtime environment might not directly reflect the variable's scope as defined in
the code. Tracing variable values can become more complex due to the potential for
unexpected variable capture.
Haskell: A statically typed functional programming language that requires explicit type
definitions and does not perform implicit type conversions.
Rust: A systems programming language that emphasizes safety and concurrency. Rust requires
explicit type conversions and does not allow implicit coercion.
Ada: A statically typed language used in systems programming and critical applications, Ada
enforces strict type rules and requires explicit type conversions.
Scala: While it does have some implicit conversions, it generally promotes explicit type handling
and can be configured to minimize coercion.
OCaml: A functional programming language with a strong static type system that does not allow
implicit type coercion.
3
TypeScript: Although it is a superset of JavaScript and allows some implicit coercions,
TypeScript’s type system encourages explicit type definitions and checks.
Swift: Swift has a strong static type system and requires explicit conversions between
incompatible types.
31.
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
public class FileReadTiming {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// Reading file character by character
try {
File file = new File("largeFile.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
// Process each line if needed
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
long charReadingTime = endTime - startTime;
System.out.println("Time taken to read file character by character: " + charReadingTime + "
milliseconds");
startTime = System.currentTimeMillis();
// Reading file in bulk
try {
File file = new File("largeFile.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
char[] buffer = new char[(int) file.length()];
bufferedReader.read(buffer);
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
4
endTime = System.currentTimeMillis();
long bulkReadingTime = endTime - startTime;
System.out.println("Time taken to read file in bulk: " + bulkReadingTime + " milliseconds");
if (charReadingTime < bulkReadingTime) {
System.out.println("Reading character by character was faster.");
} else {
System.out.println("Reading in bulk was faster.");
}
}
}