Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Explain the treatment of types and static semantic errors in the attribute grammar of the calculator language with types of image below . Provide examples

Explain the treatment of types and static semantic errors in the attribute grammar of the calculator language with types of image below. Provide examples illustrating the statements you make.

image text in transcribed

image text in transcribed

image text in transcribed

4.14. With the exception of name, all variants of a given class have all the class's attributes. programitem D item.symtab: null D program.errors-item.errors.out D item.errors.in:null int_ded : item id item2 o declare.namelid, itemi, itemz, int) D item.errors.out:- item.errors.out real decl: te id item D declare.namelid, itemi, itema, real) D item errors.out:-item.errors.out read: itemid item D item.symtab:- item.symtab D if (id.name, ?) E item, symtab item2.errors.in :# item; .errors.in item.errors.in:item errors.in +id.name "undefined at" id. locationl D item,.errors.out:- item.errors out write: itemexpr item expr.symtab := item.Symtab item2 ,Sym tab :# itemi,symtab item2.errors.in : iteml.errorsJn + expr.errors D itemi.efrors.outitemz.errors.out itemid expr item b expr.symtab: itemi symtab D itemz.Symtab:- item symtab D if (id.name, A) E item Symtab -for some type A if A error and expr.type error and Aexpr.type item2-errors-in ::: item.errors.in + ["type clash at', item 1.location] item errors.in:-item.errors.in +expr.errors item2.errors.in :# itemierrors.in + lid.name "undefined at" id.location] +expr.errors item'errors-out :# item 2.errors.out ul:iteme D item.errors.out item.errors.in id : expr e D if (id.name, A) E expr.symtab -for some type A expr.errorsnull expr.type:A else expr.errors id.name "undefined at" id.location] expr. type :# error nt const expre D expr.type:- int real const: expre D expr.type real exprexpr exprs D expr.symtab:- expri.symtab D expr,.symtab :s expri symtab D check.types(expr, expr, expr) expriexpr expry D exprz.symtab expr.symtab D exprs.symtab:- expri.symtab D check.typeslexpri, expr, expr) D expr.symtab- expri.symtab D expr,.symtab : - expri.symtab D check.typeslexpri, expr2, expr) +' : expri- expr: expr, expr2 Symtab := expr1-symtab D check.typeslexpri, exprz, expr3) D exprz.symtab expri.symtab float expriexpr D convert.typelexpr, expr, int, real, "float of non-int") trune: expri expr D expr.symtab expri.Symtab convert.typelexprz, expri, real, int, trunc of non-real") macro declare.namelid, curjtem, next.item: syntax.tree.node; t: typel f (id.name, ?) E curitem.symtab next.tem.erros.in:# curJtemerrors.m + l"redefinition of" id.name "at" curitem.locationl next item.symtab: cur item.symtab- (id.name, )+(id.name, error) else next.item.errors.in:cur.jtem.errors.in nextitem.symtab:curjitem.symtab(id.name, t) macro check.typesiresult, operand1, operand2) if operandl.type error or operand2.type-error result.type: error result.errors :-operand1.errors + operand2.errors else if operand1.type operand2 type result.type :error result.errors := operandi.errors + operand2.errors + [-type clash at" result.location] else result-type :# operand!.type result.errors := operandi.errors + operand2.errors macro convert.typelold.expr, new.expr: syntax.tree.node; from.t, to.t: type; msg: string) if old expr.type from.t or old.expr.type error new.expr.type to. else new.exprerrors :" old-expr.errors + [msg "at" old.expr.location] new.expr.type error FIGURE 4.14 Attribute grammar to decorate an abstract syntax tree for the calculator language with types. We use square brackets to delimit error messages and pointed brackets to delimit symbol table entries. Juxtaposition indicates concatenation within error messages; the'+and- operators indicate insertion and removal in lists. We assume that every node has been initialized by the scanner or by action routines in the parser to contain an indication of the location (line and column) at which the corresponding construct appears in the source (see Exercise 4.22). The' ?"symbol is used as a "wild card", it matches any type. 4.14. With the exception of name, all variants of a given class have all the class's attributes. programitem D item.symtab: null D program.errors-item.errors.out D item.errors.in:null int_ded : item id item2 o declare.namelid, itemi, itemz, int) D item.errors.out:- item.errors.out real decl: te id item D declare.namelid, itemi, itema, real) D item errors.out:-item.errors.out read: itemid item D item.symtab:- item.symtab D if (id.name, ?) E item, symtab item2.errors.in :# item; .errors.in item.errors.in:item errors.in +id.name "undefined at" id. locationl D item,.errors.out:- item.errors out write: itemexpr item expr.symtab := item.Symtab item2 ,Sym tab :# itemi,symtab item2.errors.in : iteml.errorsJn + expr.errors D itemi.efrors.outitemz.errors.out itemid expr item b expr.symtab: itemi symtab D itemz.Symtab:- item symtab D if (id.name, A) E item Symtab -for some type A if A error and expr.type error and Aexpr.type item2-errors-in ::: item.errors.in + ["type clash at', item 1.location] item errors.in:-item.errors.in +expr.errors item2.errors.in :# itemierrors.in + lid.name "undefined at" id.location] +expr.errors item'errors-out :# item 2.errors.out ul:iteme D item.errors.out item.errors.in id : expr e D if (id.name, A) E expr.symtab -for some type A expr.errorsnull expr.type:A else expr.errors id.name "undefined at" id.location] expr. type :# error nt const expre D expr.type:- int real const: expre D expr.type real exprexpr exprs D expr.symtab:- expri.symtab D expr,.symtab :s expri symtab D check.types(expr, expr, expr) expriexpr expry D exprz.symtab expr.symtab D exprs.symtab:- expri.symtab D check.typeslexpri, expr, expr) D expr.symtab- expri.symtab D expr,.symtab : - expri.symtab D check.typeslexpri, expr2, expr) +' : expri- expr: expr, expr2 Symtab := expr1-symtab D check.typeslexpri, exprz, expr3) D exprz.symtab expri.symtab float expriexpr D convert.typelexpr, expr, int, real, "float of non-int") trune: expri expr D expr.symtab expri.Symtab convert.typelexprz, expri, real, int, trunc of non-real") macro declare.namelid, curjtem, next.item: syntax.tree.node; t: typel f (id.name, ?) E curitem.symtab next.tem.erros.in:# curJtemerrors.m + l"redefinition of" id.name "at" curitem.locationl next item.symtab: cur item.symtab- (id.name, )+(id.name, error) else next.item.errors.in:cur.jtem.errors.in nextitem.symtab:curjitem.symtab(id.name, t) macro check.typesiresult, operand1, operand2) if operandl.type error or operand2.type-error result.type: error result.errors :-operand1.errors + operand2.errors else if operand1.type operand2 type result.type :error result.errors := operandi.errors + operand2.errors + [-type clash at" result.location] else result-type :# operand!.type result.errors := operandi.errors + operand2.errors macro convert.typelold.expr, new.expr: syntax.tree.node; from.t, to.t: type; msg: string) if old expr.type from.t or old.expr.type error new.expr.type to. else new.exprerrors :" old-expr.errors + [msg "at" old.expr.location] new.expr.type error FIGURE 4.14 Attribute grammar to decorate an abstract syntax tree for the calculator language with types. We use square brackets to delimit error messages and pointed brackets to delimit symbol table entries. Juxtaposition indicates concatenation within error messages; the'+and- operators indicate insertion and removal in lists. We assume that every node has been initialized by the scanner or by action routines in the parser to contain an indication of the location (line and column) at which the corresponding construct appears in the source (see Exercise 4.22). The' ?"symbol is used as a "wild card", it matches any type

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions