programming language principles some question answers

Anonymous
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).

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BC 2016. Prof. Amenyo. CS 357. Quiz 1 2 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. 2016. Prof. Amenyo. CS 357. Quiz 1 3
1.This project is about to make a grading calculator for a single semester of 5 score in a single course. First we have to ask user to input the score ,after that we sum the all score and find the average of the score .finally we calculate the grade: Source code: INCLUDE Irvine32.inc .data promot byte "Please enter the score :",0 Array dWORd 5 DUP(0) promot1 byte "The average Score of the student is :",0 promot2 byte "The Grade of the course is :",0 NoSt dword ? Total dword ? promot3 byte "How many Student Do you have :",0 Num dword ? num1 dword 5 .code main proc mov ebx,3 mov esi,OFFSET Array mov ecx,LENGTHOF Array call ScoreVal call Sum call Average call CalcGrad call crlf call waitmsg exit main endp ;----------------------------------------------------ScoreVal PROC USES ecx edx esi ; ; Prompts the user for an arbitrary number of integers ; and inserts the integers into an array. ; Receives: ESI points to the array, ECX = array size ; Returns: nothing ;----------------------------------------------------mov edx,OFFSET promot L1: call WriteString call ReadInt call Crlf mov [esi],eax add esi,TYPE dWORD loop L1 ; ; ; ; ; display string read integer into EAX go to next output line store in array next integer ret ScoreVal ENDP ;========================== ;**************** ;************************************* Sum proc push esi push ecx mov eax,0 mov ebx,0 L2: add eax,[esi] add esi, TYPE DWORD loop L2 pop ecx pop esi ret Sum endp ;================= Average proc mov edx,offset promot2 call writeString call crlf mov bl,5 div bl ret Average endp ;============== CalcGrad proc cmp eax,90 jae A cmp eax,80 jae B cmp eax,70 jae M cmp eax,60 jae D jmp F A: mov al,'A' call WriteChar jmp Next B: mov al,'B' call WriteChar jmp Next M: mov al,'C' call WriteChar jmp Next D: mov al,'D' call WriteChar jmp Next F: mov al,'F' call WriteChar jmp Next Next: call crlf ret CalcGrad endp end main Screenshot : Grade of a course Screenshot : Grade of a course 2.This is same as the previous question the difference is we have to calculate now for 10 students Source code: INCLUDE Irvine32.inc .data promot byte "Please enter the score :",0 Array dWORd 5 DUP(0) promot1 byte "The average Score of the student is :",0 promot2 byte "The Grade of the course is :",0 NoSt dword ? Total dword ? promot3 byte "How many Student Do you have :",0 Num dword ? num1 dword 5 .code main proc ;calculate the grade of 10 student ;============================= mov ebx,0 .REPEAT INC EBX mov esi,OFFSET Array mov ecx,LENGTHOF Array call ScoreVal call Sum call Average call CalcGrad call crlf .UNTIL EBX==10 call waitmsg exit main endp ;----------------------------------------------------ScoreVal PROC USES ecx edx esi ; ; Prompts the user for an arbitrary number of integers ; and inserts the integers into an array. ; Receives: ESI points to the array, ECX = array size ; Returns: nothing ;----------------------------------------------------mov edx,OFFSET promot L1: call WriteString ; display string call ReadInt ; read integer into EAX call Crlf ; go to next output line mov [esi],eax ; store in array add esi,TYPE dWORD ; next integer loop L1 ret ScoreVal ENDP ;========================== ;**************** ;************************************* Sum proc push esi push ecx mov eax,0 mov ebx,0 L2: add eax,[esi] add esi, TYPE DWORD loop L2 pop ecx pop esi ret Sum endp ;================= Average proc mov edx,offset promot2 call writeString call crlf mov bl,5 div bl ret Average endp ;============== CalcGrad proc cmp eax,90 jae A cmp eax,80 jae B cmp eax,70 jae M cmp eax,60 jae D jmp F A: mov al,'A' call WriteChar jmp Next B: mov al,'B' call WriteChar jmp Next M: mov al,'C' call WriteChar jmp Next D: mov al,'D' call WriteChar jmp Next F: mov al,'F' call WriteChar jmp Next Next: call crlf ret CalcGrad endp end main Screen shot:10 different student grade
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors