Modeling Language Properties - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Modeling Language Properties

Description:

Finite-State Automaton. Pushdown Automaton. Linear-Bounded Automaton. Turing Machine ... Linear-Bounded Automaton. 0. 0. 1. 0. 1. 1. 0. 1. 1. 0. Context ... – PowerPoint PPT presentation

Number of Views:1407
Avg rating:3.0/5.0
Slides: 46
Provided by: juancarl
Category:

less

Transcript and Presenter's Notes

Title: Modeling Language Properties


1
Modeling Language Properties
  • Juan Carlos Guzmán
  • CS 6123 Design and Implementation of Programming
    Languages
  • Southern Polytechnic State University

2
Formal Models Introduced
  • Formal Grammars
  • Language Semantics
  • Program Verification

3
Formal Grammars/Languages
  • Already studied
  • Grammar types
  • Regular (type 3)
  • Context-Free (type 2)
  • Context-Sensitive (type 1)
  • Unrestricted (type 0)

Unrestricted
Context-Sensitive
Context-Free
Regular
Chomsky Hierarchy
4
Recognizers
  • Language
  • Regular
  • Context-Free
  • Context-Sensitive
  • Unrestricted
  • Recognizer
  • Finite-State Automaton
  • Pushdown Automaton
  • Linear-Bounded Automaton
  • Turing Machine

5
Finite-State Automaton
  • Grammars
  • Productions of form
  • A ? w B
  • A ? w
  • Also described by regular expressions
  • Regular Languages
  • Tape
  • read-only
  • read-once (one-way)
  • finite
  • Future behavior is dependent of
  • current state
  • whats left on tape

0
0
1
0
1
1
0
1
1
0
6
Push-Down Automaton
  • Grammars
  • Productions of form
  • A ? ?
  • Context-Free Languages
  • Tape
  • read-only
  • read-once
  • finite
  • Stack
  • read/write (pop/push)
  • finite
  • Future behavior is dependent of
  • current state
  • whats left on input
  • whats on the stack

0
0
1
0
1
1
0
1
1
0
1
0
0
1
0
7
Linear-Bounded Automaton
  • Grammars
  • Productions of form
  • ? ? ?
  • where ???????
  • Context-Sensitive Languages
  • Tape
  • read/write
  • finite
  • No stack
  • Future behavior is dependent of
  • current state
  • whats on tape

0
0
1
0
1
1
0
1
1
0
8
Turing Machine
  • Grammars
  • Productions of form
  • ? ? ?
  • without restrictions
  • Unrestricted Languages
  • Tape
  • read/write
  • unbounded
  • Future behavior is dependent of
  • current state
  • whats on tape

0
0
1
0
1
1
0
1
1
0
9
Theoretical Issues
  • It is believed that the Turing Machine can
    describe any computable function (Church-Turing
    Thesis)
  • There are functions that are not computable
  • The Halting Problem
  • Algorithm Complexity
  • Computation in polynomial time (P)
  • Computation in nonpolynomial time (NP)

10
Why Semantics?
  • We need to establish precisely the meaning of
    programs
  • English is not enough it is imprecise!

11
Examples
  • What does it mean to compute
  • x5
  • x
  • xy

12
More Examples
  • What does it mean to compute
  • fact(5)
  • fact(-5)
  • where fact is defined as
  • int fact(n)
  • if (n0)
  • return 1
  • return n fact(--n)

13
Still more Examples
  • What does it mean to define
  • int fact(n)
  • if (n0)
  • return 1
  • return n fact(--n)

14
Issues
  • Any construct can be given a semantics, if one is
    persistent enough!
  • The more complex the semantics, the less
    intuitive your language is!
  • Be simple and elegant

15
Modeling the Semantics (I)
  • Grammatical Models
  • attribute grammars
  • additional information extracted from the parse
    tree
  • Operational Models
  • operational semantics
  • virtual machines
  • the behavior of the machine defines the meaning
    of programs
  • Axiomatic Models
  • axiomatic semantics
  • Meaning given by using Logic to establish
    assertions (properties) about programs
  • Applicative Models
  • denotational semantics
  • meaning given by modeling each construct as a
    function

16
Modeling the Semantics (II)
  • Specification Models
  • algebraic data types
  • meaning given by describing the relationship
    among elements implementing the program

17
Attribute Grammars
  • Attribute grammars are extensions to Context-Free
    grammars that allow certain language rules to be
    expressed
  • They are used to
  • further restrict the phrases in a language
  • building and use of symbol tables
  • enforce static binding (decl. use of ids)
  • compute properties of phrases
  • Construction of the AST

18
Attribute Grammars (II)
  • There is the tendency associate syntax with the
    CFG used in parsing, and to call all other
    properties pertaining to the phrases of the
    language as semantics, or, more properly, static
    semantics
  • Your professor would rather call them abstract
    interpretations, with AG being one convenient
    way of expressing these

19
Attribute Grammars (III)
  • An attribute grammar is
  • a context-free grammar, with
  • attributes
  • attribute computation functions
  • predicate functions
  • Attributes are variables that can hold values on
    specific nodes of a syntax tree
  • Attribute computation functions indicate how the
    values to those attributes are computed
  • Predicate functions state properties that must be
    satisfied by the tree Sebesta

20
Attributes
  • Attributes are variables, associated to tree
    nodes, that will be given a value
  • Inherited attribute the value of the attribute
    is determined by the environment where the
    attribute is located
  • Synthesized attribute the value of the attribute
    is dependent on the information contained in the
    subtree where it is located
  • Intrinsic attribute a synthesized attribute for
    a leaf node whose values come from outside the
    parse tree Sebesta

21
Grammar for Simple Assignment Statements
  • ?assign? ? ?var? ?expr?
  • ?expr? ? ?var2? ?var3?
  • ?expr? ? ?var?
  • ?var? ? A B C
  • Allowable types are int or real
  • Attributes
  • actual_type, for nonterminals ?expr? and ?var?
  • expected_type, for nonterminal ?expr?
  • string, for ?var?

22
Attribute Grammar for Simple Assignment Statements
  • ?assign? ? ?var? ?expr?
  • ?expr?.expected_type ?var?.actual_type
  • ?expr? ? ?var?2 ?var?3
  • ?expr?.actual_type
  • if (?var?2.actual_typeint)
    (?var?3.actual_typeint)
  • then int
  • else real
  • ?expr?.actual_type ?expr?.expected_type
  • ?expr? ? ?var?
  • ?expr?.actual_type ?var?.actual_type
  • ?expr?.actual_type ?expr?.expected_type
  • ?var? ? A B C
  • ?var?.actual_type lookup(?var?.string)

23
Flow of Attributes for AAB
?assign?
expected_type
?expr?
actual_type
?var?
?var?
?var?
actual_type
actual_type
actual_type
A

A

B
This tree does not reflect the checking of the
Predicate Functions, in this case, just in the
?expr? node
24
Fully Attributed Parse Tree for AAB
?assign?
?expr?
expected_type int actual_type int
?var?
?var?
?var?
actual_type int string A
actual_type int string A
actual_type int string B
A

A

B
This tree does not reflect the checking of the
Predicate Functions, in this case, just in the
?expr? node
25
Operational Semantics
  • Meaning by interpretation
  • Easy to construct just build the machine
  • To know the meaning of a program, run the program
    in the machine
  • The machine provided is simple, of easy
    implementation
  • The limitation of this approach is that the
    meaning is given in the realm of computation
    only computable concepts can be understood

26
Operational Semantics (II)
  • Typical machine
  • JVM
  • von Neumann machine
  • Turing Machine!

exp1 loop if exp2 0 goto out
body exp3 goto loop out
for (exp1 exp2 exp3) body
27
Limitations of Operational Semantics
  • Because meaning is given by another machine,
    there is no reasoning about noncomputable
    properties
  • nontermination
  • function equality

28
Axiomatics Semantics
  • Meaning by using Logic
  • Assertions everywhere!
  • Each assertion establishes the state of the
    system at any given time
  • Computation changes the system from one state to
    another
  • Therefore, computation can be thought of as a
    transformer of states

29
Axiomatic Semantics (II)
  • State Transformer
  • P S Q
  • S being the statement, or construct, P is its
    precondition, and Q its postcondition
  • P being valid before S is executed implies that Q
    must be valid after S
  • The name of the game is finding the weakest
    precondition so that the postcondition holds true!

30
Example of Axiomatic Semantics
  • xgt10 sum 2x1 sumgt1
  • Why the weakest precondition?
  • The expected result of the program happens to be
    the postcondition of the program, i.e., the
    postcondition of its last statement
  • We can work all the way back to find the weakest
    precondition of the program that would satisfy
    the expected result!
  • Anything stronger that the weakest precondition
    will work

31
More Examples
Sequence
If
While
32
A Correct Program
  • n gt 0
  • count n
  • fact 1
  • while count ltgt 0 do
  • fact fact count
  • count count 1
  • end
  • fact n!

33
A Correct Program
  • n ? 0
  • count n
  • fact 1
  • count ? 0 ? n ? 0 ? fact count! n!
  • while count ltgt 0 do
  • count gt 0 ? n ? 0 ? fact count! n!
  • fact fact count
  • count count 1
  • end
  • count 0 ? n ? 0 ? fact count! n!
  • i.e., fact n!

34
Denotational Semantics
  • Meaning given by modeling each construct as a
    function
  • Maps all syntactic objects to corresponding
    semantic elements
  • The number 12 present in the program means the
    mathematical concept 12

35
Example
  • Let e n (number)
  • e1e2 (sum)
  • Eval n Number n
  • Eval e1e2 (Eval e1) (Eval e2)
  • Number n, not defined here, maps the input n
    into its corresponding mathematical concept --
    Number12 12

36
A More Complex Example
  • is in your book
  • In that example
  • They use to enclose syntactic elements
    (rather than )
  • (state s) is an extra parameter needed by C to
    compute the semantics
  • Cstmt1 stmt2
  • (state s) state Cstmt2 (C stmt1
    (s))
  • is equivalent to
  • Cstmt1 stmt2 s
  • Cstmt2 (C stmt1 (s))

37
Domains et al
  • value eval
  • eval num bool
  • num
  • bool
  • id
  • exp
  • stmt
  • state id ? value
  • values
  • expression values
  • integers
  • booleans
  • identifiers
  • expressions
  • statements
  • states (program memory)

semantic domains
syntantic domains
38
State -- Program Memory
  • state was defined as
  • state id -gt value
  • This is called an environment, or a mapping
  • To modify the mapping, use the substitution
    operator
  • _ _ / _ state ? id ? value ? state
  • svar/val x
  • if varx then val else (s x)

39
Semantic Functions
  • E is the evaluation of expressions
  • does not modify the memory -- takes an expression
    in the current memory and finds its value
  • E exp ? state ? value
  • C is the meaning of commands
  • potentially modifies the memory -- takes a
    statement in the current memory and returns a
    modified memory
  • C stmt ? state ? state

40
Denotational Semantics of Example in Book
  • C begin stmt end s
  • C stmt s
  • C stmt1 stmt2 s
  • C stmt2 (C stmt1 s)
  • C id exp s
  • sid/v, where v E exp s

41
Denotational Semantics of Example in Book (II)
  • C if exp then stmt1 else stmt2 s
  • if (E exp s)
  • then C stmt1 s
  • else C stmt2 s
  • C while exp do stmt s
  • if (E exp s)
  • then C while exp do stmt
  • (C stmt s)
  • else s

42
Specification models
  • Description of Algebraic Data Types
  • Stack Data Type
  • push stack ? int ? stack
  • pop stack ? stack
  • top stack ? int ? undefined
  • empty stack ? bool
  • size stack ? int
  • newstack ? stack

43
Algebraic Axioms
  • pop(newstack) newstack
  • pop(push(S,I)) S
  • top(newstack) undefined
  • top(push(S,I)) I
  • empty(newstack) true
  • empty(push(S,I)) false
  • size(newstack) 0
  • size(push(S,I)) size(S) 1

44
Example
  • push(pop(push(pop(push(1,newstack)),2)),3)
  • ? push(pop(push(newstack,2)),3)
  • ? push(newstack,3)

45
Other Semantics
  • Many other ways of expressing the semantics of a
    program have been invented you can always
    express the semantics if you try hard enough!
  • Simple semantics usually means simple
    implementation and a better mathematical
    foundation
Write a Comment
User Comments (0)
About PowerShow.com