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

4. (10 pts) Design a grammar for Modula-2 if-statements. In Modula-2, all keywor

ID: 3890216 • Letter: 4

Question

4. (10 pts) Design a grammar for Modula-2 if-statements. In Modula-2, all keywords are written in uppercase characters. If-statements use the keyword THEN instead of parentheses (as in C) around the boolean expression, allow zero or more ELSIF-THEN parts, have an optional ELSE part at the end, and are terminated with the two tokens END IF. The body of a then-part or the else-part is a statement list instead of a single statement as in C. For example, the following two statements are legal Modula-2 if-statements: IF x Y THEN :- 3; y:- 4 END IF IF x Y THEN x :- 3 ELSIF x - y THEN x :-4 ELSIF x Write the corresponding Don't just extend the C don't need to write the grammar for assignments (or other statements) and boolean expressions; you can assume they are given. You can use brackets for optional parts in the grammar and braces for parts that can be repeated. Also show the grammar for then- and else-parts (sequence of statements separated by semicolons) -statements. r, write the grammar for Modula-2 if-statements from scratch. You

Explanation / Answer

In modula-2 if and elseif are of type BOOLEAN, they can be evaluated in the sequence of their occurance, untill one gives he value as true. Hence its associated statement sequence is executed. If only if-else is present, then if the statement if is correct then it will never goes to else statement. or if the statement if is not correct and else is present then else will be executed.

for example.

IF (ch>= "0") & (ch <= "9") THEN ReadNumber

ELSEIF (ch>= "A") & (ch <= "Z") THEN ReadIndentidier

ELSEIF ch = ' "A" ' THEN ReadString ( ' " ' )

ELSEIF ch = " ' " THEN ReadString ( " ' " )

ELSE SpecialCharacter

End

if ch value lies from number 0 to 9 then it is readed as number

if ch value lies from capital A to capital Z then it is readed as identifier

if ch is " then it is readed as string

if ch is ' then it is readed as string

else for any condition it will be readed as special character.

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