Sunum yükleniyor. Lütfen bekleyiniz

Sunum yükleniyor. Lütfen bekleyiniz

Describing Syntax and Semantics

Benzer bir sunumlar


... konulu sunumlar: "Describing Syntax and Semantics"— Sunum transkripti:

1 Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics

2 Chapter 3 Topics Giriş Introduction
Yazım kurallarını tanımlamanın genel problemleri The General Problem of Describing Syntax Yazım kuralları tanımlamanın formal Metotları Formal Methods of Describing Syntax Öznitelik Gramerleri Attribute Grammars Programların anlamlarını tanımlama : Dinamik AnlamlarDescribing the Meanings of Programs: Dynamic Semantics Copyright © 2012 Addison-Wesley. All rights reserved.

3 Introduction Syntax: Program birimi, expression yapısı, ve Statement yapısı the form or structure of the expressions, statements, and program units Semantics: Expression, statement yada program biriminin anlamı the meaning of the expressions, statements, and program units Syntax ve semantics bir dilin tanımını yapmaktadır. Syntax and semantics provide a language’s definition Bir Dil tanımının kullanıcıları Users of a language definition Diğer dil tasarımcıları Other language designers Uygulayıcılar Implementers Programcılar Programmers (the users of the language) Copyright © 2012 Addison-Wesley. All rights reserved.

4 Statement Example For example, the syntax of a Java while statement is
while (boolean_expr) statement Describing syntax is easier than describing semantics, partly because a con- cise and universally accepted notation is available for syntax description, but none has yet been developed for semantics. Copyright © 2012 Pearson Education. All rights reserved.

5 Yazım kurallarını tanımlama genel problemleri: TerminolojiThe General Problem of Describing Syntax: Terminology Cümle bir alfabedeki karakterler dizisidir. A sentence is a string of characters over some alphabet Dil cümleler kümesidir.A language is a set of sentences Sözcükbirim bir dilin en düşük sentetik düzeyidir A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) Jeton sözlükbirim kategorisidir. A token is a category of lexemes (e.g., identifier) Copyright © 2012 Addison-Wesley. All rights reserved.

6 Copyright © 2012 Pearson Education. All rights reserved.

7 Dillerin Formal Tanımı Formal Definition of Languages
Tanıtıcılar Recognizers Bir tanıma aracı, alfabeden biri giriş disizi okur ve bunu Dile ait olup olmadığına karar verir. A recognition device reads input strings over the alphabet of the language and decides whether the input strings belong to the language Example: bir derleyicinin yazım kural analizi syntax analysis part of a compiler - Detailed discussion of syntax analysis appears in Chapter 4 Üreticiler Generators Bir dile ait cümle üreten araç. A device that generates sentences of a language Üreticinin yapısı kıyaslanarak, belirli bir cümlenin syntaxsı ın doğruluğu sentetik olarka belirlenebilir. One can determine if the syntax of a particular sentence is syntactically correct by comparing it to the structure of the generator Copyright © 2012 Addison-Wesley. All rights reserved.

8 BNF ve İçerik bağımsız Gramer BNF and Context-Free Grammars
İçerik Bağımsız Gramerler Context-Free Grammars 1950 ortlarında Noam Chomsky tarafından gelişti Developed by Noam Chomsky in the mid-1950s Dil Üreteciler, doğal dilleri tanımlamak için kullanılanılır. Language generators, meant to describe the syntax of natural languages İçerik bağımsız dil sınıfı tanımlama Define a class of languages called context-free languages Backus-Naur Form (1959) Algol 58 yazımını tanımlamak için Johm Backus Invented by John Backus to describe the syntax of Algol 58 BNF içerik bağımsız gramer denkidir. BNF is equivalent to context-free grammars Copyright © 2012 Addison-Wesley. All rights reserved.

9 <assign> → <var> = <expression>
BNF uses abstractions for syntactic structures. A simple Java assignment statement, for example, might be represented by the abstraction <assign> (pointed brackets are often used to delimit names of abstractions). The actual definition of <assign> can be given by <assign> → <var> = <expression> Copyright © 2012 Pearson Education. All rights reserved.

