450 exam 2 Flashcards
(139 cards)
static semantics vs dynamic semantics
static: syntax
dynamic: meaning
attribute grammar is a ? grammar with additions
context-free
what are the three additions to context-free grammars that make an attribute grammar? (“need to understand but not write all down”)
- each grammar symbol has a set of attributes
- each rule has function(s) that define certain attributes of the nonterminals in the rule
- each rule has predicate(s) to check for attribute consistency
fully attributed parse tree
if all the attribute values in a parse tree have been computed
three approaches to describe dynamic semantics
operational, denotational, axiomatic
dynamic semantics: operational vs denotational vs axiomatic
operational: describe meaning by executing its statements on a machine
denotational: meaning defined by the values of the program’s variables
axiomatic: based on mathematical logic (predicate calculus); axioms (inference rules) defined for each statement type in the language
example of operational semantics
JUnit Testing
@Test public void testBinarySearch() { // do stuff }
example of denotational semantics
grammar rules with a semantic function (? - review this)
example of axiomatic semantics
{x > 5} x = x - 3 {x > 0}
precondition, statement, postcondition
assertions
the logic expressions in axiomatic expressions
precondition
an assertion before a statement that states the relationships and constraints among variables that are true at that point in execution
weakest precondition
the least restrictive precondition that will guarantee the postcondition
postcondition
an assertion following a statement
what is the weakest precondition for x = x- 3 {x > 0} ?
{x > 3}
what is the weakest precondition for a = b + 1 {a > 1} ?
{b > 0}
the syntax analysis portion of a language nearly always consists of two parts
lexical analysis and syntax analysis
lexical analyzer
converts characters in the source program into lexical units (lexemes and tokens)
lexeme
the lowest level syntactic unit (such as =)
token
collection of lexemes (such as ASSIGN_OP)
syntax analyzer (or parser)
analyzes the syntactical structure of the given input and checks if the input is in the correct syntax of the language
two categories of parsers
top-down and bottom-up
top-down parser
produce the parse tree, beginning at the root (downward to the leaves) – order is that of a leftmost derivation
bottom-up parser
produce the parse tree, beginning at the leaves (upward to the root) – order is that of the REVERSE of a rightmost derivation
two most common top-down parsing algorithms (LL algorithms)
recursive-descent parser and LL parser