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

(Semantic Analysis - Attribute Grammar). Given the following context- free gramm

ID: 3840557 • Letter: #

Question

(Semantic Analysis - Attribute Grammar). Given the following context- free grammar. Add attribute rules to the grammar to accumulate into the root of the tree a count of the number of subroutine calls. For example, given the string fl (a, f2(b * (c + (d - (e - f))))), the stmt at the root of the tree should have an attribute with a count of 2. stmt rightarrow assignment stmt rightarrow subr call assignment rightarrow id: = expr subr call rightarrow id (arg list) expr rightarrow primary expr tail expr tail rightarrow op expr expr tail rightarrow element primary rightarrow id primary rightarrow subr call primary rightarrow (expr) op rightarrow + | - |*|/arg list rightarrow expr args tail args tail rightarrow, arg list args tail rightarrow elementof

Explanation / Answer

PRODUCTION ATTRIBUTE RULE stmt -> assignment stmt -> subr call count := count+1 assignment -> id := expr subr call -> id( arg list ) expr -> primary expr tail expr tail -> op expr expr tail -> E primary -> id primary -> subr call count := count+1 primary -> ( expr ) op -> +|-|*|/ arg list -> expr args tail args tail -> , arg list args tail -> E