10 BNF temelleri BNF Fundamentals
A metalanguage is a language that is used to describe another language. BNF is a metalanguage for programming languages. BNF de, kelimesel sınıf yapısını tanımlamak için soyutlama kullanılır. Kelimesel değişken olarak kullanılırlar ( terminal yada terminal olamayan sembol adı verilir)In BNF, abstractions are used to represent classes of syntactic structures--they act like syntactic variables (also called nonterminal symbols, or just terminals) Terminaller sözlükbirim veya jetondur Terminals are lexemes or tokens Bir kural sol tarafı Terminal olamayan, yada terminal olan olmayan sağ tarafı vardır A rule has a left-hand side (LHS), which is a nonterminal, and a right-hand side (RHS), which is a string of terminals and/or nonterminals Copyright © 2012 Addison-Wesley. All rights reserved.

11 BNF temelleri BNF Fundamentals (continued)
Terminal olmayanlar genellikle küçük büyük parantezi arasında gösterilir. Nonterminals are often enclosed in angle brackets Examples of BNF rules: <ident_list> → identifier | identifier, <ident_list> <if_stmt> → if <logic_expr> then <stmt> Gramer: boş olamayan sonlu kurallar kümesi Grammar: a finite non-empty set of rules Başlangıç sembolü terminal olamyanların gramerinin özel bir elemantidir A start symbol is a special element of the nonterminals of a grammar Copyright © 2012 Addison-Wesley. All rights reserved.

12 <if_stmt> → if ( <logic_expr> ) <stmt>
| OR <if_stmt> → if ( <logic_expr> ) <stmt> <if_stmt> → if ( <logic_expr> ) <stmt> else <stmt> or with the rule | if ( <logic_expr> ) <stmt> else <stmt> Copyright © 2012 Pearson Education. All rights reserved.

13 BNF Rules Bir soyutlama ( yada terminal olamyan sembolün) Birden fazla RHS olabilir. An abstraction (or nonterminal symbol) can have more than one RHS <stmt>  <single_stmt> | begin <stmt_list> end Copyright © 2012 Addison-Wesley. All rights reserved.

14 Listeleri tanımlama Describing Lists
Yazım kuları listesi özyinelimi olarak tanımlanır Syntactic lists are described using recursion <ident_list>  ident | ident, <ident_list> Türev kuralların tekrarlı uyulanmasıdır. Bşalangıç sembolü ile başlama ve cümle ile bitme A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) Copyright © 2012 Addison-Wesley. All rights reserved.

15 Örnek Gramer An Example Grammar
<program>  <stmts> <stmts>  <stmt> | <stmt> ; <stmts> <stmt>  <var> = <expr> <var>  a | b | c | d <expr>  <term> + <term> | <term> - <term> <term>  <var> | const A Grammar for a Small Language <program> → begin <stmt_list> end <stmt_list> → <stmt> | <stmt> ; <stmt_list> <stmt> → <var> = <expression> <var> →A|B|C <expression> → <var> + <var> | <var> – <var> | <var> Copyright © 2012 Addison-Wesley. All rights reserved.

16 An Example Derivation <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const Copyright © 2012 Addison-Wesley. All rights reserved.

17 Türevler Derivations Her bir semboller dizisi bir cümlelerle alakalı formdur. Every string of symbols in a derivation is a sentential form Bir cümle sadece terminal sembollerine sahip sentential formdur. A sentence is a sentential form that has only terminal symbols En sol türev, en sol terminal olmayan açılan her bir sentential form içindeki türev A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost Copyright © 2012 Addison-Wesley. All rights reserved.

18 Copyright © 2012 Pearson Education. All rights reserved.

19 Copyright © 2012 Pearson Education. All rights reserved.

20 Copyright © 2012 Pearson Education. All rights reserved.

21 Parse Tree Türevin Hiyerarşik gösterimiA hierarchical representation of a derivation <program> <stmts> <stmt> <var> = <expr> a <term> + <term> <var> const b Copyright © 2012 Addison-Wesley. All rights reserved.

22 Every internal node of a parse tree is labeled with a nonterminal sym- bol; every leaf is labeled with a terminal symbol. Every subtree of a parse tree describes one instance of an abstraction in the sentence Copyright © 2012 Pearson Education. All rights reserved.

