Docsity

CS 3721 Lab: Building Context-Free Grammars with BNF and Parse Trees, Lab Reports of Programming Languages

Toggle sidebar
bg1
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 Check P 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/~cs3723
1. Suppose we have the following BNF.
E::=n|E+E|E*E|(E)
(a) Give a parse tree and an AST for 3 + 5 ∗(7 + 2).
(b) Rewrite the given grammar to be non-ambigous.
2. Give a context-free grammar for a small graph description language.
•Terminals: digits(0,1,...,9),(, ), ; and ->
•Each node of the graph is represented by an integer number. Each edge is
represented by a pair of nodes connected with ‘->’. For example, 3->4 is an
edge from node ‘3’ to node ‘4’
•Each graph description is a sequence of edges surrounded by a pair of (). Eg.
(1 →2; 2 →5; 5 →1)
Give a parse tree and an abstract syntax tree for (1 →2; 2 →5; 5 →1)
1

Partial preview of the text

Download CS 3721 Lab: Building Context-Free Grammars with BNF and Parse Trees and more Lab Reports Programming Languages in PDF only on Docsity!

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

  1. Suppose we have the following BNF. E ::= n | E + E | E * E | ( E ) (a) Give a parse tree and an AST for 3 + 5 ∗ (7 + 2). (b) Rewrite the given grammar to be non-ambigous.
  2. Give a context-free grammar for a small graph description language.
    • Terminals: digits(0,1,...,9),(, ), ; and ->
    • Each node of the graph is represented by an integer number. Each edge is represented by a pair of nodes connected with ‘->’. For example, 3->4 is an edge from node ‘3’ to node ‘4’
    • Each graph description is a sequence of edges surrounded by a pair of (). Eg. (1 → 2; 2 → 5; 5 → 1) Give a parse tree and an abstract syntax tree for (1 → 2; 2 → 5; 5 → 1)