4.4 Write an S-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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please help me with this Principles of Programming Languages homework.

The question is attached along with Example 4.7.

Thanks!

4.4 Write an S-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.
Transcribed Image Text:4.4 Write an S-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
Top-down CFG and parse
tree for subtraction
Inherited Attributes
In general, we can imagine (and will in fact have need of) attributes whose values
are calculated when their symbol is on the right-hand side of the current produc-
tion. Such attributes are said to be inherited. They allow contextual information
to flow into a symbol from above or from the side, so that the rules of that produc-
tion can be enforced in different ways (or generate different values) depending on
surrounding context. Symbol table information is commonly passed from sym-
bol to symbol by means of inherited attributes. Inherited attributes of the root of
the parse tree can also be used to represent the external environment (character-
istics of the target machine, command-line arguments to the compiler, etc.).
As a simple example of inherited attributes, consider the following fragment
of an LL(1) expression grammar (here covering only subtraction):
expr
const expr_tail
expr_tail → - const expr_tail | €
For the expression 9 -4 -3, we obtain the following parse tree:
9
expr
expr_tail
4
expr_tail
3
expr_tail
If we want to create an attribute grammar that accumulates the value of the
overall expression into the root of the tree, we have a problem: because subtrac-
tion is left associative, we cannot summarize the right subtree of the root with
a single numeric value. If we want to decorate the tree bottom-up, with an S-
attributed grammar, we must be prepared to describe an arbitrary number of
right operands in the attributes of the top-most expr-tail node (see Exercise 4.4).
This is indeed possible, but it defeats the purpose of the formalism: in effect, it
requires us to embed the entire tree into the attributes of a single node, and do all
the real work inside a single semantic function.
Transcribed Image Text:EXAMPLE 4.7 Top-down CFG and parse tree for subtraction Inherited Attributes In general, we can imagine (and will in fact have need of) attributes whose values are calculated when their symbol is on the right-hand side of the current produc- tion. Such attributes are said to be inherited. They allow contextual information to flow into a symbol from above or from the side, so that the rules of that produc- tion can be enforced in different ways (or generate different values) depending on surrounding context. Symbol table information is commonly passed from sym- bol to symbol by means of inherited attributes. Inherited attributes of the root of the parse tree can also be used to represent the external environment (character- istics of the target machine, command-line arguments to the compiler, etc.). As a simple example of inherited attributes, consider the following fragment of an LL(1) expression grammar (here covering only subtraction): expr const expr_tail expr_tail → - const expr_tail | € For the expression 9 -4 -3, we obtain the following parse tree: 9 expr expr_tail 4 expr_tail 3 expr_tail If we want to create an attribute grammar that accumulates the value of the overall expression into the root of the tree, we have a problem: because subtrac- tion is left associative, we cannot summarize the right subtree of the root with a single numeric value. If we want to decorate the tree bottom-up, with an S- attributed grammar, we must be prepared to describe an arbitrary number of right operands in the attributes of the top-most expr-tail node (see Exercise 4.4). This is indeed possible, but it defeats the purpose of the formalism: in effect, it requires us to embed the entire tree into the attributes of a single node, and do all the real work inside a single semantic function.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education