CSE 6341 (755) Programming Languages - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CSE 6341 (755) Programming Languages

Description:

Title: Computer and Information Science Engineering Subject: Dept Vist Author: Jeremy Loomis Last modified by: neelam Created Date: 10/20/1996 4:17:05 PM – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 23
Provided by: Jeremy177
Learn more at: https://cse.osu.edu
Category:

less

Transcript and Presenter's Notes

Title: CSE 6341 (755) Programming Languages


1
CSE 6341 (755)Programming Languages
  • Neelam Soundarajan
  • Computer Sc. Eng.
  • Dreese Labs 579
  • e-mail neelam_at_cse

2
Outline
  • Main Topic
  • Ways to formally define syntax and semantics of
    PLs
  • Plus a bit about programming methodologies
  • Tentative Schedule
  • Attribute grammars 3 weeks
  • Operational Semantics (including Lisp its
    interpreter) 3.5 weeks
  • Axiomatic Semantics 3.5 weeks
  • Denotational Semantics 1 week
  • Other topics ??
  • Exams etc. 0.5 week

3
References
  • Unfortunately, no good books for the course.
  • Mainly depend on slides, class discussions, your
    notes.
  • DO NOT miss classes.
  • Some useful references
  • Formal specification of programming languages, F.
    Pagan
  • Formal syntax and semantics of programming
    languages,Kurtz and Slonnegar
  • Lisp 1.5 programmers manual, McCarthy and others
  • Copies of all on reserve in the Sc./Eng. Library

4
Attribute Grammars
  • (Ref. Pagan (Ch. 2.3) Kurtz (Ch. 3))
  • Fact Context-free conditions specified using
    BNF
  • Question How do we specify context-sensitive
    (CS) conds?
  • Answer Using Attribute Grammars (AGs)
  • An AG is
  • a BNF grammar
  • attributes
  • rules for evaluating attributes
  • conditions (to capture c.s. requirements)

5
Example
  • L an bn cn n gt 1
  • ltasgt a altasgt1
  • Na(ltasgt)? 1 Na(ltasgt)? Na(ltasgt1)1
  • ltbsgt b bltbsgt1
  • Nb(ltbsgt)? 1 Nb(ltbsgt)? Nb(ltbsgt1)1
  • ltcsgt c cltcsgt1
  • Nc(ltcsgt)? 1 Nc(ltcsgt)? Nc(ltcsgt1)1
  • ltlsgt ltasgtltbsgtltcsgt
  • Cond Na(ltas) Nb(ltbsgt) Nc(ltcsgt)
  • Na Synthesized attribute of ltasgt
  • Nb Synthesized attribute of ltbsgt
  • Nc Synthesized attribute of ltcsgt
  • No inherited attributes in this grammar.

6
Some Comments
  1. Consider how the grammar works with a parse
    tree,allowing, say, "aabbcc", and disallowing
    "aabcc"
  2. Attributes are NOT program variables can't
    have Na(ltasgt) ? Na(ltasgt) 1
  3. In rules/conditions, can only refer to attributes
    of non-terminal on the left and the non-terminals
    in the current alternative. Can't look at "grand
    children" etc.
  4. Could have used N (instead of Na, Nb, Nc) as the
    name of all three attributes.

7
Example (revisited)
  • L an bn cn n gt 1
  • ltlsgt ltasgtltbsgtltcsgt
  • ExpNb(ltbsgt) ? Na(ltasgt) ExpNc(ltcsgt) ?
    Na(ltasgt)
  • ltasgt a altasgt1
  • Na(ltasgt)? 1 Na(ltasgt)? Na(ltasgt1)1
  • ltbsgt b bltbsgt1
  • Cond ExpNb(ltbsgt) 1 ExpNb(ltbsgt1) ?
    ExpNb(ltbsgt) -1
  • ltcsgt c cltcsgt1
  • Cond ExpNc(ltcsgt) 1 ExpNc(ltcsgt1) ?
    ExpNc(ltcsgt) -1
  • Na Synthesized attribute of ltasgt
  • ExpNb Inherited attribute of ltbsgt
  • ExpNc Inherited attribute of ltcsgt
  • Consider all strings over a,b,c (i.e., a's,
    b's, c's may be in any order) and require no. of
    a's no. of b's no. of c's (using synh.
    synthinh. attr)

8
Some Comments
  1. An AG is a BNF grammar plus a set of attributes,
    some synth., others inh. Each attribute is
    associated with a specific non-terminal.
  2. If S is a synth. attrib. of ltNgt, then for each
    alternative in ltNgt's production, must have an
    eval. rule that will be used to compute S(ltNgt)
    whenever that alternative is used.
  3. If I is an inh. attrib. of ltNgt, then for each
    occurrence of ltNgt on the right side of any
    production, must have eval. rule that will be
    used to computer I(ltNgt) if that alternative is
    used.
  4. Conditions are associated with individual
    alternatives of individual productions.
  5. If any condition in a tree evaluates to false,
    the tree collapses

9
Context-free condns. using AGs
  • CF conditions can also be expressed using AGs.
  • L an bn n gt 1
  • ltlsgt ltasgtltbsgt
  • Cond Na(ltasgt) Nb(ltbsgt)
  • AGs can be used to capture precedence, i.e.,
    specify how a string is to be parsed Consider
    all strings over a, b.Given "abab", want to
    ensure it is parsed as a(b(a(b))), notas
    (ab)(ab) etc.
  • ltstrgt a b
  • N(ltstrgt)? 1 N(ltstrgt)? 1
  • ltstrgt1ltstrgt2 N(ltstrgt) ? N(ltstrgt1)
    N(ltstrgt2) Cond (N(ltstrgt1) 1)

10
Context-sens. condns. in PLs
  • Main condition Id's that are used must have been
    declared
  • ltproggt ltblockgt
  • ltblockgt begin ltdecl seqgt ltstmt seqgt end
  • How to ensure that in ltstmt seqgt we only use
    objects declared in ltdecl seqgt?
  • Using synthesized attributes
  • ltblockgt begin ltdecl seqgt ltstmt seqgt end
  • Cond UsedIds(ltstmt seqgt) ? DeclIds(ltdecl
    seqgt)
  • Using inherited attributes
  • ltblockgt begin ltdecl seqgt ltstmt seqgt end
  • AllowedIds(ltstmt seqgt) ? DeclIds(ltdecl
    seqgt)

11
CS conditions in PLs (contd.)
  • Problem Nested blocks (what are they?)
  • Solution Use a sequence of sets, each containing
    Ids declared in a (surrounding) block
  • ltblockgt begin ltdecl seqgt ltstmt seqgt end
  • Nest(ltstmt seqgt) ? append(Nest(ltblockgt),
    Decs(ltdecl seqgt))
  • ltstmt seqgt ltstmtgt Nest(ltstmtgt
    ) ? Nest(ltstmt seqgt) ltstmtgtltstmt
    seqgt1 Nest(ltstmtgt), Nest(ltstmt seqgt1) ?
    Nest(ltstmt seqgt)
  • ltprogramgt ltblockgt Nest(ltblock
    gt) ? ltgt

12
CS conditions in PLs (contd.)
  • Problem Different types of Ids
  • Solution An element of Decs() is of the
    form ("xy", int), or ("ab", bool), or ("PQ",
    proc)
  • (For procedures, also need info about no./types
    of pars)
  • Where do we check (that the CS conds. are
    satisfied)?
  • ltassigngt ltidgt ltint expgt
  • Cond lastType(Name(ltidgt, Nest(ltassigngt)
    int
  • ltidgt ltbool expgt Cond
    lastType(Name(ltidgt, Nest(ltassigngt) bool

13
CS conditions in PLs (contd.)
  • ltproc callgt call ltidgt()
  • Cond lastType(Name(ltidgt), Nest(ltproc
    callgt)) proc ? parTypes(Name(ltidgt),
    Nest(ltproc callgt)) ltgt
  • call ltidgt(ltarg list)
  • Cond lastType(Name(ltidgt), Nest(ltproc
    callgt)) proc ? parTypes(Name(ltidgt),
    Nest(ltproc callgt)) ...
  • Question What about double declarations?
  • ltdsgt ltdeclgt
  • Decs(ltdsgt) ? Decs(ltdeclgt)
    Nest(ltdeclgt)?Nest(ltdsgt)
  • ltdeclgt ltdsgt1
  • Decs(ltdsgt) ? Decs(ltdeclgt) ? Decs(ltdsgt1)
  • Nest(ltdeclgt), Nest (ltdsgt1) ? Nest(ltdsgt)
  • Cond Decs(ltdeclgt) ? Decs(ltdsgt1) ? //
    Not quite?

14
  • Note Maybe better to look at e.g. in pp. 15/16
    before grammar rules below
  • Questions How do elements get into
    Decs()? How do procs access surr. block/call
    other procs? (Ans Need to pass Nest also
    to the ltdeclgts.)
  • ltblockgt begin ltdecl seqgt ltstmt seqgt end
  • Nest(ltstmt seqgt) ? append(Nest(ltblockgt),
    Decs(ltdecl seqgt)) Nest(ltdecl seqgt) ?
    append(Nest(ltblockgt), Decs(ltdecl seqgt))
  • ltdeclgt int ltidgt
  • Decs(ltdeclgt) ? (Name(ltidgt), int)
  • bool ltidgt
  • Decs(ltdeclgt) ? (Name(ltidgt), bool)
  • proc ltidgt() ltblockgt
  • Decs(ltdeclgt) ? (Name(ltidgt), proc, ltgt)
  • Nest(ltblockgt) ? Nest(ltdeclgt) // need to
    change?
  • proc ltidgt(ltpar listgt) ltblockgt
  • Decs(ltdeclgt) ? (Name(ltidgt), proc,
    Partypes(ltpar listgt))
  • Nest(ltblockgt) ? append(Nest(ltdeclgt),
    Decs(ltpar listgt))

15
ltproggt
ltblockgt
ltdsgt
ltssgt
ltdgt
ltdsgt
int X, Y
ltdsgt
ltdgt
ltblockgt
proc Q()
ltdsgt
ltstmtgt
ltblockgt
ltdsgt
ltssgt
16
ltproggt
ltblockgt
N1
D1
ltdsgt
ltssgt
D2
N2
N3
D3
ltdgt
ltdsgt
N5
N4
D4
D5
int X, Y
ltdsgt
D6
N6
N7
D7
ltdgt
D17
D18
ltblockgt
proc Q()
N8
D8
N18
D18
ltdsgt
N9
N10
D9
D10
ltstmtgt
N10
D10
ltblockgt
N12
D12
ltdsgt
N11
D11
ltssgt
D14
N14
N13
D13
N15
D15
N16
D16
17
Translational Semantics
  • AGs also used to specify translations (code
    generation) (e.g. YACC) (Ref Pagan (ch. 3.2)
    Kurtz (ch. 7))
  • Basic idea
  • ltstmtgt ltstmtgt1 ltstmtgt2 Code(ltstmtgt)?
    append(Code(ltstmtgt1), Code(ltstmtgt2))
  • but the details are more complex ...
  • E.g. How to ensure the same label is not used
    inCode(ltstmtgt1) and Code(ltstmtgt2) ?
  • A simple imperative language (we will call it
    IMP taken from Pagan)
  • ltproggt ltstmtgt
  • ltstmtgt skip ltassigngt
    ltstmtgt1ltstmtgt2 if ltbegt thenltstmtgt1 else
    ltstmtgt2 while ltbegt do ltstmtgt1
  • ltassigngt ltidgt ltaegt
  • ltaegt ltidgt ltintgt ltaegt1ltaegt2 ltaegt1 ?
    ltaegt2 ltaegt1 ltaegt2
  • ltbegt true false ltaegt1ltaegt2 ltaegt1 lt
    ltaegt2 ?ltbegt ltbegt1 ? ltbegt2 ltbegt1 ?
    ltbegt2

18
Translational Semantics (contd.)
  • Key attributes
  • Code a synth. attribute of ltstmtgt, ltaegt, ltbegt
    The seq. of assembly instructions corresponding
    to a particular ltstmtgt, ltaegt, ltbegt
  • Labin (inh.), Labout (synth.) keep track of next
    available label
  • Temp (inh.) keeps track of next available memory
    loc. for temporary use
  • ltproggt ltstmtgt
  • Code(ltproggt) ? Code(ltstmtgt)
  • Labin(ltstmtgt) ? 1
  • ltstmtgt ltstmtgt1ltstmtgt2 Code(ltstmtgt) ?
    append(Code(ltstmtgt1), Code(ltstmtgt2))
  • Labin(ltstmtgt1) ? Labin(ltstmtgt)
  • Labin(ltstmtgt2) ? Labout(ltstmtgt1)
  • Labout(ltstmtgt) ? Labout(ltstmtgt2)

19
Translational Semantics (contd.)
  • ltstmtgt ltassigngt
  • Code(ltstmtgt) ? Code(ltassigngt)
  • Labout(ltstmtgt) ? Labin(ltstmtgt) // why?
  • if ltbegt then ltstmtgt1 else
    ltstmtgt2 Labin(ltstmtgt1) ? Labin(ltstmtgt) 2
    // why?
  • Labin(ltstmtgt2) ? Labout(ltstmtgt1)
  • Labout(ltstmtgt) ? Labout(ltstmtgt2)
  • Code(ltstmtgt) ? append(
  • Code(ltbegt), ("BZ", Labin(ltstmtgt)), //
    slight problem
  • Code(ltstmtgt1), ("BR", label(Labin(ltstmtgt)
    1)),
  • (label(Labin(ltstmtgt)) "No-Op"),
  • Code(ltstmtgt2), (label(Labin(ltstmtgt)1)
    "No-Op") )
  • while ltbegt do ltstmtgt1 Labin(ltstmtgt1
    ) ? ...
  • Labout(ltstmtgt) ? ...
  • Code(ltstmtgt) ? ...
  • BZ "branch on zero" BR "unconditional
    branch" "No-Op" "continue".

20
Translational Semantics (contd.)
  • ltassigngt ltidgt ltaegt
  • Code(ltassigngt) ? append(Code(ltaegt), ("STO",
    Name(ltidgt))
  • Temp(ltaegt) ? 1
  • ltaegt ltintgt
  • Code(ltaegt) ? lt("LOAD" Value(ltintgt))gt
  • ltidgt
  • Code(ltaegt) ? lt("LOAD" Name(ltidgt))gt
  • ltaegt1ltaegt2
  • Code(ltaegt) ? append(
  • Code(ltaegt1), ("STO" temp(Temp(ltaegt)),
  • Code(ltaegt2), ("ADD" temp(Temp(ltaegt))) )
    Temp(ltaegt1) ?Temp(ltaegt)
  • Temp(ltaegt2) ?Temp(ltaegt) 1 // why?

21
Static Scope (Algol, Pascal, C, C,...)
  • Entities accessible in a procedure Entities
    declared in that procedure Entities declared in
    the surrounding procedure (less those with
    name conflicts) Entities declared in procedure
    surrounding the surrounding procedure ...
  • Visualize Each procedure is a box whose sides
    are one-way mirrors you can look out of the box,
    but you cant look into a box
  • Some languages are not quite static scope but are
    close.

22
Example
  • program
  • A, B, C integer
  • Q procedure // no parameters begin B
    B2 C C2 print A, B, C end (Q)
  • R procedure A integer begin A 3 C
    2 call Q B AC print A, B, C end (R)
  • S procedure A, C integer
  • Q procedure // nested in S C
    integer begin A A1 C B1
    print A, B, C end (S.Q)
  • begin // body of S B 3 C 1
    A 4 print A, B, C call R print A, B, C
    end (S)
  • begin // main body A 1 B 1 C
    1 call R print A, B, C call S print A,
    B, C end (main)
Write a Comment
User Comments (0)
About PowerShow.com