23 Gramerdeki Çok anlamlılık Ambiguity in Grammars
Bir gramar sadece ve sadece bir sentential formdan 2 yada daha fazla ayrı parse tree üretilebilirse Çok anlamlıdır. A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees Copyright © 2012 Addison-Wesley. All rights reserved.

24 An Ambiguous Expression Grammar
<expr>  <expr> <op> <expr> | const <op>  / | - <expr> <expr> <expr> <op> <expr> <expr> <op> <op> <expr> <expr> <op> <expr> <expr> <op> <expr> const - const / const const - const / const Copyright © 2012 Addison-Wesley. All rights reserved.

25 <assign> → <id> = <expr> <id> →A|B|C
<expr> → <expr> + <expr> | <expr> * <expr> |( <expr> ) | <id> Copyright © 2012 Pearson Education. All rights reserved.

26 An Unambiguous Expression Grammar
If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity <expr>  <expr> - <term> | <term> <term>  <term> / const| const <expr> <expr> - <term> <term> <term> / const const const Copyright © 2012 Addison-Wesley. All rights reserved.

27 A = B + C * A Copyright © 2012 Pearson Education. All rights reserved.

28 Operatörlerdeki Birleşme Associativity of Operators
Operatör birleşim öz. Bir gramerlede gösterilebilir. Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous) <expr> <expr> <expr> + const <expr> + const const Copyright © 2012 Addison-Wesley. All rights reserved.

29 Gelişmiş BNF Extended BNF
İsteğe bağlı parçalar [ ] içinde gösterilir Optional parts are placed in brackets [ ] <proc_call> -> ident [(<expr_list>)] RHS ların altermatif kısımları parantez içine yerleştirilir ve | ile ayrılırAlternative parts of RHSs are placed inside parentheses and separated via vertical bars <term> → <term> (+|-) const 0veya fazla tekrarlar { } içine yerleşritilir. Repetitions (0 or more) are placed inside braces { } <ident> → letter {letter|digit} Copyright © 2012 Addison-Wesley. All rights reserved.

30 BNF and EBNF BNF <expr>  <expr> + <term> EBNF
<term>  <term> * <factor> | <term> / <factor> | <factor> EBNF <expr>  <term> {(+ | -) <term>} <term>  <factor> {(* | /) <factor>} Copyright © 2012 Addison-Wesley. All rights reserved.

31 Yeni EBNF varyasyonları Recent Variations in EBNF
Alternatif RHS lar farklı satırlara konulmuştur Alternative RHSs are put on separate lines => yerine kolon kullanılır Use of a colon instead of => opt isteğe bağlı kısımlar için kullanılır.Use of opt for optional parts Oneof seçimler için kullanılır se of oneof for choices Copyright © 2012 Addison-Wesley. All rights reserved.

32 Sabit Anlam Static Semantics
Anlmala bir ilişkisi yoktur. Nothing to do with meaning İçirik bağımsız gramerler programlama dillerinin tüm yazımlarını tanımlamazlarContext-free grammars (CFGs) cannot describe all of the syntax of programming languages Problemli olan Yapıcıların katagorileri Categories of constructs that are trouble: - İçerik bağımsız fakat, kullanımı zorContext-free, but cumbersome (e.g. types of operands in expressions) - İçerikten bağımzıs olmayan Non-context-free (e.g., variables must be declared before they are used) Copyright © 2012 Addison-Wesley. All rights reserved.

33 Öznitelik Grameri Attribute Grammars AG
Öznitelik Gramerleri parse ağaç düğümlerine anlamsal bilgi taşımak için CFGlere ektir Attribute grammars (AGs) have additions to CFGs to carry some semantic info on parse tree nodes AG lerin birincil değerleri Primary value of AGs: Sabit anlamsal özelliklerStatic semantics specification Derleyici tasarımı Compiler design (static semantics checking) Copyright © 2012 Addison-Wesley. All rights reserved.

34 AG tanımı Attribute Grammars : Definition
Def: bir AG CFG dir G = (S, N, T, P) aşağıdaki eklerleAn attribute grammar is a context-free grammar G = (S, N, T, P) with the following additions: Herbir x gramer sembolü için bir A(x) öznitelik semboller kümesi vardır For each grammar symbol x there is a set A(x) of attribute values Her bir kuralı fonksiyonlar kümesi vardır. Bunlar kural içindeki termial olmayanlar için belirli öznitelik sağlarlarEach rule has a set of functions that define certain attributes of the nonterminals in the rule Özniteliğin tutarlılığını kontrol etmek için predicate kümelerine sahiptir) muhtemel boş Each rule has a (possibly empty) set of predicates to check for attribute consistency Copyright © 2012 Addison-Wesley. All rights reserved.

35 AG tanımı Attribute Grammars: Definition
X0  X1 ... Xn bir kural olsun Let X0  X1 ... Xn be a rule S(X0) = f(A(X1), ... , A(Xn)) form fonksiyonu, sentezlenmiş öznitelikleri temsil eder.Functions of the form S(X0) = f(A(X1), ... , A(Xn)) define synthesized attributes I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, fom fonksiyonu, miras öznitelikleri temsil eder Functions of the form I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, define inherited attributes Başlangıçta, yapraklarda saf özellikler vardır çthere are intrinsic attributes on the leaves Copyright © 2012 Addison-Wesley. All rights reserved.

36 AG örnekAttribute Grammars: An Example
Syntax <assign> -> <var> = <expr> <expr> -> <var> + <var> | <var> <var> A | B | C actual_type: synthesized for <var> and <expr> expected_type: inherited for <expr> Copyright © 2012 Addison-Wesley. All rights reserved.

37 Attribute Grammar (continued)
Syntax rule: <expr>  <var>[1] + <var>[2] Semantic rules: <expr>.actual_type  <var>[1].actual_type Predicate: <var>[1].actual_type == <var>[2].actual_type <expr>.expected_type == <expr>.actual_type Syntax rule: <var>  id Semantic rule: <var>.actual_type  lookup (<var>.string) Copyright © 2012 Addison-Wesley. All rights reserved.

38 Attribute Grammars (continued)
Öznitelik değerleri nasıl hesaplanırHow are attribute values computed? Eğer tüm öznitelikler miras alınmışsa, Ağaç tepeden aşağı olarak doldurulur. If all attributes were inherited, the tree could be decorated in top-down order. Eğer tüm öznitelikler sentezlenmişse, ağaç aşağıdan yukarı doğru doldurulabilir. If all attributes were synthesized, the tree could be decorated in bottom-up order. Bir çok durumda, iki tip öznitelik kullanılmaktadır. bu durumda aşağıdan yukarı yada yukardan aşağının bir şekilde kullanılmalıdır.In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used. Copyright © 2012 Addison-Wesley. All rights reserved.

39 Attribute Grammars (continued)
<expr>.expected_type  inherited from parent <var>[1].actual_type  lookup (A) <var>[2].actual_type  lookup (B) <var>[1].actual_type =? <var>[2].actual_type <expr>.actual_type  <var>[1].actual_type <expr>.actual_type =? <expr>.expected_type Copyright © 2012 Addison-Wesley. All rights reserved.

40 AnlamlarSemantics Genel olarak kabul görmüş semantiği anlatan bir notasyon yada formül yoktur There is no single widely acceptable notation or formalism for describing semantics Semantiğe bir metot ve notasayon için ihtiyaçlar için Several needs for a methodology and notation for semantics: Programcı ifadelerin en anlamda olduğunu bilmelidir.Programmers need to know what statements mean Derleyici yazanlar dil yapıları tyam olarak ne yaptığını bilmeli Compiler writers must know exactly what language constructs do Doğruluk isğatları mümkün olmalı Correctness proofs would be possible Derleyici üreticileri mümkün olmalı Compiler generators would be possible Tasarımcılar çoklu anlamlılığı ve tutarsızlıkları belirleyebilirler Designers could detect ambiguities and inconsistencies Copyright © 2012 Addison-Wesley. All rights reserved.

41 İşlemsel Anlamlılık Operational Semantics
Anlamını gerçek kodu çalıştırarak ortaya çıkarma. Makinedeki değişiklikler onun anlamını ortaya koyacaktır. 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.) defines the meaning of the statement Yüksek seviye bir dil için işlemsel anlamlılığı kullanmak sanal makine geretirecektir. To use operational semantics for a high-level language, a virtual machine is needed Copyright © 2012 Addison-Wesley. All rights reserved.

42 Operational Semantics
Donanım olarak tasarlanmış bir Tercüme çok pahalı olacaktırA hardware pure interpreter would be too expensive Tam yazılım tercümelerinde problemleri olacaktır.A software pure interpreter also has problems Belirli bilgisayarların detaylı karakteristikleri eylemlerin anlaşılırlılığını zorlaştıracaktırç The detailed characteristics of the particular computer would make actions difficult to understand Bu tip anlam tanımları makine bağımlı olacaktırSuch a semantic definition would be machine- dependent Copyright © 2012 Addison-Wesley. All rights reserved.

43 Operational Semantics (continued)
Daha iyi bir alternatif: Tam bilgisauar simülasyonu A better alternative: A complete computer simulation Süreç The process: Ideal bir bilgisayara ait makine koduna çeviren bir çevirici hazırlaBuild a translator (translates source code to the machine code of an idealized computer) Ileal bilgisayar için bir simülasyon hazırla hazBuild a simulator for the idealized computer İşlemsel semantiğin değerlendirilmesi Evaluation of operational semantics: Bilgi veriçi şekilde kullanılırsa iyi Good if used informally (language manuals, etc.) Eğer formal şekilde yapılırsa aşırı derecede zor Extremely complex if used formally (e.g., VDL), it was used for describing semantics of PL/I. Copyright © 2012 Addison-Wesley. All rights reserved.

44 Operational Semantics (continued)
İşlemsel semantiğin kulanımı Uses of operational semantics: - Dil klavuzları ve kitapları Language manuals and textbooks - Programlama dilini öğretmekTeaching programming languages İşlemsel semantiğin kullanımının 2 farklı seviyesi Two different levels of uses of operational semantics: - Doğal işlemsel semantik Natural operational semantics -Yapısal işlemsel semantikStructural operational semantics Değerlendirme Evaluation - Good if used informally (language manuals, etc.) - Extremely complex if used formally (e.g.,VDL) Copyright © 2012 Addison-Wesley. All rights reserved.

45 Denotational Semantics
Öz yineli fonksiyon teorisine bağlıdır. Based on recursive function theory En soyut anlam tanıma metoduThe most abstract semantics description method Orjinal olarak Sott and Strachey tarafından geliştirildi Originally developed by Scott and Strachey (1970) Copyright © 2012 Addison-Wesley. All rights reserved.

46 Denotational Semantics - continued
Bir Dil için Denotational özellikler geliştirme süreci The process of building a denotational specification for a language: - Her bir dil varlığı için matematik nesne tanımla Define a mathematical object for each language entity Dil varlıkları örneklerini matematik nesne örneklerine eşleyen bir fonksiyon tanımla Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects Dil yapıcılarının anlamı program değişkenlerinin değerleri ile tanımlanırThe meaning of language constructs are defined by only the values of the program's variables Copyright © 2012 Addison-Wesley. All rights reserved.

47 Denotational Semantics: program state
Bir programın durumu o anki değişlenlerinin değerleridir The state of a program is the values of all its current variables s = {<i1, v1>, <i2, v2>, …, <in, vn>} VARMAP, bir değişken adı ve durumu verildğinde o anki değerini döndüren bir fonksiyon olsunLet VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(ij, s) = vj Copyright © 2012 Addison-Wesley. All rights reserved.

48 Decimal Numbers <dec_num>  '0' | '1' | '2' | '3' | '4' | '5' |
'6' | '7' | '8' | '9' | <dec_num> ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9 Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>) Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1 Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9 Copyright © 2012 Addison-Wesley. All rights reserved.

