CS 21: Structure of Prog' Languages - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

CS 21: Structure of Prog' Languages

Description:

What we need: concise yet understandable description of a ... metalanguage: language used to describe another language. XU Department of Computer Studies ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 44
Provided by: jayrcfer
Category:

less

Transcript and Presenter's Notes

Title: CS 21: Structure of Prog' Languages


1
CS 21 Structure of Prog. Languages
  • Jay R.C. Fernandez, M.S.
  • Instructor

2
Describing Syntax and Semantics
3
Syntax Semantics
  • What we need concise yet understandable
    description of a programming language
  • implementers must understand
  • how the statements are formed
  • what they mean

4
Syntax Semantics
  • need for a language reference manual
  • Syntax form of its expressions, statements and
    program units
  • Semantics meaning of those expressions and
    program units

5
Syntax Semantics
  • Formal Methods of Describing Syntax
  • Grammars
  • Syntax Diagrams
  • Parse Trees
  • metalanguage language used to describe another
    language

6
Describing Syntax
  • Grammar
  • constituted by a set of tokens, terminals,
    non-terminals, productions, and the goal symbol
  • describe the hierarchical syntactic structure of
    the sentence of language
  • BNF (Backus-Naur Form) one way of writing
    grammars

7
Describing Syntax
  • Backus-Naur Form (BNF)
  • method of describing syntax
  • originally presented by John Backus (to describe
    ALGOL 58)and later modified by Peter Naur
  • similar to Chomskys Context-free Diagrams (1950)

8
Describing Syntax
  • Parts of a Grammar
  • a set of tokens
  • a set of non-terminal symbols
  • a set of rules called productions
  • a goal symbol

9
Describing Syntax
  • Grammar
  • lexemes small syntactic units (lexical
    specifications)
  • e.g. identifiers, constants, operators, and
    special words
  • language statements string of lexemes
  • token a category of lexemes

10
Describing Syntax
  • Grammar Symbols
  • Goal Symbol
  • one of the set of non-terminal symbols
  • also referred to as the start symbol
  • Terminal Symbols
  • symbols that are atomic / non-divisible
  • can be combined to form valid constructs in the
    language

11
Describing Syntax
  • Grammar Symbols
  • Non-Terminal Symbols
  • symbols used to represent intermediate
    definitions within the language
  • defined by productions
  • syntactic classes or categories

12
Describing Syntax
  • Grammar Productions
  • BNF uses abstraction for syntactic structures
  • ltassigngt ? ltvargtltexpressiongt

13
Describing Syntax
  • Grammar Productions
  • LHS abstraction being defined
  • RHS tokens, lexemes, references to other
    abstractions

14
Describing Syntax
  • Grammar Productions
  • a definition of a non-terminal symbol
  • has the form
  • x ? y
  • where x is a non-terminal symbol and y is a
    sequence of symbols (non-terminal or terminal)

15
Describing Syntax
  • Grammar Derivation
  • sentences of the language are generated through
    repeated application of the rules, beginning with
    a start symbol

16
Describing Syntax
  • Grammar Production Example
  • ltif_stmtgt ? if ltlogic_exprgt then
  • ltstatement_listgt
  • else
  • ltstatement_listgt

17
Describing Syntax
  • Rules to form Grammar
  • every non-terminal symbol must appear to the left
    of the ? at least one production
  • the goal symbol must not appear to the right of
    the ? of any production

18
Describing Syntax
  • Rules to form Grammar
  • a rule is recursive if its LHS appears in its RHS
  • ltid_listgt ? ltidentifiergt

19
Describing Syntax
  • Given Grammar Productions
  • ltprogramgt ? begin ltstmt_listgt end
  • ltstmt_listgt ? ltstmtgt ltstmtgt ltstmt_listgt
  • ltstmtgt ? ltvargt ltexpressiongt
  • ltvargt ? A B C
  • ltexpressiongt ? ltvargt ltvargt

20
Describing Syntax
  • Sample Derivation
  • ltprogramgt ? begin ltstmt_listgt end
  • begin ltstmtgt end
  • begin ltvargt ltexpressiongt end
  • begin ltvargt ltvargt ltvargt end
  • begin A B C end

21
Describing Syntax
  • When does derivation stop?
  • by exhaustingly choosing all combinations of
    choices, the entire language can generate

