Question: Extend the grammar of Figure 2.25 to include if statements and while loops, along the lines suggested by the following examples: abs := n if

Extend the grammar of Figure 2.25 to include if statements and while loops, along the lines suggested by the following examples:

abs := n

if n < 0 then abs := 0 - abs fi


sum := 0

read count

while count > 0 do

        read n
        sum := sum + n
        count := count - 1
od

write sum

Your grammar should support the six standard comparison operations in conditions, with arbitrary expressions as operands. It should also allow an arbitrary number of statements in the body of an if or while statement.

Figure 2.25:

+ stmtlist $$ stmt_list stmt list stmt stmt list stmt program stmt id := expr stmt + read id stmt write expr expr term expr expr add_op term term + factor term → term mult op factor factor → ( expr ) factor → id factor number add op +

+ stmtlist $$ stmt_list stmt list stmt stmt list stmt program stmt id := expr stmt + read id stmt write expr expr term expr expr add_op term term + factor term term mult op factor factor ( expr ) factor id factor number add op + add_op - ult.op * mult_op /

Step by Step Solution

3.42 Rating (168 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Add the following productions t... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!