0% found this document useful (0 votes)
29 views

CSCI351-Week 2-Lecture 1

Uploaded by

AbuSaMrA /Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

CSCI351-Week 2-Lecture 1

Uploaded by

AbuSaMrA /Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 3: Describing Syntax

and Semantics

Week 2 Lecture 1 (1/8)


Objectives
3.1 Introduction

3.2 The General Problem of Describing Syntax

3.3 Formal Methods of Describing Syntax


3.3.1 Backus-Naur Form and Context-Free Grammars
3.3.1.1 Context-Free Grammars

3.3.1.2 Origins of Backus-Naur Form

3.3.1.3 Fundamentals

3.3.1.4 Describing Lists

3.3.1.5 Grammars and Derivations

CSCI351 - Concepts of Programming


1-2
Languages
Introduction
• Syntax: the form or structure of the expressions, statements,
and program units (Example1: while (boolean_expr) statement)

• Semantics: the meaning of the expressions, statements, and


program units
– The semantics of Example1 is: when boolean_expr is true, statement is
executed. If boolean_expr is false, control transfers to the statement following
the while construct.)

• Syntax and semantics provide a language’s definition


– Users of a language definition

• Other language designers

• Implementers

• Programmers (the users of the language)

CSCI351 - Concepts of Programming


1-3
Languages
The General Problem of Describing
Syntax (1/3)
• A metalanguage is a language used to describe another
language. (Example: English used to describe Java)

• A sentence is a string of characters over some alphabet

• A language is a set of sentences


– a language is specified by a set of rules

• A lexeme is the lowest level syntactic unit of a language


(e.g., *, sum, begin)

• A token is a category of lexemes (e.g., identifier)

CSCI351 - Concepts of Programming


1-4
Languages
The General Problem of Describing
Syntax (2/3)
Consider the following Java statement:

index = 2 * count + 17;

Lexeme Token
index identifier
= equal_sign
2 int_literal
* mult_operator
count identifier
+ plus_operator
17 int_literal
; semicolon
CSCI351 - Concepts of Programming
1-5
Languages
The General Problem of Describing
Syntax (3/3)
• Recognizers
– Read a string and decide whether it follows the rules for the
language
– Example: syntax analysis part of a compiler (Chapter 4) (Not
included in the course)

• Generators
– A device that generates sentences of a language (BNF)
(Chapter 3)
– More useful for specifying the language than for checking a
string

CSCI351 - Concepts of Programming


1-6
Languages
Formal Methods of Describing
Syntax
• Backus-Naur Form (BNF) and Context-Free Grammars
– Most widely known method for describing programming
language syntax

– Developed as part of the process for specifying ALGOL

– Define a class of languages called context-free languages

– BNF is a metalanguage for programming languages.

• Extended BNF (Not included)


– Improves readability and writability of BNF

CSCI351 - Concepts of Programming


1-7
Languages
Backus-Naur Form and Context-
Free Grammars (1/9)
• Context-Free Grammars
– Chomsky described four classes of grammars

Type Characteristics
0 Unrestricted
1 Context-sensitive
2 Context-free
3 Regular

– Types 2 and 3 are useful for programming language specifications

• Origins of Backus-Naur Form


– Backus developed notation for programming language specifications
syntax (ALGOL58).

CSCI351 - Concepts of Programming


1-8
Languages
Backus-Naur Form and Context-
Free Grammars (2/9)
• Fundamentals (1/2)
– Abstractions are used to describe syntactic structures
• Example: A Java assignment statement might be represented by the abstraction <assign>

• The actual definition of <assign> can be given by <assign> → <var> = <expression>

– Terminals used are Lexemes and Tokens

– Example 1 of BNF rule (or production):


<assign> → <var> = <expression>
example: total = subtotal1 + subtotal2

– Example 2 of BNF rules:


<if_stmt> → if (<logic_expr>) <stmt>
<if_stmt> → if (<logic_expr>) <stmt> else <stmt>

<stmt>  <single_stmt>
| begin <stmt_list> end
CSCI351 - Concepts of Programming
1-9
Languages
Backus-Naur Form and Context-Free
Grammars (3/9)
• Fundamentals (2/2)
– A rule has a left-hand side (LHS) and a right-hand side (RHS),
and consists of terminal and nonterminal symbols

– In a context-free grammar, there can only be one symbol on


the LHS

– A grammar is a finite nonempty set of rules

– An abstraction (or nonterminal symbol) can have more than


one RHS usually separated by the symbol “|” (logical OR)

CSCI351 - Concepts of Programming


1-10
Languages
Backus-Naur Form and Context-
Free Grammars (4/9)
• Describing Lists
– A list of identifiers appearing on a data declaration statement.
– Syntactic lists are described using recursion:
<ident_list> → identifier
| identifier, <ident_list>

<ident_list> → <ident>
| <ident>, <ident_list>

<arg_list> → <expr>
Example:
| <expr>, <arg_list>
<digit> 0|1|2|3|4
<param_list> → <param> |5|6|7|8|9
| param, <param_list>
<integer>  <digit>
<stmt_list> → <stmt> | <digit> <integer>
| <stmt>; <stmt_list>
CSCI351 - Concepts of Programming
1-11
Languages
Backus-Naur Form and Context-
Free Grammars (5/9)
• Grammars and Derivations (1/5)
– A Grammar for a Small Language

CSCI351 - Concepts of Programming


1-12
Languages
Backus-Naur Form and Context-
Free Grammars (6/9)
• Grammars and Derivations (2/5)
– A Derivative of a program in this language:

begin A = B + C; B = C end

CSCI351 - Concepts of Programming


1-13
Languages
Backus-Naur Form and Context-
Free Grammars (7/9)
• Grammars and Derivations (3/5)
– A derivation is a repeated application of rules, starting with the
start symbol and ending with a sentence (all terminal symbols)

– Every string of symbols in the derivation is a sentential form

– A sentence is a sentential form that has only terminal symbols

– A leftmost derivation is one in which the leftmost nonterminal in


each sentential form is the one that is expanded

– A derivation may be neither leftmost nor rightmost

CSCI351 - Concepts of Programming


1-14
Languages
Backus-Naur Form and Context-
Free Grammars (8/9)
• Grammars and Derivations (4/5)
– A Grammar for a Simple Assignment Statements

CSCI351 - Concepts of Programming


1-15
Languages
Backus-Naur Form and Context-
Free Grammars (9/9)
• Grammars and Derivations (5/5)
– A Derivative of a program in this language: A = B * (A + C)

CSCI351 - Concepts of Programming


1-16
Languages

You might also like