Languages
05:39
05:39
05:39
05:39
Describing Syntax
Grammar
constituted by a set of tokens, terminals, nonterminals, productions, and the goal symbol describe the hierarchical syntactic structure of the sentence of language BNF (Backus-Naur Form): one way of writing grammars
05:39
Describing Syntax
Backus-Naur Form (BNF)
method of describing syntax originally presented by John Backus (to describe ALGOL 58)and later modified by Peter Naur similar to Chomskys Context-free Diagrams (1950)
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
05:39
Describing Syntax
Grammar:
lexemes: small syntactic units (lexical specifications)
e.g. identifiers, constants, operators, and special words
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 the language
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
05:39
Describing Syntax
Grammar: Productions
BNF uses abstraction for syntactic structures <assign> <var>:=<expression>
05:39
Describing Syntax
Grammar: Productions
LHS: abstraction being defined RHS: tokens, lexemes, references to other abstractions
05:39
Describing Syntax
Grammar: Productions
a definition of a non-terminal symbol has the form xy where x is a non-terminal symbol and y is a sequence of symbols (non-terminal or terminal)
05:39
Describing Syntax
Grammar: Derivation
sentences of the language are generated through repeated application of the rules, beginning with a start symbol
05:39
Describing Syntax
Grammar Production Example
<if_stmt> if <logic_expr> then <statement_list> else <statement_list>
05:39
Describing Syntax
Rules to form Grammar
every non-terminal symbol must appear to the left of the at least one production the goal symbol must not appear to the right of the of any production
05:39
Describing Syntax
Rules to form Grammar
a rule is recursive if its LHS appears in its RHS <id_list> <identifier>
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>
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
05:39
Describing Syntax
When does derivation stop?
by exhaustingly choosing all combinations of choices, the entire language can generate
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 syntactic unit Rectangles: Non-terminal Symbols Circles: Terminal Symbols
05:39
Describing Syntax
Syntax Diagrams
Calculator
<expr>
<expr>
<val> <opr>
05:39
<expr>
Describing Syntax
Syntax Diagrams
<val>
<unsgn>
<sgn>
<unsgn>
05:39
Describing Syntax
Syntax Diagrams
<unsgn>
<digit> <unsgn>
<sign>
+
05:39
Describing Syntax
Syntax Diagrams
<digit> 0 1 2 3 4 5 .. 9
05:39
Describing Syntax
Syntax Diagrams
<opr> + -
*
/
05:39
Describing Syntax
Parse Trees
parse trees: hierarchical structure node: non-terminal symbol leaves: terminal symbol
05:39
<var>
A
:=
<var> B
<expr>
+ <var> C
Derivation of
05:39
begin A := B + C end
Describing Syntax
Why do we need to describe syntax?
to express our ideas to a form understandable by the computer be acquainted with rules transformation/translation comparison with other languages
05:39
Describing Semantics
Informal Description Attribute Grammars Operational Semantics Axiomatic Semantics Denotational Semantics
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
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 semantics which can be determined without executing its code
05:39
Describing Semantics
Attribute Grammar
grammars with sets of attribute values associated with a grammar symbol attribute computation functions -- how attribute values are computed predicate functions -- state some of the syntax and semantic rules
05:39
Describing Semantics
Example of Attribute Grammar
expr term expr expr + term $$ $1 $2 $3 some attributes of symbols are: t = type v = value s = scope
05:39
Describing Semantics
to compute the v attribute of $$ the attribute grammar 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: ... } }
05:39
Describing Semantics
Operational Semantics
the semantics of a program language is described by executing its statements on a machine, either real or simulated the changes that occur in the machines state when it executes a given statement define the meaning of that statement
05:39
Describing Semantics
Example of Operational Semantics
C Statement for (expr1; expr2; expr3) { } Operational Semantics expr1; loop: if expr2 = 0 goto out expr3; goto loop out:
05:39
Describing Semantics
Axiomatic Semantics
prove the correctness of programs assertions: preconditions and post-conditions only for very simple statements
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}
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 the entity to the instances of mathematical objects
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>)
05:39
Describing Semantics
Why do we need to describe semantics?
understanding programs transformation/translation proving correctness comparison with other languages
05:39