Page 1 of 1

Modify the solution of PLP problem 4.6, attached below, so that the attribute of the root accumulates the number of arit

Posted: Sun May 15, 2022 9:56 am
by answerhappygod
Modify the solution of PLP problem 4.6, attached below, so that
the attribute of the root accumulates the number of arithmetic
operations +, −, ∗, / instead of the maximum depth of nested
parentheses.
Modify The Solution Of Plp Problem 4 6 Attached Below So That The Attribute Of The Root Accumulates The Number Of Arit 1
Modify The Solution Of Plp Problem 4 6 Attached Below So That The Attribute Of The Root Accumulates The Number Of Arit 1 (422.42 KiB) Viewed 36 times
4.6 Refer back to the context-free grammar of Exercise 2.13. Add attribute rules to the grammar to accumulate into the root of the tree a count of the maximum depth to which parentheses are nested in the program string. For example, given the string f1(a, f2(b * (c + (d - (e - f))))), the stmt at the root of the tree should have an attribute with a count of 3 (the parentheses surrounding argument lists don't count). Answer: stmt → assignment stmt -7 subr_call assignment - id := expr subr_call + id ( arg_list ) expr + primary expr_tail expr_tail + op expr expr_tail —— € primary →id primary + subr-call primary + ( expr ) op + -|- |*|/ arg_list + expr args_tail args-tail → args_tail D stmt.depth := assignment.depth stmt.depth := subr-call.depth o assignment.depth := expr.depth o subr-call.depth := arg.list.depth expr.depth := max(primary.depth, expr_tail.depth) expr_tail.depth := expr.depth ► expr-tail.depth := 0 primary.depth := 0 primary.depth := subr-call.depth s primary.depth := expr.depth + 1 + Dargslist.depth := max(expr.depth, args_tail.depth) sargstail.depth := args-list.depth Dargs_tail.depth = 0 3 arg-list E