Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started