22
Describing Syntax
  • Syntax Diagrams
  • a.k.a. Syntax Graphs / Syntax Charts
  • represented by a directed graph
  • a separate graph is used for each syntactic unit
  • Rectangles Non-terminal Symbols
  • Circles Terminal Symbols

23
Describing Syntax
  • Syntax Diagrams

Calculator

ltexprgt
ltexprgt
ltvalgt
ltoprgt
ltexprgt
24
Describing Syntax
  • Syntax Diagrams

ltvalgt
ltunsgngt
.
ltunsgngt
ltsgngt
25
Describing Syntax
  • Syntax Diagrams

ltunsgngt
ltdigitgt
ltunsgngt
ltsigngt

-
26
Describing Syntax
  • Syntax Diagrams

ltdigitgt
0
1
2
3
4
5
..
9
27
Describing Syntax
  • Syntax Diagrams

ltoprgt

-

/
28
Describing Syntax
  • Parse Trees
  • parse trees hierarchical structure
  • node non-terminal symbol
  • leaves terminal symbol

29
ltprogramgt
begin
end
ltstmt_listgt
ltstmtgt
ltvargt
ltexprgt

ltvargt
ltvargt

A
B
C
Derivation of begin A B C end
30
Describing Syntax
  • Why do we need to describe syntax?
  • to express our ideas to a form understandable by
    the computer
  • be acquainted with rules
  • transformation/translation
  • comparison with other languages

31
Describing Semantics
  • Informal Description
  • Attribute Grammars
  • Operational Semantics
  • Axiomatic Semantics
  • Denotational Semantics

32
Describing Semantics
  • Informal Description
  • uses informal neutral language descriptions
  • construed from English language description
  • example when an integer and a double are added,
    the result is a double

33
Describing Semantics
  • Attribute Grammars
  • the CFG (Context Free Grammar) for the language
    is augmented with a set of attributes and rules
    for computing those attributes
  • describe the syntax and static semantics
  • Static Semantics that part of its semantics
    which can be determined without executing its code

34
Describing Semantics
  • Attribute Grammar
  • grammars with sets of attribute values associated
    with a grammar symbol
  • attribute computation functions -- how attribute
    values are computed
  • predicate functions -- state some of the syntax
    and semantic rules

35
Describing Semantics
  • Example of Attribute Grammar
  • expr ? term
  • expr ? expr term
  • 1 2 3
  • some attributes of symbols aret typev
    values scope

36
Describing Semantics
  • to compute the v attribute of the attribute
    grammar might be the following rule
  • (1.t 3.t)
  • switch (1.t)
  • case INTEGER .t INTEGER
  • .v 1.v.INTEGER
    3.v.INTEGER
  • break
  • case DOUBLE ...

37
Describing Semantics
  • Operational Semantics
  • the semantics of a program language is described
    by executing its statements on a machine, either
    real or simulated
  • the changes that occur in the machines state
    when it executes a given statement define the
    meaning of that statement

38
Describing Semantics
  • Example of Operational Semantics

Operational Semantics expr1 loop if expr2 0
goto out expr3 goto loop out
C Statement for (expr1 expr2 expr3)
39
Describing Semantics
  • Axiomatic Semantics
  • prove the correctness of programs
  • assertions preconditions and post-conditions
  • only for very simple statements

40
Describing Semantics
  • Example of Axiomatic Semantics
  • P S Q
  • where P is the Precondition
  • S is the statement to be defined
  • Q is the Postcondition
  • xgt5 sum 2 x 1 sum gt11

41
Describing Semantics
  • Denotational Semantics
  • describe the language by execution
  • define a mathematical object for each entity
  • define a function that maps instances of the
    entity to the instances of mathematical objects

42
Describing Semantics
  • Example of Denotational Semantics
  • ltnumgt ltnumgtltdigitgt ltdigitgt
  • ltdigitgt 0 1 2 3 4 5 6 7 8 9
  • V(0) 0
  • V(1) 1
  • V(9) 9
  • V(ltnumgtltdigitgt) V(ltnumgt)10 V(ltdigitgt)

43
Describing Semantics
  • Why do we need to describe semantics?
  • understanding programs
  • transformation/translation
  • proving correctness
  • comparison with other languages
Write a Comment
User Comments (0)
About PowerShow.com