Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a simple programming language which consists of the following primitive f

ID: 3679980 • Letter: D

Question

Design a simple programming language which consists of the following primitive five statement types as program constructs, beside the declaration statements: - Declaration statements; (e.g., int x; float x; int array A[1..n-1]; float array A[1..n-1]; char x;) - read and write statement; - assignment statement; - if-then-else statement and if-then statement; and - while-do statement; - Composition statement; i.e., statement; statement(s); For example, I can use these program constructs to input (such as read) and output (such as write) any given integers, find the sum of these given integers, and output their total. program sum begin int x, sum; sum := theta; | while (not end of file) do {read x; write x; sum := sum + x; write sum; end You are free to create the descriptive details of these statements, which allows you to define a set of grammar rules (also called syntax rules) for these statements. In addition to any other reserved words, you can use begin and end as reserved words and { Statements } for defining a block-structured language. For your reference, some of the grammar rules are given as follows: [ The notation is used to denote nonterminal symbols. Otherwise, they are terminal symbols (e.g., the entire program sum... end are terminal symbols, consisting tokens/identifiers/variables, constant, assignment operators, relation operator such as not, and {... {... >...}.... > end. These grammar rules are not complete. You are free to develop a complete set of grammar rules for these statements. You are advised to expand the following grammar rules to cover most of the basic properties/features for the statements for developing a program for any given problem. 2. Construct a finite state machine M that accepts these variables, constants, assignment operators and others, as well as all the reserved words for this simple programming language that you are defined in Problem 1. 3. Construct a pushdown automata N for accepting or rejecting any given program which is written based on these program constructs, developed in Problem 1.

Explanation / Answer

  1)

Program program1;

Uses Crt;

Label 1; {this is used with a goto statement}

Var Sel: String;

    N1,N2, Total : Real;

    YN : Char; {this is a character variable type,

                 which holds single characters ONLY}

Begin

1:Clrscr;

Total := 0; {always initialise integer/real variables}

GotoXy(4,3);

Writeln('1.Addition');

GotoXy(4,4);

Writeln('2.Subtraction');

GotoXy(4,5);

Writeln('3.Exit');

GotoXy(6,8);

Write('Select: ');

Sel := Readkey;

If Sel = '1' {action} then

Begin {more than one statement}

   ClrScr;              

   Write('Input No.1:');

   Readln(N1);          

   Write('Input No.2:');

   Readln(N2);          

   Total := N1 + N2;   

   Writeln('Addition: ',N1:2:3,' + ',N2:2:3,' = ',Total:2:3);

   Write('Press any key to continue...');

   Readkey;

   Goto 1;{this leads back to the beginning of the program,

           otherwise the program terminates}

End; {Closing the if statement(begin)}

If Sel = '2' then

{note that the assignment statement

   is not used within an if statement}

Begin

   ClrScr;

   Write('Input No.1:');

   Readln(N1);

   Write('Input No.2:');

   Readln(N2);

   Total := N1 - N2;

   Write('Subtraction: ');

   Write(N1:2:3,' - ',N2:2:3,' = ',Total:2:3);

   Write('Press any key to continue...');

   Readkey;

   Goto 1;

End; {Closing the if statement}

If Sel = '3' then

Begin

   ClrScr;

   Write('Are you sure?(Y/N)');

   YN := Readkey;

   If YN = 'y' then Halt; {1 action, so no need of Begin..End}

   If YN = 'n' then Goto 1; {the goto statement is not

                             recommended for excessive use}

End;

End.

2)

Program program2;

Uses Crt;

Var Ch : Char;

Begin

Writeln('Press ''q'' to exit...');

Ch := Readkey;

While Ch <> 'q' do

Begin

   Writeln('I told you press ''q'' to exit!!');

   Ch := Readkey;

End;

End.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote