Semantics

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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP4020 Programming Languages Semantics Prof. Robert van Engelen COP4020 Spring 2011 2 2/10/11 Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees COP4020 Spring 2011 3 2/10/11 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 COP4020 Spring 2011 4 2/10/11 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 COP4020 Spring 2011 5 2/10/11 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 Can also be used to build simple language interpreters production semantic rule < A > ::= < B > < C > A .a := ...; B .a := ...; C .a := ... COP4020 Spring 2011 6 2/10/11 Example Attributed Grammar The val attribute of a (non)terminal holds the subtotal value of the subexpression Nonterminals are indexed in the attribute grammar to distinghuish multiple occurrences of the nonterminal in a production production semantic rule < E 1 > ::= < E...
View Full Document

Page1 / 22

Semantics - COP4020 Programming Languages Semantics Prof....

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online