1 / 12

Static semantics

Static semantics. Semantic checking which can be done at compile-time Type-compatibility int can be assigned to double (type coercion) double cannot be assigned to int without explicit type cast Can be captured in grammar, but only at expense of larger, more complex grammar.

naiara
Télécharger la présentation

Static semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Static semantics • Semantic checking which can be done at compile-time • Type-compatibility • int can be assigned to double (type coercion) • double cannot be assigned to int without explicit type cast • Can be captured in grammar, but only at expense of larger, more complex grammar

  2. Type rules in grammar • Must introduce new non-terminals which encode types: • Instead of a generic grammar rule for assignment: • Stmt  Var “=” Expr “;” • we need multiple rules: • Stmt  doubleVar “=”intExpr | doubleExpr “;” • Stmt  intVar“=”intExpr “;” • Of course, such rules need to handle all the relevant type possibilities (e.g. byte, char, short, int, long, float and double).

  3. Attribute grammars • Attribute grammars provide a neater way of encoding such information.

  4. Attributes • We can associate with each symbol X of the grammar a set of attributes A(X). Attributes are partitioned into: • synthesized attributes S(X) – pass info up tree • inherited attributes I(X) – pass info down tree

  5. Semantic functions • We can associate with each rule R of the grammar a set of semantic functions. • For rule X0  X1 X2 … Xn • S(X0) = f(A(X1), A(X2), …, A(Xn)) • for 1<=j<=n, I(Xj) = f(A(X0),…,A(Xj-1))

  6. Predicates • Boolean expression involving the attributes and a set of attribute values • If true, node is ok • If false, node violates semantic rule

  7. Example <assign>  <var> = <expr> <expr>.expType  <var>.actType <expr>  <var>[2] + <var>[3] <expr>.actType  if (var[2].actType = int) and (var[3].actType = int) then int else real <expr>.actType == <expr>.expType <expr>  <var> <expr>.actType  <var>.actType <expr>.actType == <expr>.expType <var>  A | B | C <var>.actType  lookUp(<var>.string) Syntax rule Semantic rule Semantic predicate

  8. <assign> <expr> <var> <var>[2] <var>[3] Suppose: A is int B is int A = A + B

  9. <assign> expected type = int actual type = int <expr> actual type = int <var> <var>[2] <var>[3] actual type = int actual type = int actual type = int Suppose: A is int B is int A = A + B

  10. <assign> expected type = real actual type = real <expr> actual type = real <var> <var>[2] <var>[3] actual type = real actual type = int actual type = real Suppose: A is real B is int A = A + B

  11. <assign> expected type = real actual type = real <expr> actual type = real <var> <var>[2] <var>[3] actual type = real actual type = int actual type = real Suppose: A is int B is real A = A + B

  12. Houston, we have a problem! <assign> expected type = int actual type = real <expr> actual type = int <var> <var>[2] <var>[3] actual type = int actual type = real actual type = int Suppose: A is int B is real A = A + B

More Related