Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider programs consisting of two parts. The first part is variable declarations . Each variable declaration is of the form: type id 1 , ...,

Consider programs consisting of two parts. The first part is variable declarations. Each variable declaration is of the form: type id1, ..., idn; where type is int or string, and each idi is an identifier and called a variable. A variable declaration must end with a semicolon. An example of variable declarations is

int x, y;

string z;

The second part is expressions. Each expression is defined as any expression in a common language such as C/C++, Java, and Python, but can contain only identifiers, numbers, and up to two types of operators: assignment := and arithmetic minus -. An expression must end with a semicolon. For example, we can have expressions like

x;

y := x;

y := x - y;

a) Write a context free grammar for the programs given above. Your grammar has to capture * Precedence: assignment := has a lower precedence than minus -. * Asscociativity: minus - is left associative. As an example of left associativity, 10-5-5 must be understood as ((10-5)-5). b) We would like to introduce two attributes -- name and type -- for each variable. The former is the name of the variable and the later the type of the variable. Write an attribute grammar for the program using name and type attributes (and auxiliary attributes you may need). You only need to introduce the attributes for appropriate non terminals in your grammar. No need to introduce them to all non-terminals in your grammar. c) We have a semantic rule on a program: ``all variables in the same expression should have the same type." For example the following program violates this rule:

int x;

string y;

x := y;

Explain how this semantic rule can be checked against a given program using the decoration of the parse tree of the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

Define marketing concepts.

Answered: 1 week ago

Question

1 what does yellow colour on the map represent?

Answered: 1 week ago