1. Trang chủ
  2. » Giáo án - Bài giảng

Describing syntax and semantics

46 267 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 46
Dung lượng 231,74 KB

Nội dung

Chapter 3 Describing Syntax and Semantics ISBN 0-321-33025-0 Chapter 3 Topics • Introduction Introduction • The General Problem of Describing Syntax lhdf b •Forma l Met h o d s o f Descri b ing Syntax • Attributes Grammars • Describing the Meanings of Programs: Dynamic Semantics Dynamic Semantics Copyright © 2006 Addison-Wesley. All rights reserved. 1-2 Introduction • A language may be hard to learn, hard to implement and any ambiguity in the implement , and any ambiguity in the specification may lead to dialect differences if we do not have a clear language definition we do not have a clear language definition • Most new programming languages are subjected to a period of scrutiny by potential users before to a period of scrutiny by potential users before their designs are completed • Who must use lan g ua g e definitions gg – Other language designers – Implementors Copyright © 2006 Addison-Wesley. All rights reserved. 1-3 – Programmers (the users of the language) Introduction (cont.) • The study of programming languages can be divided into examinations of syntax and semantics – Syntax - the form or structure of the expressions, statements, and program units Sti th i f th i – S eman ti c s - th e mean i ng o f th e express i ons, statements, and program units • Semantics should follow from syntax the form • Semantics should follow from syntax , the form of statements should be clear and imply what the statements do or how they should be used Copyright © 2006 Addison-Wesley. All rights reserved. 1-4 the statements do or how they should be used Example • Syntax Example : Simple C if statement if (<expr>) <true-statement> else <false statement> <false - statement> • Semantics Example : If the expression evaluated to true execute the true statement otherwise execute the false Copyright © 2006 Addison-Wesley. All rights reserved. 1-5 statement The General Problem of Describing Syntax Syntax •A sentence is a string of characters over some alphabet • A language is a set of sentences A language is a set of sentences •A lexeme is the lowest level syntactic unit of a language (e g * + sum begin) language (e . g ., * , + , =, sum , begin) •A token is a category of lexemes (e.g., identifier) • Languages can be formally defined in two Copyright © 2006 Addison-Wesley. All rights reserved. 1-6 distinct ways: by recognition and by generation Example index = 2 * count + 10; Lexeme Token index identifier = equal_sign 2 int_literal * mult_op count identifier l + p l us_op 10 int_literal semicolon Copyright © 2006 Addison-Wesley. All rights reserved. 1-7 ; semicolon Language Recognizers • A reco g nition device reads in p ut strin g s of gpg the language and decides whether the input strings belong to the language strings belong to the language • Example: syntax analysis part of a compiler Copyright © 2006 Addison-Wesley. All rights reserved. 1-8 Language Generators • A device that generates sentences of a A device that generates sentences of a language One can determine if the syntax of a • One can determine if the syntax of a particular sentence is correct by comparing ih fh i t to t h e structure o f t h e generator Copyright © 2006 Addison-Wesley. All rights reserved. 1-9 Language Recognizers vs. Generators • The language recognizer can only be used The language recognizer can only be used in trial-and-error mode (black box) The structure of the language generator is • The structure of the language generator is an open-book which people can easily read dd d an d un d erstan d Copyright © 2006 Addison-Wesley. All rights reserved. 1-10 Formal Methods of Describing Syntax • This section discusses the formal language This section discusses the formal language generation mechanisms that are commonly used to describe the syntax of programming used to describe the syntax of programming languages Th h i f ll d • Th ese mec h an i sms are o f ten ca ll e d grammars • We will discuss the class of languages called context-free languages Copyright © 2006 Addison-Wesley. All rights reserved. 1-11 Context-Free Grammars • Noam Chomsky – A linguist – Described four classes of grammars in the mid-1950s Tfh l f • T wo o f t h ese grammar c l ass, context- f ree and regular grammars, are useful in i computer sc i ence – The tokens of programming languages can be described by regular grammars described by regular grammars – Whole programming languages, with minor exce p tions , can be described b y context-free Copyright © 2006 Addison-Wesley. All rights reserved. 1-12 p, y grammars Backus-Naur Form • Invented by John Backus to describe Algol 58 • BNF is a metalanguage –A metalanguage is a language used to describe another language • BNF is equivalent to context-free grammars • Extended BNF (EBNF) improves readability and writability of BNF •In BNF, abstractions are used to represent syntactic structures (also called nonterminal Copyright © 2006 Addison-Wesley. All rights reserved. 1-13 symbols) Backus-Naur Form (cont.) • Although BNF is simple, it is sufficiently powerful to describe the great majority of the syntax of programming languages: – Lists of similar constructs – The order in which different constructs must appear – Nested structures to any depth – Operator precedence –O p erator associativit y Copyright © 2006 Addison-Wesley. All rights reserved. 1-14 py –… Grammar and Rules • A grammar is a finite nonempty set of rules • A rule has a left-hand side (LHS) and a right- hand side (RHS), and consists of terminal (lexeme or token) and nonterminal symbols <assign>  <var> = <expression> • An abstraction (or nonterminal symbol) can have more than one RHS <if_stmt>  if <logic_expr> then <stmt> | Copyright © 2006 Addison-Wesley. All rights reserved. 1-15 if <logic_expr> then <stmt> else <stmt> Describing Lists • Syntactic lists (for example, a list of identifiers appearing on a data declaration statement) are described usin g recursion g • A rule is recursive if its LHS appears in its RHS RHS <ident_list>  identifier | identifier , <ident_list> Note: Comma ‘,’ is a terminal Copyright © 2006 Addison-Wesley. All rights reserved. 1-16 Example: A grammar of expressions • Seven terminal symbols: + -* ()xy + ( ) x y • Four non-terminal symbols: ‹expr› ‹term› ‹factor› ‹var› • Start/goal symbol: ‹expr› Rl f R u l es o f grammar ‹expr›  ‹term› | ‹expr› + ‹term› | ‹expr› - ‹term› ‹term›  ‹factor› | ‹term› * ‹factor› ‹factor›  ‹var› | ( ‹expr› ) Copyright © 2006 Addison-Wesley. All rights reserved. 1-17 ‹var›  x | y Derivations •A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) • Derivations can be used to generate all the Derivations can be used to generate all the possible sentences in a grammar • Every string of symbols in the derivation is a • Every string of symbols in the derivation is a sentential form Copyright © 2006 Addison-Wesley. All rights reserved. 1-18 Derivations (cont.) •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 is the one that is expanded • A derivation may be neither leftmost nor rightmost rightmost • Derivation order should have no effect on the ldb Copyright © 2006 Addison-Wesley. All rights reserved. 1-19 l anguage generate d b y a grammar Example: a grammar and left derivation <sentence>  <noun-phrase> <verb-phrase> . <noun-phrase>  <article> <noun> <article>  a | the <noun>  girl | dog <verb-phrase>  <verb> <noun-phrase> <verb>  sees | pets <sentence>  <noun-phrase> <verb-phrase> .  <article> <noun> <verb - phrase>  <article> <noun> <verb phrase> .  the <noun> <verb-phrase> .  the girl <verb-phrase> .  the girl <verb> <noun phrase>  the girl <verb> <noun - phrase> .  the girl sees <noun-phrase> .  the girl sees <article> <noun> .  the girl sees a <noun> Copyright © 2006 Addison-Wesley. All rights reserved. 1-20  the girl sees a <noun> .  the girl sees a dog . [...]... reserved 1-61 Dynamic Semantics (cont.) • There is no single widely acceptable notation or formalism for describing semantics • Three methods for formal semantics – Operational Semantic – defines a language by describing its actions in terms of operations on an actual or h t l hypothetical machine th ti l hi – Denotational Semantic – uses mathematical functions on programs to specify semantics – Axiomatic... a real and B is defined as an int … expected_type = real actual_type = real actual_type = real A actual_type = real [2] = Copyright © 2006 Addison-Wesley All rights reserved A + actual_type = int [3] B 1-60 Describing the Meanings of Programs: Dynamic Semantics • Specifying the semantics of a programming language is a much more difficult task than specifying syntax •... 1-44 Attribute Grammars • CFGs cannot describe all of the syntax of programming languages y • Additions to CFGs to carry some semantic info along through parse trees • Primary value of attribute grammars: – Static semantics specification – Compiler design (static semantics checking) Copyright © 2006 Addison-Wesley All rights reserved 1-45 Static semantics • Example 1: In Java, a floating-point value cannot... = x – 3 {x > 0} • If we let P be {x > 3} Q and Q’ be {x > 0} P’ be {x 3}, Q 0}, P > 5}, we have {x  3}x  x  3{x  0}, ( x  5)  ( x  3), ( x  0)  ( x  0) {x  5}x  x  3{x  0} Copyright © 2006 Addison-Wesley All rights reserved 1-71 The Rule of Sequence • Let S1 and S2 be adjacent program statements If S1 and S2 have the following preconditions and postconditions: {P1} S1 {P2} {P2} S2 {P3}... Statement • The only variable names are A, B, and C • The RHS can either be a variable or an expression in the form of a variable added to another variable • The variables can be one of two types: int or real • The type of the expression when the operand types are not the same is always real When they are the same, the expression type is that of the operands • The type of the LHS must match the type... only if the LHS and the value resulting from evaluating the RHS have the same type Copyright © 2006 Addison-Wesley All rights reserved 1-53 Example (cont.) • The syntax portion of attribute grammar is:  =  + |  A | B | C • The attributes for the nonterminals in the attribute grammar: – actual type: A synthesized attribute for and actual_type... rights reserved 1-42 Syntax Graphs • The information in BNF and EBNF rules can be represented in a directed graph Such graphs are called syntax graphs • A separate graph is used for each syntactic unit • Syntax graphs use different kinds of nodes to represent the terminal and nonterminal symbols of the right sides of a grammar's rules – Rectangle nodes contain the names of syntactic units (nonterminals)... rights reserved 1-41 Example: BNF and EBNF versions of an expression grammar BNF: p p  + | – |  * | / | f t EBNF:  {(+ | –) } )  {(* | /) } Copyright © 2006 Addison-Wesley All rights reserved 1-42 Syntax Graphs • The information in BNF and EBNF rules can be represented... d ith t i l is true • A false predicate function value indicates a violation of the syntax or static semantics rules o the a guage of t e language Copyright © 2006 Addison-Wesley All rights reserved 1-51 Example • Rule in English: “The name on the end of an The Ada procedure must match the procedure's name name” • Syntax rule:  procedure [1] end proc_name>[2]; • Semantic... ambiguous if and only if it generates a sentential form that has two or more distinct parse trees • Ambiguity should be avoided Copyright © 2006 Addison-Wesley All rights reserved 1-30 Two distinct parser trees for sentence A=B+C*A A = + * p A p = * + C B A B C and their meaning . 3 Describing Syntax and Semantics ISBN 0-321-33025-0 Chapter 3 Topics • Introduction Introduction • The General Problem of Describing Syntax lhdf b •Forma l Met h o d s o f Descri b ing Syntax •. express i ons, statements, and program units • Semantics should follow from syntax the form • Semantics should follow from syntax , the form of statements should be clear and imply what the. 1-14 py –… Grammar and Rules • A grammar is a finite nonempty set of rules • A rule has a left-hand side (LHS) and a right- hand side (RHS), and consists of terminal (lexeme or token) and nonterminal

Ngày đăng: 26/01/2015, 09:58

TỪ KHÓA LIÊN QUAN

w