Professional Documents
Culture Documents
CSC 305: Programming Paradigm: Introduction To Language, Syntax and Semantics
CSC 305: Programming Paradigm: Introduction To Language, Syntax and Semantics
PROGRAMMING
PARADIGM
CHAPTER 2:
Introduction to Language,
Syntax and Semantics
Contents
Describing languages
Sentences,
Describing Syntax
Language
Recognizers, Generators,
Grammars
Describing Semantics
Operational,
Axiomatic, Denotation
Languages
Method of communication
Spoken and written languages can be described
as a system of symbols (sometimes known as lexemes)
and the grammars (rules) by which the symbols
are manipulated.
.. is a set of Sentences
Sentences is a string of characters over some
alphabet
Programming language is ..
..a system of signs used to communicate a
task/algorithm to a computer, causing the
task to be performed. The task to be
performed is called computation , which
follows absolutely precise and
unambiguous rules.
Programming Language
Example of a program that adds two integers
and prints: 1 + 1 = 2
#include <stdio.h>
int add(int x, int y)
{
return x + y;
}
int main(void)
Syntax for main ()
{
function
int foo = 1, bar = 1;
printf("%d + %d = %d\n", foo, bar, add(foo, bar));
return 0;
}
Lexeme
Example :
{
return x + y;
}
1
2
3
4
5
6
7
{
return
x
+
y
;
}
Lexeme
Token
..is a category of lexemes
Example :
{
return x + y;
}
1
2
3
4
5
6
7
{
return
x
+
y
;
}
Lexeme
1
2
3
4
5
6
7
open
keyword
identifier
plus op
identifier
separator
close
Tokens
Language Recognizers
Determines whether given programs are in
the language and syntactically correct.
Example : Compiler
Syntax analyzer is part of compiler.
Also known as parser.
Compiler
Program that
converts entire
source program
into machine
language before
executing it
Compiler process
Source
code
Lexical
Analyzer
Tokenized
code
Syntactic
Analyzer
Parsed
code
Object
code
Semantic
Analyzer
Qualified
code
Code
Generator
optimizer
Final
code
Interpreter
Program that
translates and
executes one
program code
statement at
a time
Does not produce
object program
Language Generators
To generate the sentences of a language
Comparing with the structure of the
generator.
Formal methods for describing syntax is:
Grammars
Grammars
Describe the syntax of programming
language.
Backus-Naur Form and Context-Free
Develop by Noam Chomsky and John
Backus
Grammar classes :
Context-free
grammars Whole PL
Regular grammars Tokens of PL
Context-Free grammars
Regular grammars
Is a formal grammars.
The two main categories of formal
grammar:
generative
Classification of grammars
0 grammar (unrestricted)
Type 1 grammar (context-sensitive)
Type 2 grammar (context free grammar)
Type 3 grammar (regular)
Type
grammar
(context-sensitive)
A formal1grammar
G = (N, ,
P, S) is context-sensitive if
all rules in P are of the form
A
The name context-sensitive is explained by the and
that form the context of A and determine whether A
can be replaced with or not. This is different from a
context-free grammar where the context of a
nonterminal is not taken into consideration.
Generated
Grammar
<program> begin <stmt_list> end
<stmt_list> <stmt> | <stmt>;<stmt_list>
<stmt> <var> = <expression>
<var> A | B| C
<expression>
<var> + <var>
| <var> - <var>
| <var>
A program consist of the special word begin followed by a list
of statements separated by semicolons followed by the special
word end
An expression is either single or two variables separated by
either + or operator. The only variable name is A, B and C
Grammar Example
A = B * (A + C)
<assign>
=>
=>
=>
=>
=>
=>
=>
=>
=>
<id> = <expr>
A = <expr>
A = <id> * <expr>
A = B * <expr>
A = B * (<expr>)
A = B * ( <id> + <expr>)
A = B * ( A + <expr>)
A = B * ( A + <id>)
A = B * ( A + C )
BNF
Fundamentals:
BNF
is a metalanguage
Example of BNF
<postal-address> ::=
Example of BNF
<street-address> ::=
EBNF
Drawback of BNF.
Increase the readability and writability of
the production rules.
New notations which are:
Braces
Parse Tree
<id>
A
<expr>
<id>
<expr>
<expr>
<id>
<expr>
<id>
C
Semantics
The meaning of words and other parts of
languages.
Reveal the meaning of the
syntax/grammar
Categorized as follow:
Static
semantics
Dynamic semantics
Static semantics
Attribute Grammar
A = A + B
Syntax rules : <expr> <var>[2] + <var> [3]
Semantics rules
:
<expr>.actual_type
if
(<var>[2].actual_type = int) and
(<var>[3].actual_type = int)
then
int
else
real
end if
Predicate
: <expr>.actual_type == <expr>.expected_type
Dynamic semantics
Done during the run time.
Several ways to specify DS which is:
By
By
By
a formal definition
Operational semantics
Example
The
while (expression)
Statement;
Axiomatic semantics
Example
Logical
Pre-condition
Post-condition
x = y , y = m
Pre condition
X = 5 , y = 7
z = x; x = y; y = z;
y = n , x = m
Program statement
Post-condition
X = 7 , y = 5
Denotational semantics
Example
Define