Give an example of three algorithms following this syntax.

profilelamia.cs
pl.docx

JUBAIL UNIVERSITY COLLEGE

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SEMESTER 391

CS 307: PROGRAMMING LANGUAGES

Assignment I (10 %)

.

Assignment Explanation:

An Algorithm is an English-like description of a program having the following form:

Begin

List of Variables Declarations separated by ‘;’

List of Instructions separated by ‘;’

End.

The syntax with examples is described in the table below.

Questions:

1. Give an example of three algorithms following this syntax.

2. Give a grammar generating an algorithm.

3. What are the terminals and non-terminals of your grammar?

4. Generate one of your previous examples with your grammar

5. Give an example with its left-most derivation and right most derivation

6. Is there any ambiguity in your grammar? Justify.

Syntax

Example

Variable Declarations :

List var : type ;

List var: type; ….

x,y : integer ;

z,x0,cc9:float,

C,d,e: char;

Assignment :

var := expression ;

X:=(x+2)*y;

Input a value of a variable :

read (var)

read(x)

Output an information in the screen :

write(list of comments and var) ;

Write(‘any comment ‘ , x);

Block of instructions :

begin

List instructions ;

end;

begin read(g);

f:= g%3 ;

write (‘f=’ , f);

end;

Alternative Statement :

If(condition)

Then Block of instructions

Else Block of instructions

if(t>=0)

then begin g:=t ;

h:t/2 ;

end;

else begin g:=t-4 ;

h:t/9 ;

end;

Simple Conditional Statement :

If(condition)

Then Block of instructions

if(t>=0)

then begin g:=t ;

h:t/2 ;

end;

While loop :

while(condition)

do Block of instructions

while(a<0 and not (b>4) or s>2)

do begin a:=a+1;

b:=b-1;

end;

For loop :

for var:= integer to integer step integer

do block of instructions

for i:=2 to 20 step 2

do begin S:=S+i;

P:=P*i;

end;