Ask Question, Ask an Expert

+61-413 786 465

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_writeint: 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_write

| |

| +-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 calculated 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 writeInt 25 ; end ; end

That is, the judgement:

[] |- program begin while true do writeInt 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 writeInt x end ;

That is, the judgement:

[][x -> int][b -> bool] |- while b do writeInt 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.:- M9717590

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Question 1 what is hadoop explaining hadoop 2 what is

Question: 1. What is Hadoop (Explaining Hadoop) ? 2. What is HDFS? 3. What is YARN (Yet Another Resource Negotiator)? The response must be typed, single spaced, must be in times new roman font (size 12) and must follow t ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

  • 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

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As