CS 405/505 Assignment #5: LISP Function to Derive Parse Trees, Assignments of Programming Languages

Instructions for two lisp functions: 'derive' and 'parsetrees'. The 'derive' function takes a list of context-free grammar production rules, a start symbol, and a list of integers as input, and returns the parse tree resulting from applying the productions in the order indicated by the list. The 'parsetrees' function takes the same inputs as 'derive' and an additional integer, and returns a list of all parse trees reached after the integer or fewer production rules have been applied. The document also includes a test for the 'derive' function and an example of a core program.

Typology: Assignments

Pre 2010

Uploaded on 04/12/2010

koofers-user-6gh
koofers-user-6gh 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 405/505 ASSIGNMENT #5
Due Thursday, February 24, 2005
1. Write a LISP function (derive P S L), where Pis a list of context-free grammar production
rules of the form (A X1 X2 ... Xn) corresponding to A X1X2...Xn,Sis the start symbol
of the grammar, and Lis a list of integers, which returns the parse tree resulting from applying
the productions in Pin the order indicated by L. The first production rule to be applied must
be a production of the start symbol or a null tree is returned. The process terminates if the
integer indicated does not match any production rule which can be applied, e.g. is either
not a production rule or there is no corresponding nonterminal. If there are more than one
nonterminal to apply a production rule to, the leftmost one is always selected. The parse
tree will be of the form (A X1 X2 ... Xn) for A X1X2...Xn, with each Xi replaced by
its corresponding parse tree. Test your function on the following:
(derive ’((E E + T) (E T) (T T * F) (T F) (F \(E\)) (F id)) ’E
’(1 2 4 6 3 4 6 6))
(E (E (T (F id))) + (T (T (F id)) * (F id)))
(derive ’((E E + T) (E T) (T T * F) (T F) (F \(E\)) (F id)) ’E
’(1 3 6 4 6 2 4 6))
(E (E (T (F id))) + (T (T (F id)) * (F id)))
(derive ’((E E + E) (E E * E) (E \(E\)) (E id)) ’E ’(1 4 2 4 4))
(E (E id) + (E (E id) * (E id)))
2. Write a LISP function (parsetrees P S n), where Pand Sare as in the derive function
and nis an integer, which returns a list of all parse trees reached after nor less production
rules have been applied. If n0, a null list is returned. One way to design this function is to
produce a set of lists (0 0 ... 0) (n 0’s) ... (mm... m)(nm’s) in order and call derive
for each one, making a list of the non-null parse trees returned. Note that this approach has
complexity mn, which is exponential and will not be practical for large values of n, although
it should not cause problems for the test cases below. You are welcome to develop a more
efficient algorithm which does not need to try all of the mncombinations. Test your function
on the following:
(parsetrees ’((E E + T) (E T) (T T * F) (T F) (F \( E \)) (F id)) ’E 8)
(parsetrees ’((E E + E) (E E * E) (E \( E \)) (E id)) ’E 8)
3. Consider the Core program below:
input x; input y;
u := 1; v := 0;
while (v < y) loop
u := u * x;
v := v + 1;
end loop;
output u;
Assuming that the input file contains the integers 10 and 2 (i.e., the list <10, 2>), use the
denotational semantics of Core to trace the interpretation of the program.

Partial preview of the text

Download CS 405/505 Assignment #5: LISP Function to Derive Parse Trees and more Assignments Programming Languages in PDF only on Docsity!

CS 405/505 ASSIGNMENT

Due Thursday, February 24, 2005

  1. Write a LISP function (derive P S L), where P is a list of context-free grammar production rules of the form (A X1 X2 ... Xn) corresponding to A → X 1 X 2 ...Xn, S is the start symbol of the grammar, and L is a list of integers, which returns the parse tree resulting from applying the productions in P in the order indicated by L. The first production rule to be applied must be a production of the start symbol or a null tree is returned. The process terminates if the integer indicated does not match any production rule which can be applied, e.g. is either not a production rule or there is no corresponding nonterminal. If there are more than one nonterminal to apply a production rule to, the leftmost one is always selected. The parse tree will be of the form (A X1 X2 ... Xn) for A → X 1 X 2 ...Xn, with each Xi replaced by its corresponding parse tree. Test your function on the following: (derive ’((E E + T) (E T) (T T * F) (T F) (F ( E )) (F id)) ’E ’(1 2 4 6 3 4 6 6)) ⇒ (E (E (T (F id))) + (T (T (F id)) * (F id))) (derive ’((E E + T) (E T) (T T * F) (T F) (F ( E )) (F id)) ’E ’(1 3 6 4 6 2 4 6)) ⇒ (E (E (T (F id))) + (T (T (F id)) * (F id))) (derive ’((E E + E) (E E * E) (E ( E )) (E id)) ’E ’(1 4 2 4 4)) ⇒ (E (E id) + (E (E id) * (E id)))
  2. Write a LISP function (parsetrees P S n), where P and S are as in the derive function and n is an integer, which returns a list of all parse trees reached after n or less production rules have been applied. If n ≤ 0, a null list is returned. One way to design this function is to produce a set of lists (0 0 ... 0) (n 0’s) ... (m m ... m) (n m’s) in order and call derive for each one, making a list of the non-null parse trees returned. Note that this approach has complexity mn, which is exponential and will not be practical for large values of n, although it should not cause problems for the test cases below. You are welcome to develop a more efficient algorithm which does not need to try all of the mn^ combinations. Test your function on the following: (parsetrees ’((E E + T) (E T) (T T * F) (T F) (F ( E )) (F id)) ’E 8) (parsetrees ’((E E + E) (E E * E) (E ( E )) (E id)) ’E 8)
  3. Consider the Core program below:

input x; input y; u := 1; v := 0; while (v < y) loop u := u * x; v := v + 1; end loop; output u;

Assuming that the input file contains the integers 10 and 2 (i.e., the list <10, 2>), use the denotational semantics of Core to trace the interpretation of the program.