Title: Programming Languages
1ProgrammingLanguages
CSCI-4430 CSCI-6969January 18, 2008
- David Goldschmidt, Ph.D.
- Computer Science
- The College of Saint Rose
2Syntax and Semantics (i)
- Syntax is the form or structure of the
expressions, statements, and program units of a
given language - Syntax of a Java while statement
- while ( ltboolean_exprgt ) ltstatementgt
- Partial syntax of an if statement
- if ( ltboolean_exprgt ) ltstatementgt
3Syntax and Semantics (ii)
- What is semantics?
- The meaning of the expressions, statements, and
program units of a given language - Semantics of the given Java while statement?
- while ( ltboolean_exprgt ) ltstatementgt
- Execute ltstatementgt repeatedly (0 or more times)
as long as ltboolean_exprgt evaluates to true
4Syntax and Semantics (iii)
- Syntax and semantics together define a
programming language
syntax
programming language
semantics
5Describing Syntax
- Terminology to describe syntax
- A sentence is a string of characters over some
alphabet - A language is a set of sentences
- A lexeme is the lowest-level syntactic unit of a
language (e.g. , sum, while) - One step above individual characters
- A token is a category of lexemes
- e.g. identifier, equal_sign, multiplication_sign,
integer_literal, etc.
6Recognizers and Generators (i)
- Languages defined by recognition and generation
- A recognizer reads input strings and determines
whether the strings belong to the language - Syntax analysis part of a compiler
language L recognizer
7Recognizers and Generators (ii)
- Languages defined by recognition and generation
- A generator produces syntactically acceptable
strings of a given language - Inspect generator rules (i.e. a grammar) to
determine if a sentence is acceptable for a given
language
language L generator
8Grammars
- Linguist Noam Chomsky (mid-1950s) developed four
classes of generative devices or grammars to
define four classes of languages - Context-free grammars proved useful for
describing the syntax of programming languages - Regular grammars proved useful for describing the
tokens of a programming language
9Backus-Naur Form (i)
- John Backus and Peter Naur developed a formal
notation for specifying programming language
syntax (in 1959/1960) - Backus-Naur Form (BNF ) nearly identical to
Chomskys context-free grammars - Syntax of assignment statement in BNF
- Example sentence whose syntax matches the rule
ltassigngt ? ltvargt ltexpressiongt
total cost quantity
10Backus-Naur Form (ii)
- RHS consists of abstractions (non-terminals), as
well as lexemes and tokens (terminals)
ltassigngt ? ltvargt ltexpressiongt
11Backus-Naur Form (iii)
ltprogramgt ? begin ltstmtsgt end ltstmtsgt ? ltstmtgt
ltstmtgt ltstmtgt ltstmtgt ? ltvargt ltexprgt ltvargt ? a
b c d ltexprgt ? lttermgt lttermgt lttermgt -
lttermgt lttermgt ? ltvargt literal-integer-value
12Derivations (i)
- A derivation is a repeated application of rules
- Starts with the start symbol and ends with a
sentence - Many (often infinite!) possible derivations
ltprogramgt gt begin ltstmtsgt end gt begin
ltstmtgt end gt begin ltvargt ltexprgt end
gt begin b ltexprgt end gt
begin b lttermgt lttermgt end gt begin
b ltvargt lttermgt end gt begin b c
lttermgt end gt begin b c 12 end
13Derivations (ii)
- Every string of symbols in the derivationis a
sentential form - A sentence is a sentential form that hasonly
terminal symbols - Thus terminating the derivation
14Leftmost Derivations (i)
- A leftmost derivation is one in which the
leftmost non-terminal in each sentential form is
the one that is expanded next
ltprogramgt gt begin ltstmtsgt end gt begin
ltstmtgt end gt begin ltvargt ltexprgt end
gt begin b ltexprgt end gt
begin b lttermgt lttermgt end gt begin
b ltvargt lttermgt end gt begin b c
lttermgt end gt begin b c 12 end
15Leftmost Derivations (ii)
ltassigngt ? ltvargt ltexprgt ltvargt ? A B C
D ltexprgt ? ltvargt ltexprgt ltvargt
ltexprgt ( ltexprgt ) ltvargt
grammar
what about a rightmost derivation?
16Working with Grammars
ltSgt ? ltAgt ltBgt ltCgt ltAgt ? a ltAgt a ltBgt ? b ltBgt
b ltCgt ? c ltCgt c
- Given this grammar
- Which of the following sentences are generated by
this grammar? - baabbcc
- abc
- bbaa
- aabbbbccc
17Reading Assignments
- Read for next week
- Chapters 3