Concepts of Programming Language - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Concepts of Programming Language

Description:

... and only if it generates a sentential form that has two or ... semantic information is needed to select the intended parse. For example, in C the following: ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 34
Provided by: p9494
Category:

less

Transcript and Presenter's Notes

Title: Concepts of Programming Language


1
Concepts of Programming Language
  • Describing Syntax Semantics
  • Lecture 6

2
Chapter 3 Topics
  • Introduction
  • The General Problem of Describing Syntax
  • Formal Methods of Describing Syntax
  • Attribute Grammars
  • Describing the Meanings of Programs
  • Dynamic Semantics

3
Introduction
  • Syntax the form or structure of the expressions,
    statements, and program units
  • Semantics the meaning of the expressions,
    statements, and program units
  • Syntax and semantics provide a languages
    definition
  • Users of a language definition
  • Other language designers
  • Implementers
  • Programmers (the users of the language)

4
The General Problem of Describing
SyntaxTerminology
  • 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, begin)
  • A token is a category of lexemes (e.g.,
    identifier)

5
Formal Definition of Languages
  • Recognizers
  • A recognition device reads input strings over the
    alphabet of the language and decides whether the
    input strings belong to the language
  • Example syntax analysis part of a compiler
  • Generators
  • A device that generates sentences of a language
  • One can determine if the syntax of a particular
    sentence is syntactically correct by comparing it
    to the structure of the generator

6
BNF and Context-Free Grammars
  • Context-Free Grammars
  • Developed by Noam Chomsky in the mid-1950s
  • Language generators, meant to describe the syntax
    of natural languages
  • Define a class of languages called context-free
    languages
  • Backus-Naur Form (1959)
  • Invented by John Backus to describe Algol 58
  • BNF is equivalent to context-free grammars

7
BNF Fundamentals
  • In BNF, abstractions are used to represent
    classes of syntactic structures -- they act like
    syntactic variables (also called nonterminal
    symbols, or just terminals)
  • Terminals are lexemes or tokens
  • A rule has a left-hand side (LHS), which is a
    non-terminal, and a right-hand side (RHS), which
    is a string of terminals and/or non-terminals
  • Non-terminals are often enclosed in angle
    brackets
  • Examples of BNF rules
  • ltident_listgt ? identifier identifier,
    ltident_listgt
  • ltif_stmtgt ? if ltlogic_exprgt then ltstmtgt
  • Grammar a finite non-empty set of rules
  • A start symbol is a special element of the
    nonterminals of a grammar

8
BNF Rules
  • An abstraction (or nonterminal symbol) can have
    more than one RHS
  • ltstmtgt ? ltsingle_stmtgt
  • begin ltstmt_listgt end

9
Describing Lists
  • Syntactic lists are described using recursion
  • ltident_listgt ? ident
  • ident,
    ltident_listgt
  • A derivation is a repeated application of rules,
    starting with the start symbol and ending with a
    sentence (all terminal symbols)

10
An Example Grammar Derivation
  • ltprogramgt ? ltstmtsgt
  • ltstmtsgt ? ltstmtgt ltstmtgt
    ltstmtsgt
  • ltstmtgt ? ltvargt ltexprgt
  • ltvargt ? a b c d
  • ltexprgt ? lttermgt lttermgt lttermgt
    - lttermgt
  • lttermgt ? ltvargt const
  • ltprogramgt gt ltstmtsgt gt ltstmtgt
  • gt ltvargt ltexprgt
  • gt a ltexprgt
  • gt a lttermgt lttermgt
  • gt a ltvargt lttermgt
  • gt a b lttermgt
  • gt a b const

11
An Example Derivation
  • ltprogramgt gt ltstmtsgt gt ltstmtgt
  • gt ltvargt ltexprgt
  • gt a ltexprgt
  • gt a lttermgt lttermgt
  • gt a ltvargt lttermgt
  • gt a b lttermgt
  • gt a b const

12
Derivations
  • Every string of symbols in a 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

13
Parse Tree
  • A hierarchical representation of a derivation

14
Ambiguity in Grammars
  • A grammar is ambiguous if and only if it
    generates a sentential form that has two or more
    distinct parse trees
  • some string that it can generate in more than one
    way
  • semantic information is needed to select the
    intended parse
  • For example, in C the following
  • x y can be interpreted as either
  • the declaration of an identifier named y of type
    pointer-to-x, or
  • an expression in which x is multiplied by y and
    then the result is discarded.

15
An Ambiguous Expression Grammar
16
Associativity of Operators
  • Operator associativity can also be indicated by a
    grammar

17
Extended BNF
  • Optional parts are placed in brackets
  • ltproc_callgt ? ident (ltexpr_listgt)
  • Alternative parts of RHSs are placed inside
    parentheses and separated via vertical bars
  • lttermgt ? lttermgt (-) const
  • Repetitions (0 or more) are placed inside braces
  • ltidentgt ? letter letterdigit

18
BNF and EBNF
19
Semantics
  • BNF in the form of CFG is used to describe the
    syntax of a programming language
  • It can not describe the semantics (meaning) of
    the programming language statements, expressions,
    etc. 
  • There are two categories of semantics 
  • Static semantics semantic rules that can be
    checked during compile time (i.e., prior to
    runtime). As an example, variables in a program
    must be "declared" before they are referenced. 
  • Dynamic semantics semantic rules that apply
    during the execution of a program. 

20
Attribute Grammars
  • Attribute grammars (AGs) have additions to CFGs
    to carry some semantic info on parse tree nodes
  • Def An attribute grammar is a context-free
    grammar G (S, N, T, P) with the following
    additions
  • Attributes variables that can have values
    assigned to them
  • Synthesized attributes used to pass semantic
    information up a parse tree 
  • Inheritance attributes used to pass semantic
    information down a parse tree 
  • attribute computation functions specify how
    attribute values are computed
  • predicate functions state the syntax and semantic
    rules

21
Attribute Grammars An Example (1)
  • Consider the language
  • L anbncn n gt 1
  • No context-free grammar generates L.
  • An attempt
  • ltstringgt ? lta seqgt ltb seqgt ltc seqgt
  • lta seqgt ? a lta seqgt a
  • ltb seqgt ? b ltb seqgt b
  • ltc seqgt ? c ltc seqgt c
  • This context-free grammar generates the language
  • abc akbmcn kgt1, mgt1, ngt1

22
Attribute Grammars An Example (2)
  • Using a Synthesized Attributes
  • Attach a synthesized attribute, Size, to each of
    the nonterminals
  • lta seqgt, ltb seqgt, and ltc seqgt.
  • Impose a condition on the first production.
  • ltstringgt ? lta seqgt ltb seqgt ltc seqgt
  • Condition (predicate function)
  • Size(lta seqgt)Size(ltb seqgt)Size(ltc seqgt)

23
Attribute Grammars An Example (3)
  • Computation function on Attributes

24
(No Transcript)
25
(No Transcript)
26
Attribute Grammars An Example (Inherited
Attributes)
  • Using an Inherited Attribute
  • Attach a synthesized attribute Size to lta seqgt
  • Inherited attributes InSize to ltb seqgt and ltc
    seqgt.

27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
Dynamic Semantics
  • Attribute grammars still have the shortcoming of
    not being able to describe the  dynamic semantics
    of a programming language
  • Dynamic semantics is still in its infancy
  • No universally accepted notation to describe it
  • (imprecise) English text is often used to
    describe dynamic semantics
  • Three approaches are used 
  • Operational semantics describe program operation
    in terms of a lower-level language first used to
    describe PL/1 
  • Axiomatic semantics use axioms and inference
    rules for each kind of statement in the language 
  • Denotational semantics map a language construct
    into a mathematical object. 

31
Operational Semantics
  • Operational semantics is the easiest concept to
    describe
  • By way of example, consider the following use of
    a lower-level language to describe the semantics
    of a FORTRAN IV "DO" statement 
  • DO label variable initial, terminal,
    stepsize
  • The following pseudo-code from text book is an
    operational semantics description of the above
    statement 
  • init_value init_expression 
    terminal_value terminal_expression 
    step_value step_expression  do_var
    init_value  iteration_count
    max(int((terminal_value - init_value    
    step_value)/step_value),0)  loop  if
    iteration_count lt 0 goto out  loop
    body  do_var do_var step_value 
    iteration_count iteration_count - 1 
    goto loop  out...

32
Axiomatic Semantics
  • Axiomatic semantics uses axioms and inference
    rules for each kind of statement in the language
  • Assertions (also known as predicates) either
    pre-condition or post-condition a language
    statement.
  • From the example in Sebesta  
  •           X gt 10           sum 2 X
    1          SUM gt 1  (Pre-condition)           
    (Statement)          (Post-condition) 
  • The weakest pre-condition is the least
    restrictive precondition that will satisfy the
    post-condition. In the above example, the weakest
    pre-condition is X gt 0.
  • Axiom or inference rule must be defined for every
    statement type, and this can be very difficult
  • more of a research tool than a practical way of
    describing the meaning of language constructs.   

33
Denotational Semantics
  • Denotational semantics is another way of
    describing the dynamic semantics of a programming
    language
  • The approach here is to use mathematical objects
    to represent language constructs
  • Denotational semantics has the potential to
    provide accurate description of semantics
  • Language constructs are mapped to well-known
    mathematical objects
  • Too complex for general-purpose use. 
Write a Comment
User Comments (0)
About PowerShow.com