MELJUN CORTES Programming languages syntax and semantics
-
Upload
meljun-cortes -
Category
Technology
-
view
21 -
download
0
Transcript of MELJUN CORTES Programming languages syntax and semantics
Syntax and Semantics
Programming Languages
* Property of STI
Page 1 of 22
Syntax and Semantics
The syntax of a programming language is
the form of its expressions, statements,
and program units.
Its semantics is the meaning of those
expressions, statements, and program
units.
Syntax and Semantics
Programming Languages
* Property of STI
Page 2 of 22
What are Languages?
sets of strings of characters from some
alphabet
defined by a set of rules specifying which
strings of characters from the language’s
alphabet are in the language
Syntax and Semantics
Programming Languages
* Property of STI
Page 3 of 22
Language Components
Lexeme
the lowest level syntactic unit that includes
the language’s identifiers, literals,
operators, and special words
Token
category of its lexemes
Example:
index = 2 * count + 17;
The lexemes and tokens of this statement are:
Lexemes Tokens
index identifier
= equal_sign
2 int_literal
* mult_op
count identifier
+ plus_op
17 int_literal
; semicolon
Syntax and Semantics
Programming Languages
* Property of STI
Page 4 of 22
Backus-Naur Form (BNF)
In 1956, Noam Chomsky introduced
context free grammars. He described four
classes of generative devices or grammars
that define four classes of languages.
BNF began as an abbreviation of Backus
Normal Form. It was later changed to
Backus-Norm Form to recognize Peter
Naur’s contribution as editor of the ALGOL
60 report.
Syntax and Semantics
Programming Languages
* Property of STI
Page 5 of 22
Example:
list list + digit
The LHS of the example which is list is
called an abstraction.
Abstractions in a grammar are often called
nonterminal symbols or simply nonterminals.
The RHS which is list + digit is called the
definition of the LHS. It consists of a
combination of tokens, lexemes, and
references to other abstractions which are
all called terminal symbols or simply
terminals.
Altogether, the example is called a
production or rule.
Syntax and Semantics
Programming Languages
* Property of STI
Page 6 of 22
Nonterminal Symbols
These symbols can have two or more
distinct definitions, representing two or
more possible syntactic forms in the
language.
Example:
list list + digit
list list – digit
which can be rewritten as:
list list + digit | list - digit
Syntax and Semantics
Programming Languages
* Property of STI
Page 7 of 22
Recursive Rule
A rule is said to be recursive if its LHS
appears in its RHS, as in the following
example:
list list * digit | list | digit
This defines list as either a product or as a
quotient of a list and a digit.
Syntax and Semantics
Programming Languages
* Property of STI
Page 8 of 22
Grammars and
Derivations
How are sentences in the language formed?
A sequence of rules are being applied to
generate the desired sentence. This
sentence generation is called derivation.
Consider the following grammar:
list list + digit
list list - digit
list digit
digit 0|1|2|3|4|5|6|7|8|9
list is known as the start symbol
Syntax and Semantics
Programming Languages
* Property of STI
Page 9 of 22
Sentence Derivation
Using the grammar given in the previous
slide, a derivation of the program is as
follows:
list list + digit
list list – digit + digit
list digit – digit + digit
list 9 – 5 + 2
Syntax and Semantics
Programming Languages
* Property of STI
Page 10 of 22
Parse Trees
Hierarchical structures of the sentences
defined by a language
Graphically show how the start symbol of a
grammar derives a string in the language
Given a context-free grammar, a parse tree
has the following properties:
The root is labeled by the start symbol.
Each leaf is labeled with a terminal symbol.
Each internal node is labeled by a non-
terminal.
Every subtree of a parse tree describes one
instance of an abstraction in the statement.
Syntax and Semantics
Programming Languages
* Property of STI
Page 11 of 22
Ambiguous Grammars
A grammar that generates a sentence for
which there are two or more distinct parse
trees is said to be ambiguous.
Example: Generate ‘9-5+2’
We can represent the expression using two
parse trees from an ambiguous grammar.
string
string string+
string string-
9 5
2
string
string string-
9 string string+
5 2
Syntax and Semantics
Programming Languages
* Property of STI
Page 12 of 22
Associativity of
Operators
Operators for addition, subtraction,
multiplication, and division are all left
associative while the assignment operator
is right associative.
string
string string+
string string+
9 5
2
right
letter right=
x letter letter=
y z
Syntax and Semantics
Programming Languages
* Property of STI
Page 13 of 22
Extended BNF or EBNF
Brackets – to delimit an optional part of a
RHS.
<selection> if (<expression>)
<statement> [else <statement>];
Braces – to indicate that the enclosed part
of the statement can be repeated
indefinitely, or left out altogether.
<ident_list> <identifier> {, <identifier>}
Parenthesis – to indicate options separated
by the OR operator.
<for_stmt> for <var> := <expr> (to |
downto) <expr> do <stmt>
Syntax and Semantics
Programming Languages
* Property of STI
Page 14 of 22
Syntax Graphs
A graph is a collection of nodes, some of
which are connected by lines, called edges.
A directed graph is one which the edges are
directional; that is, they have arrowheads
on one end to indicate a direction.
Syntax graphs use rectangular nodes for
non-terminals while ellipses or circle nodes
for terminal symbols.
Syntax and Semantics
Programming Languages
* Property of STI
Page 15 of 22
Attribute Grammars
device used to describe more of the
structure of a programming language than
is possible with a context-free grammar
extension to a context-free grammar
the extension allows certain language rules
to be described, such as type compatibility
Syntax and Semantics
Programming Languages
* Property of STI
Page 16 of 22
Language Semantics
Static Semantics
Only indirectly related to the meaning of
programs during execution; rather, these
have to do with the legal forms of program
Dynamic Semantics
The meaning of the expressions,
statements, and units of programs
Provide valuable information to
programmers and compiler writers
Syntax and Semantics
Programming Languages
* Property of STI
Page 17 of 22
Operational Semantics
With operational semantics, describing the
meaning of a program is done by executing
its statements on a machine (real or
simulated).
The changes that occur in the machine’s
state when it executes a given statement
define the meaning of that statement.
A translator is needed to convert the
statements in a programming language,
say L to a particular low-level language.
A virtual machine is needed for the low-
level language derived.
Syntax and Semantics
Programming Languages
* Property of STI
Page 18 of 22
Axiomatic Semantics
Defined in conjunction with the
development of a method to prove the
correctness of programs.
The proof is composed of statements of a
program each of which are preceded and
followed by a logical expression.
The logical expression specifies constraints
on the meaning of the statement. The
notation used to describe constraints is
predicate calculus.
Syntax and Semantics
Programming Languages
* Property of STI
Page 19 of 22
Assertions
Assertions or predicates are logical
expressions based on mathematical logic.
Two types:
Precondition describes the constraints on
the program variables at that point in the
program. It immediately precedes the
program statement which contains the
variables.
Postcondition immediately follows the
program statement. It describes the new
constraints on the variables in that
statement after execution.
Syntax and Semantics
Programming Languages
* Property of STI
Page 20 of 22
Weakest Preconditions
The weakest precondition is the least
restrictive precondition that will guarantee
the validity of the associated postcondition.
It can be computed either by axioms or
inference rule.
An axiom is a logical statement that is
assumed to be true.
An inference rule is a method of inferring
the truth of one assertion on the basis of
the values of other assertion.
Syntax and Semantics
Programming Languages
* Property of STI
Page 21 of 22
Weakest Preconditions
Example 1: In the statement and
postcondition, {x > 10}, {x > 50}, and {x >
1000} are all valid preconditions. The
weakest of all preconditions in this case is
{x > 10}.
Example 2: The weakest precondition is
computed by substituting y/2-1 in the
assertion {x > 5}, as follows:
Y * 2 – 1 < 5
Y < 3
Thus, the weakest precondition for the
assignment statement is {y < 3}.
Syntax and Semantics
Programming Languages
* Property of STI
Page 22 of 22
Denotational Semantics
Denotational semantics is the most
rigorous widely known method for
describing the meaning of programs.
It is solidly based on recursive function
theory.
Its fundamental concept is to define for
each language entity both a mathematical
object and a function that maps instances
of that entity onto instances of the
mathematical object.