49 İfadeler expressions Z  {error} üzerine ifadeleri eşleştir Map expressions onto Z  {error} ifadelerin tam sayı, değişken, yada bir aritmetik işlem iki operant içeren binary ifadelerdirWe assume expressions are decimal numbers, variables, or binary expressions having one arithmetic operator and two operands, each of which can be an expression Copyright © 2012 Addison-Wesley. All rights reserved.

50 Expressions else if (<binary_expr>.<operator> == '+' then
Me(<expr>, s) = case <expr> of <dec_num> => Mdec(<dec_num>, s) <var> => if VARMAP(<var>, s) == undef then error else VARMAP(<var>, s) <binary_expr> => if (Me(<binary_expr>.<left_expr>, s) == undef OR Me(<binary_expr>.<right_expr>, s) = undef) else if (<binary_expr>.<operator> == '+' then Me(<binary_expr>.<left_expr>, s) + Me(<binary_expr>.<right_expr>, s) else Me(<binary_expr>.<left_expr>, s) * ... Copyright © 2012 Addison-Wesley. All rights reserved.

51 Assignment Statements
Maps state sets to state sets U {error} Ma(x := E, s) = if Me(E, s) == error then error else s’ = {<i1,v1’>,<i2,v2’>,...,<in,vn’>}, where for j = 1, 2, ..., n, if ij == x then vj’ = Me(E, s) else vj’ = VARMAP(ij, s) Copyright © 2012 Addison-Wesley. All rights reserved.

52 Logical Pretest Loops Maps state sets to state sets U {error}
Ml(while B do L, s) = if Mb(B, s) == undef then error else if Mb(B, s) == false then s else if Msl(L, s) == error else Ml(while B do L, Msl(L, s)) Copyright © 2012 Addison-Wesley. All rights reserved.

53 Loop Meaning Program değişkenlerinin değerleri belirli bir sayı kadar oradaki ifadelerle birlikte çalıştırılmasıdırThe meaning of the loop is the value of the program variables after the statements in the loop have been executed the prescribed number of times, assuming there have been no errors Özde, döngüler tekrardan-> özyineliye dönüşütürülmüştür. Özyineli kontrol matematiksel olarak başka eşleme fonksiyonu olarak tanımlanmıştır.where the recursive control is mathematically defined by other recursive state mapping functions - Özyineleme matematiksel olarak tekrardan daha iyi anlatılabilirRecursion, when compared to iteration, is easier to describe with mathematical rigor Copyright © 2012 Addison-Wesley. All rights reserved.

54 Evaluation of Denotational Semantics
Programların doğruluğunu kanıtlamak için kullanılabilir. Can be used to prove the correctness of programs Programlar hakkında düşünmek için çok ayrıntılı bir yol sağlar Provides a rigorous way to think about programs Dil tasarımına faydası olabilir Can be an aid to language design Derleyici üretim sistemlerinde kullanılmıştır. Has been used in compiler generation systems Karışık olduğu için dil kullanıcılarına pek fayda sağlamaz Because of its complexity, it are of little use to language users Copyright © 2012 Addison-Wesley. All rights reserved.

55 Axiomatic Semantics Formal mantığa dayanmıştır. Based on formal logic (predicate calculus) Orjinal amaç: formal program doğrulaması Original purpose: formal program verification Axiom veya Axioms or sonuç çıkarma kuralları her bir ifade tipi için dil içinde tanımlanmiştır inference rules are defined for each statement type in the language (to allow transformations of logic expressions into more formal logic expressions) Mantık ifadeleri ibareleri acıklama olarak tanımlanır The logic expressions are called assertions Copyright © 2012 Addison-Wesley. All rights reserved.

56 Axiomatic Semantics (continued)
Bir ifadeden önceki açıklama o çalışma noktasında doğru olan değişkenlerin ilişkilerini ve kısıtlarını belirler. An assertion before a statement (a precondition) states the relationships and constraints among variables that are true at that point in execution Bir ifadeyi takip eden açıklama postcondition dır An assertion following a statement is a postcondition En zayıf precondition en az kısıtlayıcı olan ve postconditionı garanti edendir.A weakest precondition is the least restrictive precondition that will guarantee the postcondition Copyright © 2012 Addison-Wesley. All rights reserved.

57 Axiomatic Semantics Form
Pre-, post form: {P} statement {Q} An example a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0} Copyright © 2012 Addison-Wesley. All rights reserved.

58 Program Proof Process Postcondition tüm program için istenen sonuçtur. The postcondition for the entire program is the desired result Ilk statement a kadar tüm programı geri çalıştır. Eğer ilk statement içindeki preconditon program specifikasyonu ile aynı ise, program doğrudur.Work back through the program to the first statement. If the precondition on the first statement is the same as the program specification, the program is correct. Copyright © 2012 Addison-Wesley. All rights reserved.

59 Axiomatic Semantics: Assignment
An axiom for assignment statements (x = E): {Qx->E} x = E {Q} The Rule of Consequence: Copyright © 2012 Addison-Wesley. All rights reserved.

60 Axiomatic Semantics: Sequences
S1 S2 sıralı formları içim anlam çıkarma kuralı An inference rule for sequences of the form S1; S2 {P1} S1 {P2} {P2} S2 {P3} Copyright © 2012 Addison-Wesley. All rights reserved.

61 Axiomatic Semantics: Selection
Seçim için anlam çıkarma kuralı An inference rules for selection - if B then S1 else S2 {B and P} S1 {Q}, {(not B) and P} S2 {Q} {P} if B then S1 else S2 {Q} Copyright © 2012 Addison-Wesley. All rights reserved.

62 Axiomatic Semantics: Loops
Düngüler için anlam çıkarma kuralı An inference rule for logical pretest loops {P} while B do S end {Q} I donguden bağımsız where I is the loop invariant (the inductive hypothesis) Copyright © 2012 Addison-Wesley. All rights reserved.

63 Axiomatic Semantics: Axioms
Dongüden bağımsızlığın karakteristikleri :I aşağıdaki şartları sağlamalı Characteristics of the loop invariant: I must meet the following conditions: P => I Düngü bağımsızlığı başlangıçta doğru olmalı the loop invariant must be true initially {I} B {I} Boolean değerlendirmesi I nın geçerlilğini değiştirmemeli I nın evaluation of the Boolean must not change the validity of I {I and B} S {I} -- I döngünün gövdesinin çalışması ile değişmemeli I is not changed by executing the body of the loop (I and (not B)) => Q eğer I doğru ve B yanlışsa Q anlaşılır if I is true and B is false, Q is implied The loop terminates kanıtı zor olabilir.can be difficult to prove Copyright © 2012 Addison-Wesley. All rights reserved.

64 Loop Invariant Döngü değişmezi I, postconditon ın zayıflamış hali ve anyı zamanda precondition dırThe loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. I önce döngünün başlangıcında yerine getirilebilecek kadar zayıf olmalıdır, fakat döngü çıkış koşulu ile kombine edildiğinde bu postcondition gerçeğini zorlamak için yeterince güçlü olmalıdır I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition Copyright © 2012 Addison-Wesley. All rights reserved.

65 Evaluation of Axiomatic Semantics
Bir dildeki tüm statemant lar için Axiom yada anlam çıkarma kuralı geliştirmek zordur.Developing axioms or inference rules for all of the statements in a language is difficult Bu doğruluk kanıtlaması için iyi bir araçtır, programları anlamlandırmak için iyi bir altyapıdır. Fakat program yazıcılar veya derleyici yazılar için kullanışlı değildir. It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers Copyright © 2012 Addison-Wesley. All rights reserved.

66 Denotation Semantics vs Operational Semantics
İşlevsel Semantik içinde kodlanmış algoritalar ile tanımlanmıştır. In operational semantics, the state changes are defined by coded algorithms Denotational semantik içinde ifadeler matematiksel fonksiyonlarla tanımlanmıştır.In denotational semantics, the state changes are defined by rigorous mathematical functions Copyright © 2012 Addison-Wesley. All rights reserved.

67 Summary BNF and context-free grammars are equivalent meta-languages
Well-suited for describing the syntax of programming languages An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language Three primary methods of semantics description Operation, axiomatic, denotational Copyright © 2012 Addison-Wesley. All rights reserved.


"Describing Syntax and Semantics" indir ppt

Benzer bir sunumlar


Google Reklamları