{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Semantics - COP4020 Programming Languages Semantics Prof...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Semantics Prof. Xin Yuan
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COP4020 Spring 2011 2 02/20/12 Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees
Background image of page 2
COP4020 Spring 2011 3 02/20/12 Static Semantics Syntax concerns the form of a valid program, while semantics concerns its meaning Context-free grammars are not powerful enough to describe certain rules, e.g. checking variable declaration with variable use Static semantic rules are enforced by a compiler at compile time Implemented in semantic analysis phase of the compiler Examples: Type checking Identifiers are used in appropriate context Check subroutine call arguments Check labels
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COP4020 Spring 2011 4 02/20/12 Dynamic Semantics Dynamic semantic rules are enforced by the compiler by generating code to perform the checks at run-time Examples: Array subscript values are within bounds Arithmetic errors Pointers are not dereferenced unless pointing to valid object A variable is used but hasn't been initialized Some languages (Euclid, Eiffel) allow programmers to add explicit dynamic semantic checks in the form of assertions, e.g. assert denominator not= 0 When a check fails at run time, an exception is raised
Background image of page 4
COP4020 Spring 2011 5 02/20/12 Attribute Grammars An attribute grammar “connects” syntax with semantics Each grammar production has a semantic rule with actions (e.g. assignments) to modify values of attributes of (non)terminals A (non)terminal may have any number of attributes Attributes have values that hold information related to the (non)terminal General form: Semantic rules are used by a compiler to enforce static semantics and/or to produce an abstract syntax tree while parsing tokens Syntax directed translation. Can also be used to build simple language interpreters production semantic rule < A > ::= < B > < C > A .a := ...; B .a := ...; C .a := ...
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COP4020 Spring 2011 6 02/20/12 Example Attributed Grammar The val attribute of a (non)terminal holds the subtotal value of the subexpression
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}