Describing Syntax and Semantics Chapter 3 - PowerPoint PPT Presentation

About This Presentation
Title:

Describing Syntax and Semantics Chapter 3

Description:

SYNTAX - the form of the expressions, statements and program units in a ... Noam Chomsky, linguist, described four language generation devices, called ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 24
Provided by: eckerdwor
Category:

less

Transcript and Presenter's Notes

Title: Describing Syntax and Semantics Chapter 3


1
Describing Syntaxand SemanticsChapter 3
2
Describing 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
3
Describing 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.
4
Describing 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
5
Describing Syntax
Languages may be formally defined by one of two
methods recognition or generation.
6
Describing 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.
7
Describing 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.
8
Describing 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
9
Describing 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)
10
Describing Syntax
Meta language - a language that can be used to
describe another language BNF is a meta language
for the description of programming languages.
11
Describing Syntax
BNF uses abstractions to denote various syntactic
structures. ltassigngt is an abstraction which
denotes a valid assignment statement
12
Describing 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
13
Describing 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
14
Describing 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
15
Describing Syntax
BNF rule or production ltidgt 6 A B C
16
Describing 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
17
Describing 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.
18
Describing 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
19
Grammar for Simple Assignment Statements
Example 3.2
ltassigngt 6 ltidgt ltexprgt ltidgt 6 A B
C ltexprgt 6 ltidgt ltexprgt ltidgt
ltexprgt ( ltexprgt ) ltidgt
20
An 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
21
More 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.
22
RECURSION 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.
23
An 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
Write a Comment
User Comments (0)
About PowerShow.com