# programming language principles some question answers

*label*Programming

*timer*Asked: Oct 24th, 2016

**Question description**

for details please see my attachment.

1. Consider the following grammar, G1: S -> ( L ) | a

L -> L, S | S

a. What are the terminals, non-terminals, and start symbol?

b. Write a formal definition of the grammar.

c. Find the parse trees for the following sentences:

X1. (a, a)

X2. (a, (a, a))

X3. (a, ((a, a), (a, a)))

d. Construct a derivation for (a, ((a, a), (a, a))). Indicate if it is a leftmost or a rightmost derivation.

e. What language does the grammar G1 generate?

f. A sentence, s, is said to be accepted, belongs to or is recognized by a grammar G, (or language L), if s can be derived using a finite application of the rules of G. Determine if (a, (), (a((a, a)),a(()) is accepted by G1.

2. Consider the grammar G2: bexpr -> bexpr or bterm | bterm

bterm -> bterm and bfactor | bfactor

bfactor -> not bfactor | (bexpr) | true | false

a. Construct a parse tree for the sentence: not (true or (true and false))

b. Rewrite G2 in EBNF notation.

c. Draw a syntax diagram for G2.

d. Show that G2 generates all Boolean expressions

e. Is G2 ambiguous? Explain your answer.

3. Do the following problems:

a. Write a small grammar, (called a domain specific language (DSL)), that can be used to recognize whitespaces in symbol and token streams of a programming language. Whitespace can include blanks, spaces, end-of-lines, newlines, carriage-return-line-feeds, tabs.

b. Write a small grammar to recognize identifiers (variable names) in C++.

4. The goal in this problem is to attempt to create a grammar for a DSL that can be used to recognize automobile (motor vehicle) license plates of the various states in the U.S.A. As a starting point, consider the following license plate rules:

NY: 3 letters followed by blank, followed by 4 digits.

NJ: 3 letters followed by blank, followed by 2 digits, followed by 1 letter

CT: 3 digits followed by dot, followed by 3 letters

DE: 6 digits IL: 3 digits followed by blank, followed by 4 digits

RI: 2 letters followed by hyphen, followed by 3 digits

VA: 3 letters followed by hyphen, followed by 4 digits

WA: 3 digits followed by hyphen, followed by 3 letters

a. Give examples of each of the license plate patterns described above.

b. Write a grammar G3 to support ALL the license plate patterns described above, using EBNF, such as:

A -> B | C

A -> B C

A -> {B} // 0 or more occurrences, repetitions of B

A -> [B] // B is optional, i.e., 0 or 1 occurrence of B

A -> [B](m:n) // at least m and at most n occurrences of B, m, n = 0, 1. …

c. Draw a syntax diagram that corresponds to G3. d. (Extra Credit): Extend the grammar G3 into G4, for the DSL, to support the license plate patterns for ALL 50 states of the US. Be sure to draw the corresponding syntax diagram.

---------------------------------------------------------------

1. Devise an Attribute Grammar that can be used evaluate the following kinds of arithmetic expressions. Apply your grammar to evaluate the given concrete expression. Hint: can use an evaluation technique of tree accumulation that percolates values upward a syntax tree.

(2 + (4 * 6)) * (3 + ( 5 + 7))

2. An algorithm for computing the sequence of Fibonacci numbers is given by: Fib(0) = 0, Fib(1) = 1, Fib(n) = Fib(n-1) + Fib(n-2), n > 1.

a. Write computer source code in an HLL programming language. Use the code and the example of its execution to calculate Fib(3), to identify and discuss ALL of the following concepts regarding procedure semantics: Procedure definition, declaration; function body; local variables; scope of variables; lifetime of variables; arguments; caller; callee; return statement; returned value; activation record; actual parameters; formal parameters; parameter passing mechanisms (which kind is used, in the example); type checking of parameters;

3. Consider a rooted binary tree, where each internal (intermediate) node has 2 children. Assume each node, in particular, the leaves, has a symbol or variable associated with it. For the edges from each node to its children, associate a value 1 to one edge, and a value 0 to the other edge. A special case of such trees is the Huffman tree, or Huffman coding tree. Another special case is binary decision trees. One goal is to determine the code or sequence of 0’s and 1’s that result when one traverses a path from the root to each leaf of the tree. Devise an algorithm, pseudo-code or source code to implement the generation of such root-toleaf codes, using each of the following approaches. (Hint: in case of difficulty handling the general case for arbitrary binary trees, try to first devise solutions for concrete examples of the weighted binary trees of depths 1, 2, 3, 4, 5, 6, 7, 8, and then try to tackle the general case). (Hint: use concepts and tools of procedures, functions and recursion to achieve modularity)

a. If, if-else statements, expressions

b. Case-switch statements, expressions

c. Guarded if, guarded do statements, commands

d. Attribute grammar based evaluation

4. Data types issues.

a. Devise two different data representations or (user defined) derived data types that can be used to facilitate the manipulation of weighted binary trees described above in Problem (3).

b. Use the representation schemes above to discuss how the following issues and concerns can
be handled: data type equivalence, compatibility, checking, coercion, conversion. (Hint:
consider issues that arise when the edge weights can be of different types of sets of values
(data types), and they still need to be manipulated to generate the root-to-leaf code
sequences).