Title: Describing Syntax and Semantics Chapter 3
1Describing Syntaxand SemanticsChapter 3
2Describing Syntaxand Semantics
SYNTAX - the form of the expressions, statements
and program units in a programming language
SEMANTICS - the meaning of those expressions,
statements and program units
3Describing Syntax
The strings of a language are called sentences or
statements
The lowest level syntactic units of a programming
language are called lexemes. (identifiers,
constants, operators, etc.)
A token is a category of lexemes.
4Describing Syntax
index 2 count 17
LEXEME TOKEN index identifier equal_sig
n 2 int_constant mult_op count identifie
r plus_op 17 int_constant semicolon
5Describing Syntax
Languages may be formally defined by one of two
methods recognition or generation.
6Describing Syntax
Given a language, L, that uses the alphabet 3, a
recognizer will indicate if the given string of
characters from 3 is in the language L.
A compiler is an example of a recognizer.
7Describing Syntax
A generator is a device which can be used to
generate sentences or statements of a language.
Useful for humans as a guide for the generation
of valid statements in the language.
A grammar is an example of a generator.
8Describing Syntax
Noam Chomsky, linguist, described four language
generation devices, called grammars, that can be
used to generate four different classes of
languages.
Regular grammars - used to describe tokens
Context-free grammars - used to describe whole
programming languages
9Describing Syntax
John Backus, introduced a new formal notation for
describing a programming language syntax. (Algol
58) The notation was modified by Peter
Naur. Backus-Naur form (BNF)
10Describing Syntax
Meta language - a language that can be used to
describe another language BNF is a meta language
for the description of programming languages.
11Describing Syntax
BNF uses abstractions to denote various syntactic
structures. ltassigngt is an abstraction which
denotes a valid assignment statement
12Describing Syntax
BNF uses rules or productions to describe valid
programs, statements and expressions within the
language. ltassigngt 6 ltvargt ltexpressiongt is
a rule which describes a valid assignment
statement in Pascal
13Describing Syntax
GRAMMAR - a set of rules or productions which
describe a language
RULE or PRODUCTION- describes valid programs,
statements and expressions within the language.
NON-TERMINAL - an abstraction which can be
expanded by the application of some rule
TERMINAL - corresponds to lexemes or tokens of
the language
14Describing Syntax
BNF rule or production ltassigngt 6 ltvargt
ltexpressiongt
The non-terminal on the LHS may be replaced with
the string of terminals and non-terminals on the
RHS
15Describing Syntax
BNF rule or production ltidgt 6 A B C
16Describing Syntax
BNF rules or productions for lists ltid_listgt 6
ltidgt ltidgt, ltid_listgt
Recursion is used to describe rules for variable
length lists. A rule is recursive if its LHS
also appears in its RHS
17Describing Syntax
Derivations Sentences in a language may be
generated through a sequence of applications of
the rules, beginning with a start symbol.
Parse Trees The hierarchical syntactic
structure of a sentence in the language can be
described with a parse tree.
18Describing Syntax
Example 3.1
ltprogramgt 6 begin ltstmt_listgt
end ltstmt_listgt 6 ltstmtgt ltstmtgt
ltstmt_listgt ltstmtgt 6 ltvargt
ltexpressiongt ltvargt 6 A B
C ltexpressiongt 6 ltvargt ltvargt
ltvargt - ltvargt ltvargt
19Grammar for Simple Assignment Statements
Example 3.2
ltassigngt 6 ltidgt ltexprgt ltidgt 6 A B
C ltexprgt 6 ltidgt ltexprgt ltidgt
ltexprgt ( ltexprgt ) ltidgt
20An Ambiguous Grammar for Simple Assignment
Statements
Example 3.3
ltassigngt 6 ltidgt ltexprgt ltidgt 6 A B
C ltexprgt 6 ltexprgt ltexprgt ltexprgt
ltexprgt ( ltexprgt ) ltidgt
21More on RECURSION IN GRAMMAR RULES
ltexprgt 6 ltidgt ltexprgt ltidgt ltexprgt
( ltexprgt ) ltidgt When a BNF rule has
the non-terminal symbol of its left hand side
appearing as the rightmost symbol on its right
hand side, the rule is said to be right recursive.
22RECURSION IN GRAMMAR RULES
A rule which is right recursive can be used to
specify right associativity (meaning that
operators of equal precedence are evaluated from
right to left) Many programming languages
specify that addition, subtraction,
multiplication and division follow rules of left
associativity. Those that contain that
exponentiation operators, specify that right
associativity be used for exponentiation.
23An Unambiguous Grammar with Operator Precedence
Example 3.4
ltassigngt 6 ltidgt ltexprgt ltidgt 6 A B
C ltexprgt 6 ltexprgt lttermgt
lttermgt lttermgt 6 lttermgt ltfactorgt
ltfactorgt ltfactorgt 6 ( ltexprgt ) ltidgt