syntax semantics
TRANSCRIPT
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 1/43
05:39
ICS 32Structure of Prog. Languages
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 2/43
05:39
Describing Syntax andSemantics
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 3/43
05:39
Syntax & Semantics
• What we need: concise yetunderstandable description of aprogramming language
• implementers must understand:
– how the statements are formed
– what they mean
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 4/43
05:39
Syntax & Semantics
• need for a language reference manual
• Syntax : form of its expressions,statements and program units
• Semantics : meaning of those expressionsand program units
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 5/43
05:39
Syntax & Semantics
• Formal Methods of Describing Syntax
– Grammars
– Syntax Diagrams
– Parse Trees
– metalanguage : language used to describeanother language
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 6/43
05:39
Describing Syntax
• Grammar
– constituted by a set of tokens, terminals, non-terminals, productions, and the goal symbol
– describe the hierarchical syntactic structure ofthe sentence of language
– BNF (Backus-Naur Form): one way of writing
grammars
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 7/43
05:39
Describing Syntax
• Backus-Naur Form (BNF)
– method of describing syntax
– originally presented by John Backus (todescribe ALGOL 58)and later modified byPeter Naur
– similar to Chomsky’s Context-free Diagrams
(1950)
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 8/43
05:39
Describing Syntax
• Parts of a Grammar
– a set of tokens
– a set of non-terminal symbols
– a set of rules called productions
– a goal symbol
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 9/43
05:39
Describing Syntax
• Grammar:
– lexemes : small syntactic units (lexicalspecifications)
• e.g. identifiers, constants, operators, and specialwords
– language statements : string of lexemes
– token : a category of lexemes
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 10/43
05:39
Describing Syntax
• Grammar: Symbols
– Goal Symbol
• one of the set of non-terminal symbols
• also referred to as the start symbol
– Terminal Symbols
• symbols that are atomic / non-divisible
• can be combined to form valid constructs in thelanguage
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 11/43
05:39
Describing Syntax
• Grammar: Symbols
– Non-Terminal Symbols
• symbols used to represent intermediate definitions
within the language• defined by productions
• syntactic classes or categories
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 12/43
05:39
Describing Syntax
• Grammar: Productions
– BNF uses abstraction for syntactic structures
<assign> <var>:=<expression>
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 13/43
05:39
Describing Syntax
• Grammar: Productions
– LHS: abstraction being defined
– RHS: tokens, lexemes, references to otherabstractions
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 14/43
05:39
Describing Syntax
• Grammar: Productions
– a definition of a non-terminal symbol
– has the form
x y
where x is a non-terminal symbol and y is asequence of symbols (non-terminal or
terminal)
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 15/43
05:39
Describing Syntax
• Grammar: Derivation
– sentences of the language are generatedthrough repeated application of the rules,
beginning with a start symbol
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 16/43
05:39
Describing Syntax
• Grammar Production Example
<if_stmt> if <logic_expr> then
<statement_list>
else
<statement_list>
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 17/43
05:39
Describing Syntax
• Rules to form Grammar
– every non-terminal symbol must appear to theleft of the at least one production
– the goal symbol must not appear to the rightof the of any production
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 18/43
05:39
Describing Syntax
• Rules to form Grammar
– a rule is recursive if its LHS appears in itsRHS
<id_list> <identifier>
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 19/43
05:39
Describing Syntax
• Given: Grammar Productions
<program> begin <stmt_list> end
<stmt_list> <stmt> | <stmt> <stmt_list>
<stmt> <var> := <expression>
<var> A | B | C
<expression> <var> + <var>
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 20/43
05:39
Describing Syntax
• Sample Derivation
<program> begin <stmt_list> end
begin <stmt> end
begin <var> := <expression> end
begin <var> := <var> + <var> end
begin A := B + C end
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 21/43
05:39
Describing Syntax
• When does derivation stop?
– by exhaustingly choosing all combinations ofchoices, the entire language can generate
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 22/43
05:39
Describing Syntax
• Syntax Diagrams
– a.k.a. Syntax Graphs / Syntax Charts
– represented by a directed graph
– a separate graph is used for each syntacticunit
– Rectangles: Non-terminal Symbols
– Circles: Terminal Symbols
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 23/43
05:39
Calculator
<expr>
Describing Syntax
• Syntax Diagrams
<expr>
<val>
<opr> <expr>
=
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 24/43
05:39
<val><unsgn>
<sgn> . <unsgn>
Describing Syntax
• Syntax Diagrams
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 25/43
05:39
<unsgn>
<unsgn>
<digit>
Describing Syntax
• Syntax Diagrams
<sign> +
-
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 26/43
05:39
<digit>
0 1 2 3 4 5 .. 9
Describing Syntax
• Syntax Diagrams
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 27/43
05:39
<opr>
+
-
*
/
Describing Syntax
• Syntax Diagrams
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 28/43
05:39
Describing Syntax
• Parse Trees
– parse trees: hierarchical structure
– node: non-terminal symbol
– leaves: terminal symbol
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 29/43
05:39
<program>
begin end<stmt_list>
<stmt>
<var> <expr>
<var> <var>
Derivation of begin A := B + C end
+
:=
A
B C
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 30/43
05:39
Describing Syntax
• Why do we need to describe syntax?
– to express our ideas to a form understandableby the computer
– be acquainted with rules
– transformation/translation
– comparison with other languages
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 31/43
05:39
Describing Semantics
• Informal Description
• Attribute Grammars
• Operational Semantics• Axiomatic Semantics
• Denotational Semantics
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 32/43
05:39
Describing Semantics
• Informal Description
– uses informal neutral language descriptions
– construed from English language description
– example: when an integer and a double are added, the result is a double
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 33/43
05:39
Describing Semantics
• Attribute Grammars – the CFG (Context Free Grammar) for the
language is augmented with a set of attributes
and rules for computing those attributes – describe the syntax and static semantics
– Static Semantics: that part of its semanticswhich can be determined without executing its
code
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 34/43
05:39
Describing Semantics
• Attribute Grammar
– grammars with sets of attribute valuesassociated with a grammar symbol
– attribute computation functions -- howattribute values are computed
– predicate functions -- state some of the syntax
and semantic rules
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 35/43
05:39
Describing Semantics
• Example of Attribute Grammar
expr term
expr expr ‘+’ term
$$ $1 $2 $3
some attributes of symbols are:t = type
v = values = scope
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 36/43
05:39
Describing Semantics
to compute the v attribute of $$ the attributegrammar might be the following rule:%($1.t == $3.t){
switch ($1.t) {case INTEGER: $$.t = INTEGER;
$$.v = $1.v.INTEGER + $3.v.INTEGER;
break;
case DOUBLE: ...}
}
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 37/43
05:39
Describing Semantics
• Operational Semantics
– the semantics of a program language isdescribed by executing its statements on a
machine, either real or simulated – the changes that occur in the machine’s state
when it executes a given statement define themeaning of that statement
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 38/43
05:39
Describing Semantics
• Example of Operational Semantics
Operational Semantics
expr1;
loop: if expr2 = 0 goto out
…
expr3;
goto loop
out: …
C Statementfor (expr1; expr2; expr3){
…
}
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 39/43
05:39
Describing Semantics
• Axiomatic Semantics
– prove the correctness of programs
– assertions: preconditions and post-conditions
– only for very simple statements
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 40/43
05:39
Describing Semantics
• Example of Axiomatic Semantics
{P} S {Q}
where P is the Precondition
S is the statement to be defined
Q is the Postcondition
{x>=5} sum = 2 * x + 1 {sum >=11}
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 41/43
05:39
Describing Semantics
• Denotational Semantics
– describe the language by execution
– define a mathematical object for each entity
– define a function that maps instances of theentity to the instances of mathematical objects
8/3/2019 Syntax Semantics
http://slidepdf.com/reader/full/syntax-semantics 42/43
05:39
Describing Semantics
• Example of Denotational Semantics<num> ::= <num><digit> | <digit>
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
V(‘0‘) = 0 V(‘1’) = 1
V(‘9‘) = 9
V(<num><digit>) = V(<num>)*10 + V(<digit>)