--- title: Advanced Programming Languages quiz#4 tags: Advanced Programming Languages --- :::info Tatchakorn Saibunjom 410721325 ::: # Quiz#4 Write an **L-attributed** attribute grammar, based on the CFG of **Example 4.7**, that accumulates the value of the overall expression into the root of the tree. You will need to use dynamic memory allocation so that individual attributes can hold an arbitrary amount of information. ### Example 4.7 $$ \begin{aligned} expr & \rightarrow const \ expr\_tail \\ expr\_tail &\rightarrow - \ const \ expr\_tail \ | \ \epsilon \\ \end{aligned} $$ ```graphviz digraph g { node [shape="none"] "-", "-_" [label="-"] 9, 3, 4 epsilon [label="ε"] expr_tail_ [label="expr_tail"] expr_tail__ [label="expr_tail"] expr -> 9, expr_tail expr_tail -> "-", 4, expr_tail_ expr_tail_ -> "-_", 3, expr_tail__ expr_tail__ -> epsilon } ``` ## Answer ### 1. $expr \rightarrow const \ expr\_tail$ $\square \ expr\_tail.lval := const.val$ $\square \ expr.val := expr\_tail.val$ ### 2. $expr\_tail \rightarrow - \ const \ expr\_tail$ $\square \ expr\_tail_1.lval := expr\_tail.lval - const.val$ $\square \ expr\_tail.val := expr\_tail_1.val$ ### 3. $expr\_tail \rightarrow \epsilon$ $\square \ expr\_tail.val := expr\_tail.lval$
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up