Ask Question, Ask an Expert

+1-415-315-9853

info@mywordsolution.com

Ask Programming Language Expert

1. Consider the following attributed tree grammar for type checking a program AST. For simplicity, it hard codes, declarations for "X" and "Y", the identifier "X" and "Y", and the constants 2 and 3. Note also, that it encodes lists of declarations and lists of statements by simply having each declarations/statements last child be the next declaration.

start: start ::= declarations statements
^ statements.env = declarations.env
^ start.type_ok ::= statements.type_ok
decl_stop: declarations ::= Îμ
^ declarations.env = nil
decl_x_int: declarations1 ::= declarations2
^ declarations1.env =
decl_y_int: declarations1 ::= declarations2
^ declarations1.env =
decl_x_bool: declarations1 ::= declarations2
^ declarations1.env =
decl_y_bool: declarations1 ::= declarations2
^ declarations1.env =
stmt_stop: statements ::= Îμ
^ statements.type_ok = true
stmt_if: statements1 ::= expr statements2 statements3 statements4
^ statements1.type_ok = (expr.type == BOOL)
and statements2.type_ok
and statements3.type_ok
and statements4.type_ok
^ expr.env = statements1.env
^ statements2.env = statements1.env
^ statements3.env = statements1.env
^ statements4.env = statements1.env
stmt_prepareint: statements1 ::= expr statements2
^ statements1.type_ok = (expr.type == INT) and statements2.type_ok
^ expr.env = statements1.env
^ statements2.env = statements1.env
<: expr1 ::= expr2 expr3
^ expr1.type = if expr2.type == INT and expr3.type == INT then BOOL else ERR
^ expr2.env = expr1.env
^ expr3.env = expr1.env
+: expr1 ::= expr2 expr3
^ expr1.type = if expr2.type == INT and expr3.type == INT then INT else ERR
^ expr2.env = expr1.env
^ expr3.env = expr1.env
*: epxr1 ::= expr2 expr3
^ expr1.type = if expr2.type == INT and expr3.type == INT then INT else ERR
^ expr2.env = expr1.env
^ expr3.env = expr1.env

2: expr ::= Îμ
^ expr.type = INT

3: expr ::= Îμ
^ expr.type = INT
id_x: expr ::= Îμ
^ expr.type = lookup(X, expr.env)
id_y: expr ::= Îμ
^ expr.type = lookup(Y, expr.env)
With the auxilary function lookup:
lookup(X, env) = if env = nil
then ERR
else let = env in
if Y = X then n else lookup(X, env2)
end

(a) Of the attributes, declarations.env, statements.env, statements.type_ok, expr.env, and expr.type, which are inherited? Which are synthesized?

(b) Is the grammar L-attributed?

(c) Consider the following tree:

start
|
+-decl_x_int
| |
| +-decl_y_bool
| |
| +-decl_stop
|
+-stmt_if
|
+-<
| |
| +-id_x
| |
| +-3
|
+-stmt_prepare
| |
| +-id_y
| |
| +-stmt_stop
|
+-stmt_stop
|
+-stmt_stop

Show how the tree might be type-checked. Annotate the nodes in the tree with numbers indicating the order in which you find outd them? Below, using the same numbering, show the attribute values computed at each step of the evaluation.

(d) Is there a type error in the program represented by this AST? If so, what is it?

2. Consider the TL13' type rules in the posted lecture notes.

(a) Derive a proof tree showing that the following program is well-typed according to those rules:

program begin while true do prepareInt 25 ; end ; end

That is, the judgement:

[] |- program begin while true do prepareInt 25 ; end ; end

(b) Derive a proof tree justifying the judgement that following statement sequence is well-typed under the environment [][x -> int][b -> bool]:

while b do prepareInt x end ;

That is, the judgement:

[][x -> int][b -> bool] |- while b do prepareInt x end ;

(c) Attempt to derive proof tree for the judgment [][x -> bool] |- x * 5 + 2 : int. What premise cannot be satisfied because it doesn't match any rule? Show your incomplete tree circling the unjustified leaf premise (at the top of the tree).

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91703

Have any Question? 


Related Questions in Programming Language

Module implementation and support1 how methods of top-down

MODULE: IMPLEMENTATION AND SUPPORT 1) How methods of top-down and bottom-up development can be applied to object-oriented software. 2) Ccommon characteristics of the prototyping, spiral, UP, and XP development approaches ...

Assignment on stackswrite a program that evaluates

Assignment on Stacks Write a program that evaluates arithmetic expressions in infix notation that are not necessarily fully parenthesized. An arithmetic operation +, -, * or / has its usual precedence and associativity. ...

Assignmentwrite a program to converts temperatures between

Assignment Write a program to converts temperatures between Fahrenheit and Celsius. Your program should print a brief message describing what it does, and then prompt the user to enter "1" if they would like to convert a ...

Write a gui application that prints out hello in either

Write a GUI application that prints out "Hello!" in either: English, French, or Spanish. When the user selects another language, the greeting shown in the greeting area should change. Your GUI should look like the interf ...

Assignmentwrite a console application to meet the following

Assignment Write a console application to meet the following requirements. Create a system for a simple library. The library has a name and a list of books. Each book has a title, author and an int as the id number. Defi ...

1 show how to transform a three-address code sequence into

1. Show how to transform a three-address code sequence into one in which each defined variable gets a unique variable name. 2. Determine the types and relative addresses for the identifiers in the following sequence of d ...

1 write a pay-raise program that requests a persons first

1. Write a pay-raise program that requests a person's first name, last name, and current annual salary, and then displays their salary for next year. People earning less than $40,000 will receive a 5% raise, and those ea ...

Write a program containing two classes named student and

Write a program containing two classes named student and roster, respectively. The program will maintain a current roster of students within a given course. Student data for the program includes student ID, first name, l ...

Suppose the heap consists of exactly the nine cars on three

Suppose the heap consists of exactly the nine cars on three trains shown in given figure (i.e., ignore the ellipses). Object o in car 11 has references from cars 12, 23, and 32. When we garbage collect car 11, where migh ...

Design and implement your own simple class to represent any

Design and implement your own simple class to represent any household item of your choice (toaster, fan, hair dryer, piano ...) Your class should have a constructor, one additional method and at least one member variable ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

WalMart Identification of theory and critical discussion

Drawing on the prescribed text and/or relevant academic literature, produce a paper which discusses the nature of group

Section onea in an atwood machine suppose two objects of

SECTION ONE (a) In an Atwood Machine, suppose two objects of unequal mass are hung vertically over a frictionless

Part 1you work in hr for a company that operates a factory

Part 1: You work in HR for a company that operates a factory manufacturing fiberglass. There are several hundred empl

Details on advanced accounting paperthis paper is intended

DETAILS ON ADVANCED ACCOUNTING PAPER This paper is intended for students to apply the theoretical knowledge around ac

Create a provider database and related reports and queries

Create a provider database and related reports and queries to capture contact information for potential PC component pro