1 / 107

Describing Syntax and Semantics

Describing Syntax and Semantics. 注意. 本次课程讨论的内容在教材第 3 ~ 4 章。 这两章内容大部分将在编译课程中讲授,本课程将不再重复。但并不意味着不重要或不需要掌握。. 提要. 刻画一个程序设计语言 语法,语义,语用 描述语法: BNF 与 EBNF 描述“静态语义” Attribute Grammar 描述语义: 操作语义 , 公理语义 , 指称语义 , 代数语义 词法与语法分析. 程序设计语言. 用以书写计算机程序 人工语言 涉及 PL 设计者 , 实现者 , 使用者(开发,维护)

lolita
Download Presentation

Describing Syntax and Semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Describing Syntax and Semantics

  2. 注意 • 本次课程讨论的内容在教材第3~4章。 • 这两章内容大部分将在编译课程中讲授,本课程将不再重复。但并不意味着不重要或不需要掌握。

  3. 提要 • 刻画一个程序设计语言 • 语法,语义,语用 • 描述语法: • BNF 与 EBNF • 描述“静态语义” • Attribute Grammar • 描述语义: • 操作语义, 公理语义, 指称语义, 代数语义 • 词法与语法分析

  4. 程序设计语言 • 用以书写计算机程序 • 人工语言 • 涉及PL 设计者, 实现者, 使用者(开发,维护) • 如何刻画?三个层面: • 语法,Syntax,程序的结构或形式,构成语言的各个记号之间的组合规则。 • 语义,Semantics, 程序的固有含义,各记号的特定含义及其所形成之组合的含义。 • 语用,Pragmatics,程序与使用情景有关的含义。

  5. 语用 • 语言成分相对言语情境的含义 • 相对性 • 动态性 • 多元性 • What did you mean by X? • 计算机程序设计语言的语用是一个有待深入研究的问题。 • Speech-Act 理论在ACL中的应用

  6. 语法 • 何谓语言? • A sentenceis a string of characters over some alphabet • A language is a set of sentences • Alexemeis the lowest level syntactic unit of a language (e.g., *, sum, begin) • A tokenis a category of lexemes (e.g., identifier) • 所谓某语言的语法就是决定字母表上的string是否为该语言的句子的规则集合 • 这些规则被形式地描述时,常称为文法 Grammar

  7. 形式地定义的语法 • 识别器(Recognizers) • A recognition device reads input strings over the alphabet of the language and decides whether the input strings belong to the language • Example: syntax analysis part of a compiler • 产生器(Generators) • A device that generates sentences of a language • One can determine if the syntax of a particular sentence is syntactically correct by comparing it to the structure of the generator

  8. BNF 与 上下文无关文法 • Context-Free Grammars • By Noam Chomsky, mid-1950s • Language generators, meant to describe the syntax of natural languages • Define a class of languages called context-free languages Avram Noam Chomsky

  9. BNF 与 上下文无关文法 • Backus-Naur Form (1959) • By John Backus to describe Algol 58 • BNF is equivalent to context-free grammars John Warner Backus 1977 Turing Award Peter Naur 2005 Turing Award

  10. BNF • 用非终结符nonterminal symbols 与终结符 terminals来抽象句子的结构;终结符为 lexemes 或者 tokens • 规则包括左部 (LHS), 和右部 (RHS), 左部为一个非终结符,右部为终结符和非终结符构成的string • 非终结符常用尖括号括起来,如 <ident_list> → identifier | identifier, <ident_list> <if_stmt> → if <logic_expr> then <stmt> • 所谓文法即这些规则的非空集合; • 开始符号start symbol乃是该文法的一个特殊非终结符

  11. BNF Rules • An abstraction (or nonterminal symbol) can have more than one RHS <stmt>  <single_stmt> | begin <stmt_list> end

  12. 例:Lists <ident_list>  ident | ident, <ident_list>

  13. 例:一个简单语言的文法 <program>  <stmts> <stmts>  <stmt> | <stmt> ; <stmts> <stmt>  <var> = <expr> <var>  a | b | c | d <expr>  <term> + <term> | <term> - <term> <term>  <var> | const

  14. 句子的推导 <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const

  15. 推导 • 句型:Every string of symbols in a derivation is a sentential form • 语句:A sentence is a sentential form that has only terminal symbols • A leftmost derivationis one in which the leftmost nonterminal in each sentential form is the one that is expanded • A derivation may be neither leftmost nor rightmost

  16. 识别树(Parse Tree) 推导的层次式表示 <program> <stmts> <stmt> <var> = <expr> a <term> + <term> <var> const b

  17. 文法的二义性问题(Ambiguity) • 何谓二义性? • A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees • 自然语言的例子: • “南京市 长江大桥” “南京市长 江大桥”

  18. 更困扰的问题:《论语·泰伯篇》 “子曰:民可使由之不可使知之” • 民可使由之,不可使知之; • 民可,使由之;不可,使知之; • 民可使,由之;不可使,知之; • 民可使,由之不可,知之; • 民可使由之?不。可使知之; • 民可使由之?不可。使知之。 ???

  19. 例:一个二义性的表达式文法 <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

  20. 例:改为无二义性的表达式文法 • 技巧在于“分层” <expr>  <expr> - <term> | <term> <term>  <term> / const| const <expr> <expr> - <term> <term> <term> / const const const

  21. 操作符的结合性 同一层次内 <expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous) <expr> <expr> <expr> + const <expr> + const const

  22. Extended BNF • 可选部分置于 [ ]内; <proc_call> -> ident [(<expr_list>)] • RHSs 中有多种选择的部分置于( )内,各种选择用| 分隔; <term> -> <term>(+|-) const • 可重复0次或多次的部分置于{ }内 <ident> -> letter {letter|digit}

  23. EBNF的表达常常大为简洁 • BNF <expr>  <expr> + <term> | <expr> - <term> | <term> <term>  <term> * <factor> | <term> / <factor> | <factor> • EBNF <expr>  <term> {(+ | -) <term>} <term>  <factor> {(* | /) <factor>}

  24. 静态语义(Static Semantics) • 其实不算通常所说的语义 • CFG所不能或不便表达的“语法”规则部分 • 例如: - Context-free, but cumbersome (e.g., types of operands in expressions) - Non-context-free (e.g., variables must be declared before they are used)

  25. 属性文法(Attribute Grammars) • D. E. Knuth 1968年提出 • 可为终结符和非终结符配备一些属性。 • 属性可携带一些语义信息,在语法分析的时候对属性进行赋值。从而使得语义处理和语法分析同时进行。 Donald Knuth 1974 ACM Turing Award

  26. 属性文法定义 • Def: An attribute grammar is a context-free grammar G = (S, N, T, P) with the following additions: • For each grammar symbol x there is a set A(x) of attribute values • Each rule has a set of functions that define certain attributes of the nonterminals in the rule • Each rule has a (possibly empty) set of predicates to check for attribute consistency

  27. 属性文法定义 • Let X0 X1 ... Xn be a rule • 综合属性: Functions of the form S(X0) = f(A(X1), ... ,A(Xn)) define synthesized attributes • 继承属性:Functions of the form I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, define inherited attributes • Initially, there are intrinsic attributeson the leaves

  28. 例: • Syntax <assign> -> <var> = <expr> <expr> -> <var> + <var> | <var> <var> A | B | C • actual_type: synthesized for <var> and <expr> • expected_type: inherited for <expr>

  29. 例: • 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)

  30. 如何计算属性值? • 自顶向下计算继承属性; • 自底向上计算综合属性; • 二者常常结合使用。

  31. 语义 • (所谓动态语义) • 定义语言的语义有多种方法。 • 给定一个合乎语法的程序,如何确定其意义? • 程序是用语言的各种基本成分,通过语言给出的组合手段,逐渐构建而成的。 • 因此,不管什么方法,要定义语义,都必须从定义基本成分的含义、组合手段的含义开始。

  32. 操作语义(operational semantics) • 用解释执行程序的抽象或具体的机器来定义语言的语义。 • 基本思想来自语言的实现。 • 为达到各方对操作语义的共同理解,我们必须首先对这里使用的“机器”有一个共同理解 • 具体的机器过于复杂而不易理解 • 一般用一种抽象的机器

  33. 操作语义 • 这类机器一般反映了von Neumann体系结构 • 抽象的看,机器有一组变量,可在其状态空间取值; • 内存;寄存器;或者栈 • 有改变变量值(改变状态)的指令; • 赋值 • 有控制执行流程的指令 • 顺序,(条件)跳转等。

  34. 操作语义 • 结构化操作语义 • 在数学结构上(而不是虚拟机上)用数学的归纳关系建立语义的解释系统。 • 有指称语义的结构式特征,更多略去了机器操作的细节 • Gordon D. Plotkin • http://homepages.inf.ed.ac.uk/gdp/publications/sos_jlap.pdf

  35. 操作语义 • 使用场合: - Language manuals and textbooks - Teaching programming languages • 非形式地使用 • 形式地使用时很复杂 (e.g., VDL)

  36. 公理语义 Axiomatic semantics • 公理语义源于程序正确性证明的研究 • 基于谓词逻辑 • 我们首先来看看程序正确性问题和Hoare逻辑 • 然后再用最弱前置条件来定义语义 C. A. R. Hoare 1980 ACM Turing Award E. W. Dijkstra 1972 ACM Turing Award

  37. 软件的正确性与Hoare Triple • Correctness is a relative notion: consistency of implementation vis-à-vis specification. (This assumes there is a specification!) • Basic notation: (P, Q: assertions, i.e. properties of the state of the computation. A: instructions). {P} A {Q} • “Hoare triple”, precondition, postcondition • What this means (total correctness): • Any execution of A started in a state satisfying P will terminate in a state satisfying Q.

  38. 例: {n > 5}n := n + 9 {n > 13} • Most interesting properties: • Strongest postcondition (from given precondition). • Weakest precondition (from given postcondition). • “P is stronger than or equal to Q” means: P implies Q • QUIZ: What is the strongest possible assertion? The weakest?

  39. 前后置条件用作规约 {x >= 0} ... Square root algorithm to compute y ... {abs (y ^ 2 – x) <= 2 * epsilon * y} -- i.e.: y approximates exact square root of x -- within epsilon

  40. 软件的正确性与Hoare Triple • Consider {P} A {Q} • Take this as a job ad in the classifieds. • Should a lazy employment candidate hope for a weak or strong P?What about Q? • Two special offers: • 1. {False}A{...} • 2. {...}A{True}

  41. 公理语义 • 例: • a = b + 1 {a > 1} • One possible precondition: {b > 10} • Weakest precondition: {b > 0}

  42. 程序正确性证明过程 • The postcondition for the entire program is the desired result • 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.

  43. 公理语义 • 赋值语句的公理:(x = E): {Qx->E} x = E {Q} • 推理规则: • The Rule of Consequence: Antecedent consequent

  44. 小练习

  45. 公理语义 • 如何证明 {x>5} x=x-3 {x>0}? {x>3} x=x-3 {x>0},{x>5}=>{x>3},{x>0}=>{x>0} {x>5} x=x-3 {x>0}

More Related