Programming Languages - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Programming Languages

Description:

Linguist Noam Chomsky (mid-1950s) developed four classes of generative devices ... Backus-Naur Form (BNF ) nearly identical to Chomsky's context-free grammars ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 18
Provided by: DavidGol3
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages


1
ProgrammingLanguages
CSCI-4430 CSCI-6969January 18, 2008
  • David Goldschmidt, Ph.D.
  • Computer Science
  • The College of Saint Rose

2
Syntax 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

3
Syntax 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

4
Syntax and Semantics (iii)
  • Syntax and semantics together define a
    programming language

syntax
programming language
semantics
5
Describing 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.

6
Recognizers 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
7
Recognizers 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
8
Grammars
  • 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

9
Backus-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
10
Backus-Naur Form (ii)
  • RHS consists of abstractions (non-terminals), as
    well as lexemes and tokens (terminals)

ltassigngt ? ltvargt ltexpressiongt
11
Backus-Naur Form (iii)
  • Example language

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
12
Derivations (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
13
Derivations (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

14
Leftmost 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
15
Leftmost Derivations (ii)
ltassigngt ? ltvargt ltexprgt ltvargt ? A B C
D ltexprgt ? ltvargt ltexprgt ltvargt
ltexprgt ( ltexprgt ) ltvargt
grammar
what about a rightmost derivation?
16
Working 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

17
Reading Assignments
  • Read for next week
  • Chapters 3
Write a Comment
User Comments (0)
About PowerShow.com