Title: Describing Syntax and Semantics
1Chapter 3
- Describing Syntax and Semantics
2Introduction
- Syntax
- the form or structure of the expressions,
statements, and program units - grammatical rules that specify correctly written
programs in a language - Semantics
- the meaning of the expressions, statements, and
program units - what a pgm means (what is does)
- Syntax and semantics provide a languages
definition - Users of a language definition
- Other language designers
- Implementers
- Programmers (the users of the language)
3Syntax trees for expressions
- A syntax tree is a rooted tree. Each leaf is
labeled by a variable or constant (null-ary
operator) each internal node is labeled by an
operator - Arity of an operator is number of arguments
- unary
- binary
- ternary
-
- n-ary (n arguments)
4Syntax trees for expressions
- aa -3 (b c)
- number of children in tree is equal to the arity
of the operator
5Syntax trees for expressions
- We assume that each variable stores some values.
The values of a syntax tree is completed as
follows - Recursively compute values of all children of the
root. Apply the operator labeling the root to
the values obtained. The result is the value of
the tree. (values of leaves are obvious)
6Mixfix Operators
- Infix 2 3
- Prefix 2 3
- Postfix 2 3
7Mixfix Operators
- Operations specified by a combination of symbols
do not fit neatly into the prefix, infix, postfix
classifications. - EX. The if-then-else clause
- if agtb then a else b
- the meaningful components are the condition agtb
and the expressions a and b. - When symbols or keywords appear interspersed with
the components of an expression, the operation is
said to be mixfix.
8Mixfix Operators
- if agtb then a else b
- syntax trees are used to represent the abstract
of the expressions
9The General Problem of Describing Syntax
Terminology
- A sentence is a string of characters over some
alphabet - A language is a set of sentences
- A lexeme is the lowest level syntactic unit of a
language (e.g., , sum, begin) - A token is a category of lexemes (e.g.,
identifier)
10Grammars
- Arithmetic expression, for example
- 2 x (3 a) y
- ?
- const id (const id) id
-
- tokens are represented by their type
- identifier
- constant
- etc.
- A parser recognizes the expressions
11Formal Definition of Languages
- Recognizers
- A recognition device reads input strings of the
language and decides whether the input strings
belong to the language - Example syntax analysis part of a compiler
- Detailed discussion in Chapter 4
- Generators
- A device that generates sentences of a language
- One can determine if the syntax of a particular
sentence is correct by comparing it to the
structure of the generator
12Formal Methods of Describing Syntax
- Backus-Naur Form and Context-Free Grammars
- Most widely known method for describing
programming language syntax - Extended BNF
- Improves readability and writability of BNF
- Grammars and Recognizers
13BNF and Context-Free Grammars
- Context-Free Grammars
- Developed by Noam Chomsky in the mid-1950s
- Language generators, meant to describe the syntax
of natural languages - Define a class of languages called context-free
languages
14Context-Free Grammars
- Definition 1 a context-free grammar is a
4-tuple ltN, T, S, Pgt, where - N is a finite set of symbols (nonterminals),
- T is a finite set of symbols (terminals),
- N n T 0,
- S ? N (element of) is a designated nonterminal
called the start symbol, - P is a finite set of productions, each production
is an expression of the form - X ? a can use instead of ? (read could be)
- where X ? N and a is a (possibly empty) string of
symbols from N U T.
15Context-Free Grammars
- Example
- G0 ltE, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, E, Pgt,
where the production of P are - E ? 0
- E ? 1
- E ? 2
- E ? 3
-
- E ? 9
- E ? E E
- E ? E E
- E ? E E
- E ? E / E
- E ?(E)
16Context-Free Grammars
- For what follows, assume a fixed grammar
- G ltN, T, S, Pgt
- Definition 2 A sentential form is any string
over - N U T.
- Definition 3 Let a and ß be sentential forms.
We say that a derives ß in one step - (a gt1 ß) if ß results from replacing an
occurrence of some nonterminal X in a with the
right-hand side (RHS) of some production of G
whose left-hand side (LHS) is X. - called expanding
- Example In G0, we have
- E gt1 E E gt1 E E E gt1 2 E E gt1 2 3
E gt1 2 3 5
17Context-Free Grammars
- Definition 4 Assume a and ß are sentential
forms as above. - a gt0 ß iff a ß.
- For any k gt 1, a gtk 1 ß iff there exists a
sentential form ? such that a gtk ? and - ? gt1 ß.
- We say that ß is derivable from a (a gt ß) iff a
gtk ß for some k gt 0. - Example
- In G0 we have E gt 2 3 5, because E gt5 2 3
5 (that is, the derivation is in five steps).
18Context-Free Grammars
- Definition 5 The language generated by G
(denoted L(G)) is the set of all strings over T
that are derivable from the start symbol S, using
production P. - Example
- In G0 we see that the string 2 3 5 is in
L(G0).
19Context-Free Grammars
- G1 ltC, D, 0, 1, , 9, C, Pgt where P is
- C ? D
- C ? CD
- D ? 0
- D ? 1
- D ? 2
-
- D ? 9
- LHS expands to RHS
- underline what we are expanding
- left most derivation
- this grammar describes all integer constants
- ex. C gt1 CD gt1 CDD gt1 CDDD gt1 DDDD
- gt1 2DDD gt1 21DD gt1 213D gt1 2139
- gt8 2139
20Context-Free Grammars
- Ex G2 ltS, a, b, S, Pgt where P is
- S ? a S b
- S ? e empty string (epsilon)
- S gt1 aSb gt1 aaSbb gt1 aaaSbbb
- gt1 aaabbb
21Context-Free Grammars
- Definition 6 Let ? be a string of terminals
(assume a grammar G with start symbol S). We
say that ? is derivable from G if there is a
k-step derivation - S gtk ?
- (a gt ß means a gtk ß for some kgt0)
- G0 derives all arithmetic expressions (with
single digit constants) - G1 derives all integer constants
- G2 derives anbn n gt 0
22Context-Free Grammars
- Definition 7 The language derived by a grammar
G is the set of strings of terminals derivable in
G (from start symbol S). - Intuition an expression is syntactically correct
if it is derivable from some prespecified grammar - Grammar feeds into the parser
- Grammar depends on the language
23Context-Free Grammars
- Shorthand
- E ? E E can be replaced with
- E ? E E E ? E E E E E E
- E ? E E
-
- G0
- E ? 0 1 2 3 4 5 6 7 8 9
- E ? E E E E E E E / E (E)
- G1
- C ? D CD
- D ? 0 1 2 3 4 5 6 7 8 9
- use capital letters for nonterminals
- everything else is a terminal
24Metalanguages
- A metalanguage is a language used to talk about a
language (usually a different one) - We can use English as its own metalanguage (e.g.
describing English grammar in English) - It is essential to distinguish between the
metalanguage terms and the object language terms
25Backus-Naur Form (BNF)
- Backus-Naur Form or Backus Normal Form (1959)
- Invented by John Backus to describe Algol 58
- BNF is equivalent to context-free grammars
- BNF is a metalanguage used to describe another
language - BNF is formal and precise
- BNF is essential in compiler construction
- There are many dialects of BNF in use, but
- the differences are almost always minor
26BNF Fundamentals
- lt gt indicate a nonterminal that needs to be
further expanded, e.g. ltvariablegt - Symbols not enclosed in lt gt are terminals they
represent themselves, e.g. if, while, ( - Use ? or , means is defined as
- The symbol means or it separates
alternatives, e.g. ltaddopgt -
27BNF Fundamentals
- Non-terminals BNF abstractions
- Terminals lexemes and tokens
- Grammar a collection of rules
- Examples of BNF rules
- ltident_listgt identifier identifier,
ltident_listgt - ltif_stmtgt if ltlogic_exprgt then ltstmtgt
28BNF Rules
- A rule has a left-hand side (LHS) and a
right-hand side (RHS), and consists of terminal
and nonterminal symbols - A grammar is a finite nonempty set of rules
- An abstraction (or nonterminal symbol) can have
more than one RHS - ltstmtgt ltsingle_stmtgt
- begin ltstmt_listgt end
29Describing Lists
- Syntactic lists are described using recursion
- ltintegergt ltdigitgt ltintegergt ltdigitgt or
- ltintegergt ltdigitgt ltdigitgt ltintegergt
- A derivation is a repeated application of rules,
starting with the start symbol and ending with a
sentence (all terminal symbols)
30BNF Examples I
- ltdigitgt 0 1 2 3 4 5 6 7
8 9 - ltif statementgt if ( ltconditiongt )
ltstatementgt if ( ltconditiongt )
ltstatementgt else ltstatementgt
31BNF Examples II
- ltunsigned integergt ltdigitgt ltunsigned
integergt ltdigitgt - ltintegergt ltunsigned integergt
ltunsigned integergt - ltunsigned integergt
32BNF Examples III
- ltidentifiergt ltlettergt
ltidentifiergt ltlettergt ltidentifiergt
ltdigitgt - ltblockgt ltstatement listgt
- ltstatement listgt ltstatementgt
ltstatement listgt ltstatementgt
33BNF Examples IV
- ltstatementgt ltblockgt
ltassignment statementgt ltbreak statementgt
ltcontinue statementgt ltdo
statementgt ltfor loopgt ltgoto
statementgt ltif statementgt . . .
34An Example Grammar
- ltprogramgt ltstmtsgt
- ltstmtsgt ltstmtgt ltstmtgt ltstmtsgt
- ltstmtgt ltvargt ltexprgt
- ltvargt a b c d
- ltexprgt lttermgt lttermgt lttermgt - lttermgt
- lttermgt ltvargt const
35An Example Derivation
- ltprogramgt gt ltstmtsgt
- gt ltstmtgt
- gt ltvargt ltexprgt
- gt a ltexprgt
- gt a lttermgt lttermgt
- gt a ltvargt lttermgt
- gt a b lttermgt
- gt a b const
36Derivation
- Every string of symbols in the derivation is a
sentential form - A sentence is a sentential form that has only
terminal symbols - A leftmost derivation is one in which the
leftmost nonterminal in each sentential form is
the one that is expanded - A derivation may be either leftmost or rightmost
37Parse Trees
- A parser builds a parse tree for an expression.
- Given a grammar, G, a parse tree over G is a
finite rooted tree whose root is labeled with the
start symbol, S. All nodes are labeled with
symbols in N U T (if labeled with a terminal,
then node must be a leaf). - If a node n is labeled with a nonterminal X, then
its children have labels which, when read left to
right, make up the right-hand side (RHS) of some
production whose left-hand side (LHS) is X.
38Parse Trees
(start symbol)
39Parse Trees
- Parse tree for 395 (read off terminal labels from
left to right) - Derivation
- C gt1 CD gt1 CDD gt1 DDD gt1 3DD gt1 39D
gt1 395
40Parse Tree
- A hierarchical representation of a derivation
-
-
41Ambiguity in Grammars
- A grammar is ambiguous if and only if it
generates a sentential form that has two or more
distinct parse trees
42Parse Trees
- G0 Parse tree for 2 3 4
- E start symbol
43Parse Trees
- G0 is an ambiguous grammar (same string of
terminals has 2 or more different parse trees) - Unambiguous grammars are preferred (lt 1 parse
tree for every token string)
44Parse Trees
- G3 ltE, T, F, 0, .., 9, , -, , /, (, ),
E, Pgt - E expression
- T term
- F Factor
- where P is
- E ? E T E T T
- T ? T F T / F F
- F ? 0 1 9 ( E )
45Parse Trees
- Ex. 2 3 (4 5) 6 / 7
- G3 is unambiguous
46An Ambiguous Expression Grammar
- ltexprgt ? ltexprgt ltopgt ltexprgt const
- ltopgt ? / -
ltexprgt
ltexprgt
ltexprgt
ltexprgt
ltexprgt
ltexprgt
ltopgt
ltopgt
ltopgt
ltexprgt
ltexprgt
ltexprgt
ltexprgt
ltopgt
ltopgt
const
const
const
const
const
const
-
-
/
/
47An Unambiguous Expression Grammar
- If we use the parse tree to indicate precedence
levels of the operators, we cannot have ambiguity - ltexprgt ? ltexprgt - lttermgt lttermgt
- lttermgt ? lttermgt / const const
48Associativity of Operators
- Operator associativity can also be indicated by a
grammar - ltexprgt -gt ltexprgt ltexprgt const (ambiguous)
- ltexprgt -gt ltexprgt const const (unambiguous)
ltexprgt
ltexprgt
ltexprgt
const
ltexprgt
const
const
49Parse Trees
- 2 3 4 parse tree syntax tree
50Parse Trees
- Parser can skip the parse tree step
- We are going straight from the parser to the value
51Limitations of BNF
- No easy way to impose length limitations, such as
maximum length of variable names - No way to impose distributed requirements, such
as, a variable must be declared before it is used - Describes only syntax, not semantics
- Nothing clearly better has been devised
52Extended BNF
- Optional parts are placed in brackets
- ltproc_callgt -gt ident (ltexpr_listgt)
- Alternative parts of RHSs are placed inside
parentheses and separated via vertical bars - lttermgt ? lttermgt (-) const
- Repetitions (0 or more) are placed inside braces
- ltidentgt ? letter letterdigit
53BNF and EBNF
- BNF
- ltexprgt ? ltexprgt lttermgt
- ltexprgt - lttermgt
- lttermgt
- lttermgt ? lttermgt ltfactorgt
- lttermgt / ltfactorgt
- ltfactorgt
- EBNF
- ltexprgt ? lttermgt ( -) lttermgt
- lttermgt ? ltfactorgt ( /) ltfactorgt
54Attribute Grammars
- Context-free grammars (CFGs) cannot describe all
of the syntax of programming languages - Additions to CFGs to carry some semantic info
along parse trees - Primary value of attribute grammars (AGs)
- Static semantics specification
- Compiler design (static semantics checking)
55Attribute Grammars Definition
- An attribute grammar is a context-free grammar G
(N, T, S, P) with the following additions - For each grammar symbol x there is a set A(x) of
attribute values - Each rule has a set of functions that define
certain attributes of the nonterminals in the
rule - Each rule has a (possibly empty) set of
predicates to check for attribute consistency
56Attribute Grammars Definition
- Let X0 ? X1 ... Xn be a rule
- Functions of the form S(X0) f(A(X1), ... ,
A(Xn)) define synthesized attributes - Functions of the form I(Xj) f(A(X0), ... ,
A(Xn)), for i lt j lt n, define inherited
attributes - Initially, there are intrinsic attributes on the
leaves
57Attribute Grammars An Example
- Syntax
- ltassigngt -gt ltvargt ltexprgt
- ltexprgt -gt ltvargt ltvargt ltvargt
- ltvargt -gt A B C
- actual_type synthesized for ltvargt and ltexprgt
- expected_type inherited for ltexprgt
58Attribute Grammar
- Syntax rule ltexprgt ? ltvargt1ltvargt2
- Semantic rules
- ltexprgt.actual_type?ltvargt1.actual_type
- Predicate
- ltvargt1.actual_type ltvargt2.actual_type
- ltexprgt.expected_type ltexprgt.actual_type
- Syntax rule ltvargt ? id
- Semantic rule
- ltvargt.actual_type ? lookup (ltvargt.string)
59Attribute Grammars
- How are attribute values computed?
- If all attributes were inherited, the tree could
be decorated in top-down order. - If all attributes were synthesized, the tree
could be decorated in bottom-up order. - In many cases, both kinds of attributes are used,
and it is some combination of top-down and
bottom-up that must be used.
60Attribute Grammars
- ltexprgt.expected_type ? inherited from parent
- ltvargt1.actual_type ? lookup (A)
- ltvargt2.actual_type ? lookup (B)
- ltvargt1.actual_type ? ltvargt2.actual_type
- ltexprgt.actual_type ? ltvargt1.actual_type
- ltexprgt.actual_type ? ltexprgt.expected_type
61Semantics
- There is no single widely acceptable notation or
formalism for describing semantics - Operational Semantics
- Describe the meaning of a program by executing
its statements on a machine, either simulated or
actual. The change in the state of the machine
(memory, registers, etc.) defines the meaning of
the statement
62Operational Semantics
- To use operational semantics for a high-level
language, a virtual machine is needed - A hardware pure interpreter would be too
expensive - A software pure interpreter also has problems
- The detailed characteristics of the particular
computer would make actions difficult to
understand - Such a semantic definition would be machine-
dependent
63Operational Semantics
- A better alternative A complete computer
simulation - The process
- Build a translator (translates source code to the
machine code of an idealized computer) - Build a simulator for the idealized computer
- Evaluation of operational semantics
- Good if used informally (language manuals, etc.)
- Extremely complex if used formally (e.g., VDL),
it was used for describing semantics of PL/I.
64Axiomatic Semantics
- Based on formal logic (predicate calculus)
- Original purpose formal program verification
- Axioms or inference rules are defined for each
statement type in the language (to allow
transformations of expressions to other
expressions) - The expressions are called assertions
65Axiomatic Semantics
- An assertion before a statement (a precondition)
states the relationships and constraints among
variables that are true at that point in
execution - An assertion following a statement is a
postcondition - A weakest precondition is the least restrictive
precondition that will guarantee the postcondition
66Axiomatic Semantics Form
- Pre-, post form P statement Q
- An example
- a b 1 a gt 1
- One possible precondition b gt 10
- Weakest precondition b gt 0
67Program Proof Process
- The postcondition for the entire program is the
desired result - Work back through the program to the first
statement. If the precondition on the first
statement is the same as the program
specification, the program is correct.
68Axiomatic Semantics Axioms
- An axiom for assignment statements (x E)
Qx-gtE x E Q - The Rule of Consequence
69Axiomatic Semantics Axioms
- An inference rule for sequences
- P1 S1 P2
- P2 S2 P3
70Axiomatic Semantics Axioms
- An inference rule for logical pretest loops
-
- P while B do S end Q
-
-
- where I is the loop invariant (the inductive
hypothesis)
71Axiomatic Semantics Axioms
- Characteristics of the loop invariant I must
meet the following conditions - P gt I -- the loop invariant must be true
initially - I B I -- evaluation of the Boolean must not
change the validity of I - I and B S I -- I is not changed by executing
the body of the loop - (I and (not B)) gt Q -- if I is true and B is
false, is implied - The loop terminates
72Loop Invariant
- The loop invariant I is a weakened version of the
loop postcondition, and it is also a
precondition. - I must be weak enough to be satisfied prior to
the beginning of the loop, but when combined with
the loop exit condition, it must be strong enough
to force the truth of the postcondition
73Evaluation of Axiomatic Semantics
- Developing axioms or inference rules for all of
the statements in a language is difficult - It is a good tool for correctness proofs, and an
excellent framework for reasoning about
programs, but it is not as useful for language
users and compiler writers - Its usefulness in describing the meaning of a
programming language is limited for language
users or compiler writers
74Denotational Semantics
- Based on recursive function theory
- The most abstract semantics description method
- Originally developed by Scott and Strachey (1970)
75Denotational Semantics
- The process of building a denotational
specification for a language Define a
mathematical object for each language entity - Define a function that maps instances of the
language entities onto instances of the
corresponding mathematical objects - The meaning of language constructs are defined by
only the values of the program's variables
76Denotation Semantics vs Operational Semantics
- In operational semantics, the state changes are
defined by coded algorithms - In denotational semantics, the state changes are
defined by rigorous mathematical functions
77Denotational Semantics Program State
- The state of a program is the values of all its
current variables - s lti1, v1gt, lti2, v2gt, , ltin, vngt
- Let VARMAP be a function that, when given a
variable name and a state, returns the current
value of the variable - VARMAP(ij, s) vj
78Decimal Numbers
- ltdec_numgt ? 0 1 2 3 4 5 6 7 8
9 ltdec_numgt (0 1 2 3 4
5 6 7 8 9) - Mdec('0') 0, Mdec ('1') 1, , Mdec ('9')
9 - Mdec (ltdec_numgt '0') 10 Mdec (ltdec_numgt)
- Mdec (ltdec_numgt '1) 10 Mdec (ltdec_numgt) 1
-
- Mdec (ltdec_numgt '9') 10 Mdec (ltdec_numgt) 9
79Expressions
- Map expressions onto Z ? error
- We assume expressions are decimal numbers,
variables, or binary expressions having one
arithmetic operator and two operands, each of
which can be an expression
80Semantics
- Me(ltexprgt, s) ?
- case ltexprgt of
- ltdec_numgt gt Mdec(ltdec_numgt, s)
- ltvargt gt
- if VARMAP(ltvargt, s) undef
- then error
- else VARMAP(ltvargt, s)
- ltbinary_exprgt gt
- if (Me(ltbinary_exprgt.ltleft_exprgt, s)
undef - OR Me(ltbinary_exprgt.ltright_exprgt,
s) - undef)
- then error
- else
- if (ltbinary_exprgt.ltoperatorgt then
- Me(ltbinary_exprgt.ltleft_exprgt, s)
- Me(ltbinary_exprgt.ltright_exprgt, s)
- else Me(ltbinary_exprgt.ltleft_exprgt, s)
- Me(ltbinary_exprgt.ltright_exprgt, s)
- ...
81Assignment Statements
- Maps state sets to state sets
- Ma(x E, s) ?
- if Me(E, s) error
- then error
- else s lti1,v1gt,lti2,v2gt,...,
ltin,vngt, - where for j 1, 2, ..., n,
- vj VARMAP(ij, s) if ij ltgt x
- Me(E, s) if ij x
82 Logical Pretest Loops
- Maps state sets to state sets
- Ml(while B do L, s) ?
- if Mb(B, s) undef
- then error
- else if Mb(B, s) false
- then s
- else if Msl(L, s) error
- then error
- else Ml(while B do L, Msl(L, s))
83Loop Meaning
- The meaning of the loop is the value of the
program variables after the statements in the
loop have been executed the prescribed number of
times, assuming there have been no errors - In essence, the loop has been converted from
iteration to recursion, where the recursive
control is mathematically defined by other
recursive state mapping functions - Recursion, when compared to iteration, is easier
to describe with mathematical rigor
84Evaluation of Denotational Semantics
- Can be used to prove the correctness of programs
- Provides a rigorous way to think about programs
- Can be an aid to language design
- Has been used in compiler generation systems
- Because of its complexity, they are of little use
to language users