Question

Using the following grammar write an attributes grammar that can calculate the decimal value of an...

Using the following grammar write an attributes grammar that can calculate the decimal value of an octal number. Grammar: number = list list = list octal | octal octal = ‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ Notes: An octal number consists of octal digits, i.e. 0 to 7. The following example shows how to convert the octal 67 to its equivalent decimal. The symbol star represents multiplication. 67 = 6 * 81 + 7 * 80 = 6 * 8 + 7 = 55

Questions:

1a - introduce an attribute (4 points)

1b - specify the type of your attribute, i.e. intrinsic, inherited, or synthesized. (5 points)

1c - write the attributes grammar (Syntax rule, Semantic rule, Semantic condition) for every grammar rule. (16 points)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: In this question given grammar is  number = list list = list octal | octal octal = ‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ . Means you can understand the grammar as a number can be list and list can be list and octal or simply octal and octal can be simply number from 0 to 7 which is simply total of 8 numbers . And we know that if base of the number system is 8 then total number present in the number system is 0 to seven . Hence if you get an more than 7 then get an error which means number can be a octal.

Solution 1a)  Attribute : Attribute of a grammar is nothing but attribute has well-defined domain of values, such as an integer, float, character, string, and expressions. Attribute have additional information that are appended to the given grammar or production. When parser come a cross these attribute then what ever define by these attribute it can understand and parse accordingly.

generally Attribute are Two Types :

1) Synthesized Attribute :- A attribute is said to synthesized if parent takes a value from its children then this types of attribute is called Synthesized Attribute . For example : Let's Suppose a production rule A-> B C D the { A.s= B.s or A.s=C.s} here A is taken value from its child B and C so this is Synthesized attribute.

2) Inherited Attribute: A attribute is said to inherited attribute if a parent can inherit the value from its child or child can inherit value from its parent . Then this types of attribute is called Inherited attribute . For example : Let's Suppose a same production rule which are discuss above  A-> B C D then { A.s= B.s or B.s=A.s or C.s=B.s or C.s=D.s} all are allowed in inherited attribute . Here must you careful that variable b can take value from its sibling(Sibling either right or left )and also its parent.

Solution 1b):

Another two types of attributed

i) S-Attribute:- A attribute is said to S-attribute if it can takes a value from its parents . means S-attribute are nothing but synthesized attribute . In the S-attribute you can write SDT(Syntax Direct Translation) or you can say semantic action only from right end of right side of the production.

S-attribute are evaluated during bottom up parsing .

ii) L-Attribute:- A attribute is said to L-attribute if it is synthesized as well as inherited but only restriction that it can be inherit from left side(means a variable takes value from its left of the production . It can not takes a value from its right.) So it is more power full in terms of attribute distribution because it is both synthesized as well as inherited. In L-attribute you can write the SDT you can say semantic action any where of the production of right side either right end or middle of the production or start of the production(Note-: Here write SDT only right hand side of the production ).

L-Attribute are evaluated during parsing depth first left to right.

For example: A.s= B.s or B.s=A.s or C.s=B.s or C.s=D.s, A.s= B.s or A.s=C.s or C.s = B.s (c is takes value from its left sibling B)

Solution 1c):

In this i am writing SDT for the given grammar to convert Octal Number to an equivalent Decimal number and also given along with example in which octal number 67 are converted into equivalent decimal =55 with its semantic action along with attribute.

And you can observe that here attribute are synthesized because parent taken value from its children . You can also write the same grammar using L-attribute with miner modification. In this value is an attribute which is represent the value of that variable .

Add a comment
Know the answer?
Add Answer to:
Using the following grammar write an attributes grammar that can calculate the decimal value of an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1 - Semantics - Attributes Grammar (25 points) Using the following grammar write an attributes grammar...

    1 - Semantics - Attributes Grammar (25 points) Using the following grammar write an attributes grammar that can calculate the decimal value of an octal number. Grammar: number = list list = list octal octal octal = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7' Notes: An octal number consists of octal digits, i.e. O to 7. The following example shows how to convert the octal 67 to its equivalent decimal. The symbol star represents multiplication. 67 = 6*87 + 7* 8° = 6 * 8 +...

  • Consider the following context free grammar. Write an attribute grammar that specifies the calculation rules, i.e....

    Consider the following context free grammar. Write an attribute grammar that specifies the calculation rules, i.e. how the value of E or T is calculated. There is no need to perform type checking. We assume all types are matched correctly. Please clearly specify the type of attribute(s) you introduce, i.e. synthesized, inherited, or intrinsic. E ::= E + T | T T ::= T * F | F F ::= NUM NUM ::= 1 | 2 | 3 | 4...

  • Write a C++ program using a while loop to calculate the value of pi/2 accurate to...

    Write a C++ program using a while loop to calculate the value of pi/2 accurate to 6 digits after the decimal point using the following series: DIsplay the following and turn in printouts of the program and the results: The number of terms needed to find pi/2 THe value of pi/2 using acos(-1.0)/2 using 10 digits after the deciaml point The value of pi/2 found with the series using 10 digits after the decimal points (The first 6 digits after...

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT