1.describing syntax and semantics.pptx

Upload: nsavi16edu

Post on 05-Feb-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    1/110

    Describing Syntax and Semantics

    Introduction The General Problem of Describing

    Syntax

    Formal Methods of Describing Syntax Attribute Grammars

    Describing the Meanings of

    Programs: Dynamic Semantics

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    2/110

    Describing Syntax andSemantics

    Syntax the form or structure of the expressions statements and

    program units

    describes ho! programs "loo#": their form and structure

    Semantics the meaning of the expressions statements and

    program units

    !hich describes !hat language constructs "do"

    $xample: For example the syntax of a %a&a !hile statement

    is

    while (boolean_expr) statement

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    3/110

    The semantics of this statement formis that !hen the current &alue of the'oolean expression is true the

    embedded statement is executed( )ther!ise control continues after

    the !hile construct( Then control

    implicitly returns to the 'ooleanexpression to repeat the process(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    4/110

    Steps in a compiler:

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    5/110

    'oth lexical analysis and syntactic analysis deal !ith syntax

    !hy separate them in t!o steps in the compiler*

    + $,ciency:

    + design of the lexer is based on a -nite state automaton

    !hile the parser is a pushdo!n automaton !hich is morecomplex

    + for a non+optimi.ing compiler about /01 of compile timeis spent in the lexer

    + 2istory:

    + prior to AS3II computers had their o!n character sets+ still di4erent character sets today actually

    + it used to be hat the end+of+line character !as &ery )5Sdependent

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    6/110

    6ho must use language de-nitions*7( )ther language designers

    8( Implementers

    9( Programmers the users of thelanguage;

    A sentence is a string of characters

    o&er some

    alphabet

    A language is a set of sentences

    The General Problem of DescribingSyntax

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    7/110

    A lexeme is the lo!est le&el syntactic unit of a language e(g( " sum begin;

    A to#en is a category of lexemes e(g( identi-er;

    $xample

    index < 8 " count = 7/>

    The lexemes and to#ens of this statement are

    Lexemes Tokens

    index identi-er

    < e?ual@sign

    8 int@literal" mult@op

    3ount identi-er

    = plus@op

    7/ int@literal

    > semicolon

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    8/110

    Formal approaches to describing syntax:

    1. ecogni!ers " used in compilers

    Suppose !e ha&e a language that uses an alphabet B ofcharacters(

    To de-ne formally using the recognition method !e !ouldneed to construct a mechanism C called a recognitionde&ice capable of reading strings of characters from thealphabet B(

    C !ould indicate !hether a gi&en input string !as or !asnot in

    The syntax analysis part of a compiler is a recogni.er forthe language the compiler translates(

    The recogni.er need not test all possible strings ofcharacters from some set to determine !hether each is inthe language it need only determine !hether gi&enprograms are in the language(

    The syntax analy.er then determines !hether the gi&en

    programs are syntactically correct( The structure of syntax analy.ers also #no!n as parsers

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    9/110

    #. $enerators A language generator is a de&ice that

    can be used to generate the

    sentences of a language(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    10/110

    Formal %ethods o& DescribingSyntax

    discusses the formal language+

    generation mechanisms calledgrammars that are commonly usedto describe the syntax of

    programming languages

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    11/110

    'ontext"Free $rammars De&eloped by oam 3homs#y in the

    mid+7E0s

    anguage generators meant todescribe the syntax of naturallanguages

    De-ne a class of languages calledcontext+free languages

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    12/110

    3ontext+Free Grammar

    In linguistics and computer science a context+freegrammar 3FG; is a formal grammar in !hich e&eryproduction rule is of the form

    w

    !here is a Hnon+terminal symbol and ! is a Hstringconsisting of terminals and5or non+terminals(

    The term Jcontext+freeJ expresses the fact that the non+terminal can al!ays be replaced by ! regardless of

    the context in !hich it occurs(

    A formal language is context+free if there is a context+free grammar that generates it(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    13/110

    context+&ree grammar'F$;

    Is a set of recursi&e re!riting rules or productions; used togenerate patterns of strings(

    A 'F$consists of the follo!ing components:

    a set of terminalsymbols !hich are the characters of thealphabet that appear in the strings generated by thegrammar( K 7L

    a set of nonterminal symbols !hich are placeholders forpatterns of terminal symbols that can be generated by thenonterminal symbols( K? fL

    a set ofproductions!hich are rules for replacing orre!riting; nonterminal symbols on the left side of theproduction; in a string !ith other nonterminal or terminalsymbols on the right side of the production;(

    a start symbol !hich is a special nonterminal symbol that

    appears in the initial string generated by the grammar(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    14/110

    The syntax of !hole programminglanguages !ith minor exceptionscan be described by context+free

    grammars

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    15/110

    *ac+us ,ormal Form (1--) In&ented by %ohn 'ac#us to describe Algol

    0

    'F is e?ui&alent to context+free grammars

    'F is a meta+language( A meta+languageis a language used to describe anotherlanguage(

    In 'F abstractions are used to representclasses of syntactic structures+they act li#esyntactic &ariables also called non+terminalsymbols;

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    16/110

    A 'F descriptionor grammar is acollection of rules consists terminalsand non terminals

    e(g(N!hile@stmtO +O !hile Nlogic@exprOdo NstmtO

    This is a rule> it describes thestructure of a !hile statement

    Nif@stmtO if Nlogic@exprO ;

    NstmtO

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    17/110

    A rule has a left+hand side 2S; and aright+hand side C2S; and consists ofterminal and nonterminal symbols

    A grammar is a -nite nonempty set of rules

    An abstraction or nonterminal symbol; canha&e more than one C2S

    NstmtO +O Nsingle@stmtO

    Q begin Nstmt@listO end Syntactic lists

    described in 'F using recursion

    Nident@listO +O identQ ident Nident@listO

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    18/110

    A grammaris a generati&e de&ice forde-ning languages(

    The sentences of the language are

    generated through a se?uence ofapplications of the rules beginning !itha special nonterminal of the grammarcalled the start symbol(

    A deri&ationis a repeated application ofrules starting !ith the start symbol andending !ith a sentence all terminal

    symbols;(

    Grammars and Deri&ations

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    19/110

    In a grammar for a complete programminglanguage the start symbol represents a

    complete program and is often namedNprogramO(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    20/110

    Deri&ation of a program

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    21/110

    This deri&ation begins !ith the start symbol in this caseNprogramO( The symbol

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    22/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    23/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    24/110

    A parse tree is a hierarchicalrepresentation of a deri&ation

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    25/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    26/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    27/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    28/110

    )perator Precedence

    2o! to remo&e the ambiguity* 'y enforcing 0perator precedence

    )perator !ith igher precedence

    should be placed lower in the the tree

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    29/110

    Separate rules for di4erent

    operators

    nambiguous grammar produces a uni2ue parse tree 'ut a single parse tree can ha&e di3erent deri/ations

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    30/110

    eftmost deri&ation

    NassignO

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    31/110

    Cightmost deri&ation

    NassignO

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    32/110

    'oth of these deri&ations are represented by the sameparse tree(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    33/110

    Associati&ity of )perators

    eft Associati&ity 4e&t recursi/e grammar rule

    e(g a=b=c < a=b;=c

    e(g( A < ' = 3 = A

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    34/110

    Cight Associati&ity: ight recursi/egrammar rule e(g( a=b=c < a=b=c;

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    35/110

    6hat languages do

    SmallTal#: no operator precedence

    AP: right+associati&ity !ith no operator precedence

    + Programmers typically add parentheses !hen things aredi,cult to read

    if a N x N b; e?ui&( if a N x; N b; Ureturns 7 if b O< 7V

    For languages li#e 3 3== and %a&a the number ofprecedence le&els and operators ma#es the grammar huge

    + this ma#es the parse &ery slo!

    + a possibility is to not JencodeJ anyprecedence5associati&ity in the grammar but Wust ha&e atable that says !hat these things are %a&a 3;

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    36/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    37/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    38/110

    The syntax graph and $'Fdescriptions of the Ada ifstatement

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    39/110

    Three extensions are commonlyincluded in the &arious &ersions of$'F(

    The -rst of these denotes an optionalpart of an C2S !hich is delimited bybrac#ets( For example a 3 if+else

    statement can be described as Nif@stmtO if NexpressionO;

    NstatementO UelseNstatementOV

    5xtended *,F

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    40/110

    The second extension is the use ofbraces in an C2S to indicate that theenclosed part can be

    Nident@listO Nidenti-erO KNidenti-erOL

    This is a replacement of the recursion

    by a form of implied iteration> thepart enclosed !ithin braces can beiterated any number of timesrepeated inde-nitely or left outalto ether

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    41/110

    The third common extension deals !ithmultiple+choice options(

    6hen a single element must be chosenfrom a group the options are placed inparentheses and separated by the )Coperator Q(

    For example

    NtermO NtermO " Q 5 Q 1; NfactorO

    In 'F a description of this NtermO !ould

    re?uire the follo!ing three rules: NtermO NtermO " NfactorO

    Q NtermO 5 NfactorO

    Q NtermO 1 NfactorO

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    42/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    43/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    44/110

    Syntax in isp*

    + o expressions in ISP but functionapplicationsY

    + $&erything is parenthesi.ed(

    + o associati&ity o operator precedence(

    + 3ompare !ith the 80 pages of the %a&a manual

    + ThisZs also !hy !riting a ISP interpreter is not&ery di,cult

    + !rite a pure %a&a interpreter !ould beextremely di,cult

    + !riting a byte code interpreter is not that hard

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    45/110

    Static semantics +

    IntroductionThere are some characteristics of the structure of

    programming languages that are di,cult todescribe !ith 'F and some that are impossible(

    In %a&a for example a [oating+point &alue cannotbe assigned to an integer type &ariable althoughthe opposite is legal(

    If all of the typing rules of %a&a !ere speci-ed in

    'F the grammar !ould become too large to beuseful because the si.e of the grammardetermines the si.e of the syntax analy.er(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    46/110

    The static semantics of a language is onlyindirectly related to the meaning of programsduring execution> rather it has to do !ith thelegal forms of programs syntax rather thansemantics;(

    Static semantics is so named because theanalysis re?uired to chec# these speci-cationscan be done at compile time(

    'ecause of the problems of describing staticsemantics !ith 'F a &ariety of more po!erfulmechanisms has been de&ised for that tas#(

    )ne such mechanism attribute grammars !asdesigned by \nuth 7E]a; to describe both thesyntax and the static semantics of programs(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    47/110

    Attribute grammars are a formal approach both to describing

    and chec#ing the correctness of the

    static semantics rules of a program( Dynamic semantics

    !hich is the meaning of expressions

    statements and program units

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    48/110

    *asic concepts

    Attributes6 !hich are associated !ithgrammar symbols the terminal and non+terminal symbols; are similar to &ariables inthe sense that they can ha&e &alues assigned

    to them( Attribute computation &unctions6

    sometimes called semantic functions areassociated !ith grammar rules( They are used

    to specify ho! attribute &alues are computed( 7redicate &unctions6 !hich state the static

    semantic rules of the language are associated!ith grammar rules to chec# for attribute

    consistency(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    49/110

    An attribute grammar is a context+freegrammar G < S T P; !ith thefollo!ing additions:

    7( For each grammar symbol x there is aset Ax;of attribute &alues

    The set A^; consists of t!o disWoint sets S^;and I^; called synthesi.ed and inherited

    attributes respecti&ely( Synthesi!ed attributes are used to pass

    semantic information up a parse tree

    inherited attributes pass semantic

    information do!n and across a tree(

    Attribute $rammars De8ned

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    50/110

    8( $ach rule has a set of functions that de-necertain attributes of the non+terminals in the rule

    . For a rule ^ ^7 ((( ^n

    Semantic functions of the form S^; < fA^7; (((A^n;; 3ompute Synthesi!ed attributes from child nodes;

    Semantic functions of the form I^W; < fA^; ((( A^n;; for I N< W N< n

    3ompute 9nherited attributes from parent or siblings;

    . 7redicate &unctions

    . 'oolean expressions on the attribute setKA^; ((( A^n;L

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    51/110

    Intrinsic attributes

    9ntrinsic attributes are synthesi.edattributes of leaf nodes !hose &aluesare determined outside the parse

    tree(

    $ amples of attribute

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    52/110

    The string attribute of Nproc@nameO denoted by Nproc@nameO(stringis the actual string of characters that !ere found immediately follo!ingthe reser&ed !ord procedure by the compiler(

    Syntax rule:

    Nproc@defO procedure Nproc@nameOU7V Nproc@bodyO end

    Nproc@nameOU8V>

    Predicate:

    the predicate rule states that the name string attribute of theNproc@nameO nonterminal in the subprogram header must match thename string attribute of the Nproc@nameO nonterminal follo!ing theend of the subprogram

    Nproc@nameOU7Vstring

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    53/110

    The attributes for the nonterminals in theexample attribute grammar are describedin the follo!ing paragraphs:

    actual_typeA synthesized attributeassociated with the nonterminals

    and NexprO( It is used to store the actualtype int or real of a &ariable or expression(

    expected_typeAn inherited attributeassociated with the nonterminal NexprO( It

    is used to store the type either int or realthat is expected for the expression asdetermined by the type of the &ariable onthe left side of the assignment statement(

    $xamples of attribute

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    54/110

    $xamples of attributegrammars

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    55/110

    + only t!o types: int and real int = int is an int

    int = real is a real real = real is a real

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    56/110

    A parse tree of the sentence A < A =

    '

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    57/110

    3omputing Attribute alues

    called decorating the parse tree

    If all attributes !ere inherited thiscould proceed in a completely top+

    do!n order from the root to thelea&es(

    If all the attributes !ere synthesi.ed

    it could proceed in a completelybottom+up order from the lea&es tothe root

    The follo!ing is an e&aluation of the attributes

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    58/110

    The follo!ing is an e&aluation of the attributesin an order in !hich it is possible to computethem:

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    59/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    60/110

    $&aluation

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    61/110

    $&aluation

    3hec#ing the static semantic rules of a language is an

    essential part of all compilers( $&en if a compiler !riter has ne&er heard of an attribute

    grammar it is necessary to use fundamental ideas todesign the chec#s of static semantics rules of the compiler(

    )ne of the main di,culties in using an attribute grammar

    to describe all of the syntax and static semantics of a realcontemporary programming language is the si.e andcomplexity of the attribute grammar(

    The large number of attributes and semantic rules re?uiredfor a complete programming language ma#e such

    grammars di,cult to !rite and read( the attribute &alues on a large parse tree are costly to

    e&aluate(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    62/110

    Semantics

    There is no single !idely acceptable notation or formalism fordescribing semantics

    Semantics description tool is useful for: 'etter understanding the statements of a language

    De&eloping more e4ecti&e compiler

    Program correctness proofs

    ;hree approaches 0perational

    Axiomatic

    Denotational

    0perational Semantics Describe the meaning of a program by executing its statements on a machine

    either simulated or actual(

    The change in the state of the machine memory registers etc(; de-nes themeaning of the statement

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    63/110

    natural operational semantics

    At the highest le&el the interest is in the-nal result of the execution of acomplete program(

    structural operational semantics At the lo!est le&el operational

    semantics can be used to determine the

    precise meaning of a program throughan examination of the completese?uence of state changes that occur!hen the program is executed(

    ' i P

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    64/110

    'asic Process language is clarity

    to design an appropriate intermediate language $&ery construct of the intermediate language must

    ha&e an ob&ious and unambiguous meaning(

    If the natural operational semantics is used a

    &irtual machine an interpreter; must beconstructed for the intermediate language(The &irtual machine can be used to execute either

    single statements code segments or !hole programs(

    The semantics description can be used !ithout a&irtual machine if the meaning of a single statement isall that is re?uired(

    If structural operational semantics is used theintermediate code can be &isually inspected(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    65/110

    semantics of the 3 &or construct $xample

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    66/110

    $&aluation

    Good if used informally language manuals etc(;

    $xtremely complex if used formally e(g( iennaDe-nition anguage + D; it !as used fordescribing semantics of P5I P5I stands for

    JProgramming anguage 7J( P5I !as anantecedent of the 3programming language!hich essentially replaced it as an all+purposeserious programming language;(

    seful for language users and implementors 'ased on algorithms rather than mathematics

    http://searchwinit.techtarget.com/definition/Chttp://searchwinit.techtarget.com/definition/C
  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    67/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    68/110

    It is an approach of formali.ing themeanings of programming languagesby constructing mathematical

    obWects called denotations; thatdescribe the meanings ofexpressions from the languages

    The method is named denotationalbecause the mathematical objectsdenote the meaning o theircorresponding syntactic entities(

    Denotational semantics

    http://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Programming_language
  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    69/110

    'uilding Denotational speci8cation fora language: De-ne a ,umber for each grammar symbol

    De-ne a &unction !hich con&erts:

    Grammar symbol +O corresponding numbers

    Denotational /s operational

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    70/110

    Denotational /s operationalsemantics

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    71/110

    The mapping functions of a denotationalsemantics programming languagespeci-cation li#e all functions in

    mathematics ha&e a domain and a range(The domainis the collection of &alues that are

    legitimate parameters to the function it iscalled the syntactic domain

    the rangeis the collection of obWects to !hichthe parameters are mapped it is called thesemantic domain(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    72/110

    $xample

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    73/110

    $xample

    6e !ant to associate a semantic to each 'F

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    74/110

    6e !ant to associate a semantic to each 'Fproduction

    6e de-ne a function M@bin that maps the

    syntactic obWects in the grammar rules to theobWects in the set of non negati&e integers(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    75/110

    ;he state o& a program

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    76/110

    ;he state o& a program )perational semantics:

    ;he change in the state of the machine Memory registers etc(

    The state change is de-ned by coded algorithms Denotational semantics:

    ;he change in the /alues o& the program

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    77/110

    5xpressions Semantic function: maps expressions to

    integer &alue or error;

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    78/110

    Assignment Statements Semantic function: maps state to state

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    79/110

    4ogical 7retest 4oops Semantic function: maps state to state

    $xisting functions Mb: maps boolean expression to boolean

    &alues true false undef;

    Msl: maps statement lists to states

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    80/110

    5/aluation o& denotational

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    81/110

    5/aluation o& denotationalsemantics:

    6hen a complete system has been de-nedfor a gi&en language it can be used todetermine the meaning of completeprograms in that language( i(e; Pro&ides arigorous !ay to analy.e programs

    3an be used to pro&e the correctness ofprograms

    3an be an aid to language design andcompiler generation

    'ut too complex for language users

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    82/110

    A i ti S ti

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    83/110

    Axiomatic semanticsis an approachbased on mathematical logicto pro&ingthe correctness of computer programs

    Goal: program correctness proo&(i.e) Cather than directly specifying themeaning of a program axiomatic semanticsspeci-es !hat can be pro&en about theprogram(

    Axiomatic Semantics

    http://en.wikipedia.org/wiki/Mathematical_logichttp://en.wikipedia.org/wiki/Correctness_of_computer_programshttp://en.wikipedia.org/wiki/Correctness_of_computer_programshttp://en.wikipedia.org/wiki/Correctness_of_computer_programshttp://en.wikipedia.org/wiki/Correctness_of_computer_programshttp://en.wikipedia.org/wiki/Mathematical_logic
  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    84/110

    In axiomatic semantics there is no model of thestate of a machine or program or model of statechanges that ta#e place !hen the program isexecuted(

    The meaning of a program is based onrelationships among program &ariables andconstants !hich are the same for e&eryexecution of the program(

    Axiomatic semantics has t!o distinctapplications: program &eri-cation

    program semantics speci-cation(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    85/110

    Approach Specify constraints for each statement

    by logical expressions called assertions (orpredicate)

    7re"condition: an assertion be&ore a statement

    Describes the constraints on the &ariables at thatpoint

    7ost"condition: an assertion &ollowing a statement

    Describes the ne! constraints on those &ariables

    = + t diti

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    86/110

    =ea+est precondition:

    The least restricti/e precondition that !illguarantee the postcondition(

    Pre+post form otation: KPL S K`L

    S:program statement

    P:constraints on &ariables '$F)C$ statementexecution called a JpreconditionJ

    `: constraints on &ariables AFT$C statementexecution called a JpostconditionJ

    5xample: a < b = 7 Ka O 7L )ne possible precondition: Kb O 7L

    6ea#est precondition: Kb O L

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    87/110

    An in&erence rule is a method of inferring the truth of oneassertion on the basis of the &alues of other assertions( Thegeneral form of an inference rule is as follo!s:

    This rule states that if S7 S8 ( ( ( and Sn are true thenthe truth of S can be inferred(

    The top part of an inference rule is called its antecedent>

    the bottom part is called its conse2uent(

    An axiomis a logical statement that is assumed to be true(Therefore an axiom is an inference rule !ithout anantecedent(

    P f 7 &

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    88/110

    Process of 7rogram proo&

    Assignment Statements

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    89/110

    et Jx ?x"@5 or ?x"@5B x > 5 ?B P is computed as ` in !hich all occurrences of x

    ha&e been replaced by $

    + $xample:

    a < b 5 8 + 7 Ka N 7L

    the !ea#est precondition is: P < K b 5 8 + 7N 7L !hich reduces to Kb N 88L(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    90/110

    The usual notation for specifying theaxiomatic semantics of a gi&en statementform is

    KPLSK`L !here P is the precondition ` is the

    postcondition and S is the statement form(

    In the case of the assignment statement

    the notation is

    e(g( Pro&e the correctness of Kx O 9L x < x + 9

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    91/110

    K x O L

    Ans!er: Kx X 9 O L Kx O 9L thus TC$

    e(g( 2o! about x @ B x < x + 9 K x O L *Since Kx O 0L Kx O 9L it is still TC$

    the rule of conse?uence says that a postconditioncan al!ays be !ea#ened and a precondition canal!ays be strengthened(

    Se?uences

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    92/110

    Se?uences The !ea#est precondition for a se?uence of

    statements cannot be described by an axiombecause the precondition depends on the particular#inds of statements in the se?uence(

    So the precondition can only be described !ith aninference rule(

    et S7 and S8 be adWacent program statements( If S7and S8 ha&e the follo!ing pre+ and post+conditions

    Th i f l t t th t t t

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    93/110

    The inference rule states that to getthe se?uence precondition the

    precondition of the second statementis computed(

    This ne! assertion is then used as

    the postcondition of the -rststatement !hich can then be usedto compute the precondition of the

    -rst statement !hich is also theprecondition of the !hole se?uence(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    94/110

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    95/110

    Selection

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    96/110

    Selection

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    97/110

    ogical Pretest oops

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    98/110

    ogical Pretest oops

    3omputing the !ea#est precondition for a while loop isinherently more di,cult than for a se?uence because thenumber of iterations cannot al!ays be predetermined(

    6hen the number of iterations is #no!n the loop can beunrolled and treated as a se?uence(

    The principal step in induction is -nding an inducti&ehypothesis(

    The corresponding step in the axiomatic semantics of awhile loop is -nding an assertion called a loop in/ariant(loop in&ariant is a condition that is necessarily trueimmediately before and immediately after each iterationof a loop) !hich is crucial to -nding the !ea#estprecondition(

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    99/110

    loop in&ariant 3haracteristics

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    100/110

    loop in&ariant 3haracteristics

    P

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    101/110

    To -nd I the loop postcondition ` is used to

    compute preconditions for se&eral di4erentnumbers of iterations of the loop body

    In general !ea#est precondition is gi&en by!pstatement postcondition; < precondition

    A !p function is often called a predicate

    trans&ormer because it ta#es a predicate orassertion as a parameter and returns anotherpredicate(

    $xample

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    102/110

    p while y NO x do y < y = 7 end Ky < xL

    ote: e?ual sign

    outside assertions it means the assignment operator(

    For .ero iterations

    Ky < xL

    For one iteration it is

    !py < y = 7 Ky < xL; < Ky = 7 < xL or Ky

    < x + 7L For t!o iterations it is

    !py < y = 7 Ky < x + 7L;

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    103/110

    6e must ensure that our choice satis-esthe four criteria for I for our example loop(

    7( P < I P

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    104/110

    9( It must be true that KI and not ';L

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    105/110

    If loop termination can be sho!n theaxiomatic description of the loop iscalled total correctness(

    If the other conditions can be metbut termination is not guaranteed itis called partial correctness(

    Program Proofs

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    106/110

    Program Proofs

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    107/110

    $&aluation

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    108/110

    $&aluation

    De&eloping axioms or inference rules forall of the statements in a language isdi,cult

    It is a good tool for correctness proofsand an excellent frame!or# for reasoningabout programs but it is not as useful forlanguage users and compiler !riters

    Its usefulness in describing the meaningof a programming language is limited forlanguage users or compiler !riters

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    109/110

    $&aluation of axiomatic semantics: It is hard to de&elop axioms or

    inference rules

    for all of the statements in alanguage

    ice tool for correctness proofs

    'ut not for language users andcompiler !riters

    Summary

  • 7/21/2019 1.Describing Syntax and Semantics.pptx

    110/110

    Summary