0% found this document useful (0 votes)
20 views11 pages

CSCI351-Week 4-Lecture 1-Exercises Solution

Uploaded by

AbuSaMrA /Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

CSCI351-Week 4-Lecture 1-Exercises Solution

Uploaded by

AbuSaMrA /Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 3: Describing Syntax

and Semantics

Week 4 Lecture 1 (5/8)


Objectives
Solve Chapter 3 Problem Set

Problems: 11, 12, and 21

Pages: 182-183

CSCI351 - Concepts of Programming


1-2
Languages
Problem 11
Consider the following grammar as shown in the
figure, which of the following sentences are in the
language generated by this grammar
a. babb

b. bbbabb

c. bbaaaaabc

d. aaaaaa

CSCI351 - Concepts of Programming


1-3
Languages
Problem 11 - Solution
Part (a): babb Part (b): bbbabb
<S>  <A> a <B> b <S>  <A> a <B> b
<S>  b a <B> b <S>  <A> b a <B> b
<S>  b a b b <S>  <A> b b a <B> b
 Correct <S>  b b b a <B> b
<S>  b b b a b b
 Correct

Part (c): bbaaaaabc Part (d): aaaaaa


 Incorrect  Incorrect
(presence of c) (absence of b)

CSCI351 - Concepts of Programming


1-4
Languages
Problem 12
Consider the following grammar as shown in the
figure, which of the following sentences are in the
language generated by this grammar
a. abcd

b. acccbd

c. acccbcc

d. accd

e. ccc

CSCI351 - Concepts of Programming


1-5
Languages
Problem 12 - Solution
Part (a): abcd Part (d): accd
<S>  a <S> c <B> <S>  a <S> c <B>
<S>  a <A> c <B> <S>  a <A> c <B>
<S>  a c <A> | c c <B> <S>  a c c <B>
 Incorrect <S>  a c c d
Part (b): acccbd  Correct
 Incorrect Part (e): ccc
(presence of b) <S>  <A>
<S>  c <A>
Part (c): acccbcc
<S>  c c <A>
 Incorrect
<S>  c c c
(presence of b)
 Correct

CSCI351 - Concepts of Programming


1-6
Languages
Problem 21
Using the virtual machine instructions shown in the
figures, give an operational semantic definition of
the following:
a. Java do-while

b. Ada for

c. C++ if-then-else

d. C switch

CSCI351 - Concepts of Programming


1-7
Languages
Problem 21 (a) - Solution
a. (Java do-while) do { … }while(exp);

Operational semantic definition =>

Loop: …

if !<rel_exp> goto out

goto loop

Out: …

CSCI351 - Concepts of Programming


1-8
Languages
Problem 21 (b) - Solution
b. (Ada for) for I in min_val … max_val loop

Operational semantic definition =>

I = min_val

Loop: if I >= max_val goto out

I = I + 1

goto loop

Out: …

CSCI351 - Concepts of Programming


1-9
Languages
Problem 21 (c) - Solution
c. (C++ if-else) if(bool_exp) {stmts1;} else
{stmts2;}

Operational semantic definition =>

If <bool_exp> goto L1

goto L2

L1: stmts1;

L2: stmts2;

CSCI351 - Concepts of Programming


1-10
Languages
Problem 21 (d) - Solution
d. (C switch) switch(exp)

{case <literal>: stmts1; break;

default: stmts2; }

Operational semantic definition =>

If(exp_value == <literal>) goto L1

goto L2

L1: stmts1

L2: stmts2

CSCI351 - Concepts of Programming


1-11
Languages

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy