Homework2-sol - COP 5555 Programming Language Principles...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 5555 Programming Language Principles Homework 2 Solution 1. Problem 1 (25 Points) Consider the following grammar: A -> B (',' B)+ => "a" -> B B -> B '&' C => "b" -> C C -> D '#' C => "c" -> D D -> <identifier> Write the skeleton of a recursive descent parser for this grammar, including 'BuildTree' statements that will build the AST bottom-up, for the original grammar. Hint: See Powerpoint lecture 6. Solution: Proc A; { A -> B ("," B)+ => "a" ->B } Var N: Integer; B; N :=1; While Next_Token = T_, do Read (T_,); B; N := N + 1; Od Build_Tree ("a",N); end; Proc B; { B -> B "&" C => "b" -> C } C; while NexT_Token = T_& do Read (T_&); C;
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Build_Tree ("b", 2); Od end; Proc C; { C -> D "#" C => "c" -> D } D; if NexT_Token = T_# then Read (T_#); C; Build_Tree ("b", 2); end end; proc D; {D - > <ideng415 er> } Read( T_<ideng415 er> ); end; 2. Problem 2 (25 Points) The following RPAL program is overly parenthesized; remove all superfluous parentheses, i.e. minimize the parentheses without changing the meaning of the program. Also, format the program by writing it on several lines with the appropriate indentation. let (x =((a * b)-c)) in ((e & f) -> (g -> x ** (x**2)|y)
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern