
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

CS 3721: Programming Languages Lab Lab #04: BNF and parse trees
The purpose of this recitation is to practice building context-free grammars (specifically, BNFs) to describe the syntax of small languages. You will need to use Scheme lists to express both the BNF and the parse trees/ASTs of input strings. Then, you can invoke the given Scheme functions to check the correctness of your BNF and parse trees. The following serves as an example of the process.
(require "CheckParseTree.scm") ; importing definitions from the given Scheme file
; BNF E::= n | E + E | E - E | E * E | E / E (define E_bnf ’((E n) (E E + E) (E E - E) (E E * E) (E E / E))) ; input: 8 * 9 - 2 (define parse1 ’(E (E n) * (E (E n) - (E n)))) (CheckParseTree ’E parse1 E_bnf) ; Check whether the parse tree is correct (GenInput parse1) ; check whether it generates the given input ; AST ’(* n (- n n))
Specifically, each context-free grammar needs to be first described using BNF inside a Scheme comment. Then, describe the BNF using a nested list in Scheme. Next, describe the parse tree for each given input string using a Scheme embedded list. To check the correctness of each parse tree, invoke two Scheme functions (defined in the given Scheme program for this lab): the CheckP arseT ree function to check whether the parse tree is properly constructed (if yes, the function CheckParseTree returns true; it returns false otherwise); and the GenInput function to check whether the parse tree generates the given input string. Finally, for each parse tree, give its corresponding AST (using the Scheme list notation) inside a comment section. After you are done, save your scheme solution and submit online at
www.cs.utsa.edu/~cs