Parsing Techniques 解析技术

Monographs in Computer Science

Abadi and Cardelli, A Theory of Objects

Benosman and Kang [editors], Panoramic Vision: Sensors, Theory, and Applications

Bhanu, Lin, Krawiec, Evolutionary Synthesis of Pattern Recognition Systems

Broy and Stølen, Specification and Development of Interactive Systems: FOCUS on Streams, Interfaces, and Refinement

Brzozowski and Seger, Asynchronous CircuitsBurgin, Super-Recursive Algorithms

Cantone, Omodeo, and Policriti, Set Theory for Computing: From Decision Procedures to Declarative Programming with Sets

Castillo, Gutiérrez, and Hadi, Expert Systems and Probabilistic Network Models

Downey and Fellows, Parameterized Complexity

Feijen and van Gasteren, On a Method of Multiprogramming

Grune and Jacobs, Parsing Techniques: A Practical Guide, Second Edition

Herbert and Spärck Jones [editors], Computer Systems: Theory, Technology, and Applications

Leiss, Language Equations

Levin, Heydon, Mann, and Yu, Software Configuration Management Using VESTA

Mclver and Morgan [editors], Programming Methodology

Mclver and Morgan [editors], Abstraction, Refinement and Proof for Probabilistic Systems

Misra, A Discipline of Multiprogramming: Programming Theory for Distributed Applications

Nielson [editor], ML with Concurrency
Paton [editor], Active Rules in Database Systems

Poernomo, Crossley, and Wirsing, Adapting Proof-as-Programs: The Curry-Howard Protocol

Selig, Geometrical Methods in Robotics
Selig, Geometric Fundamentals of Robotics, Second Edition

Shasha and Zhu, High Performance Discovery in Time Series: Techniques and Case Studies

Tonella and Potrich, Reverse Engineering of Object Oriented Code

第一章:

1.3 内容概要

由于解析最关键的就是句子和语法,而语法本身又非常的复杂,所以第2章我们将对语法进行详细的讲解。第3章探讨了解析背后的原理,并给出了解析方法的分类。总之,解析技术可以分为自顶向下(top-down)或自底向上(bottom-up)两种,或者是定向(directional)和非定向(non-directional)两种;定向法又可以细分为确定性(deterministic)和非确定性(non-deterministic)的。这就决定了和紧接着后面几章的内容的主体。

第4章我们讲解非定向法,包括Unger和CYK。第5章介绍有限状态自动机(finite-state automata),为后面需要的章节做一个过渡。第6到10章介绍定向法,如下。第6章涵盖了非确定性的自顶向下解析器(向下递归,Definite Clause Grammars),第7章涵盖了非确定性的自底向上解析器(Earley)。第8章和第9章介绍确定性方法(第8章介绍自顶向下法:各种形式的LL。第9章介绍自底向上法:LR)。第10章涵盖非规范(non-canonical)的解析器,以一种不太规范的自顶向下或自底向上的方法来确定解析树的节点的解析器(例如left-corner)。第11章则介绍了类似上一章中的算法的非确定性版本(比如GLR解析器)。

接下来的四章内容,不太符合上述的框架。第12章介绍了最新的用于解析某一语言中完整句子的子字符串技术,包括确定性和非确定性的。第13章介绍了一种正在发展中的技术,这种技术将解析视为贯穿有限状态机的上下文无关语法。第14章介绍了几个并行解析算法,而第15章则解释了几种关于非Chomsky文法系统的建议以及他们的解析器。而这些本身就完成了解析方法。

第16章介绍了一些错误处理方法,第17章介绍了在写作和使用中比较实用的解析器。

第二章:

2.1 将一门语言看作一个无限大的集合

language是sentences的集合,sentence是tokens组成的序列。

sentences的语义由sentences的结构和token决定。

语法:

静态的规则可能描述不了某些语言,故将其作为语法可能会无能为力。

一系列确定的有限的description也不能完备描述所有语言。

证明:(对角线证明)

可以有序地枚举所有description(无穷多)。可以有序地枚举由一个字母表产生的所有word(无穷多),令该枚举列表为EnumList。则任何语言,其所用的所有word都在EnumList里面。对于某个在EnumList中的word,一个language可以有,也可以没有。因此在不考虑语言的structure,仅考虑language所用的word,对于所有语言,都可以用一个无穷长的二进制向量表示,该向量的某一位的值表示,对于该位对应EnumList中的单词,该语言是否包含。

构造这样的表,它仅有两列,第一列是所有的description,第二列是每个description对应的language(有些description对应多个language,这样,仅仅选择一个language作为其对应项)。列表形如下:

Description           Language

Description #1      000000100· · ·

Description #2      110010001· · ·

Description #3      011011010· · ·

Description #4       110011010· · ·

Description #5       100000011· · ·

Description #6        111011011· · ·

. . .                               . . .

构造这样的language,它具有这样的特性:该language对应二进制向量的第n位的值与第n个description对应的language的二进制向量的第n位的值相反。这样,该language的二进制值就是language列组成的表中的对角线值再对每位取反。

这样的话该language必不在language列组成的表中,因为它不可能是第一行,因为它第一位跟第一行不一样,更一般地,它不可能是第i行,因为它第i位与第i行不一样。所以它不可能存在于这个表中任何一行。因此,这个language不可能有与之对应的description(这个description由有限由有限条规则组成)。

这里有个问题,为什么所构造的那个language用到的描述“该language对应二进制向量的第n位的值与第n个description对应的language的二进制向量的第n位的值相反”不在Description列里面!!

我没有深究,似乎是由悖论引起的,豆瓣上的评论有提到,另外可以看看罗素悖论。

2.2 形式语法

若如下看待语言:由object组成。先有基本的object,然后有一套规则来在基本的object上构造新的object。

则语言有一个四元组组成:(非终止object集合,终止object集合,规则,开始object)

非终止object集合 和 终止object集合 都是有限的symbol集合,且两集合无交集。

规则是形如 X -> Y 的转换规则,X和Y都必须是一串symbol,X不可为空串,Y可以(书中集合右上角星号是克莱尼星号

开始object必须是一个非终止object。

利用上述方法生成一个sentence的过程可以用一个无环有向图表示。

第十六页的:Amazingly, we have succeeded in implementing the notion “must replace” in a system that only uses “may replace”; looking more closely, we see that we have split “must replace” into “may replace” and “must not be a non-terminal”

还没找到比phrase structure grammar更sufficient的语法描述方法。

2.3 The Chomsky Hierarchy of Grammars and Languages

type 0 grammar 是无任何限制的phrase structure grammars

type 1 grammar(上下文有关文法):两种定义

(1)不存在这样的规则,左边的symbol数量比右边的symbol数量多

(2)所有的规则都是上下文有关,规则上下文有关就是sentence中只有一个非终结词经过规则变换后得到替代,且替代结果至少有一个symbol。

它的某个sentence的生成图是有向无环图

type 2 grammar(上下文无关文法):规则左边只有一个symbol。

它的某个sentence的生成图是树。

二型文法中,某个symbol可以独立作为一门language,因为其它symbol对它的映射结果不影响。

type 3 grammar:左线性,右线性

type 4 grammar:规则右边只能是终止的symbol

2.4 使用上述grammar生成sentences的算法

一个广度优先搜索算法

该算法可以用来证明某个grammar可以生成至少一个sentence,但不能证明不能。

并阐述了一个检查2型文法是否生成至少一个sentence的算法

2.5 只有0型文法会生成空串

如果一个文法能生成空串,那么parsing会变得复杂

2.9 去除文法中无用的规则

type 2 grammar相较于0型和1型,容易找出文法中无用的规则。

type 2 grammar无用的规则包括如下问题:

(1)包含未定义的非终结符号 undefined non-terminals

(2)从开始symbol,不可能产生符合规则左边的符号序列 unreachable non-terminals

(3)不能产生任何东西(无限递归)non-productive

一个闭包算法首先清除non-productive 和 undefined non-terminals的规则

另一个闭包算法清除unreachable non-terminals的规则

第二个闭包算法在清规则的时候不会让一个non-terminal(设为N)编程undefined,因为N是reachable的,因此它的所有定义(左边是N的规则)都不会在这个闭包算法中被清掉。同样的道理,第二个闭包算法也不会让N编程non-productive

如果两个闭包算法倒过来用,很可能会在第二次的时候生成unreachable non-terminals

2.11 语义与文法

某个sentence的语义与这个sentence的生成结构图(production tree)有关。

(1)attribute grammar(将语义作为attribute绑定到symbol上)

(2)transduction grammar(语义是sentence作为input string得到的output string的语义)

(3)Augmented Transition Networks(将actions绑定到生成结构图的结点上,语义就是遍历结点时触发的action)

2.12 各级文法的比较

More powerful grammars can define more complicated boundaries between correct and incorrect sentences. Some boundaries are so fine that they cannot be described by any grammar (that is, by any generative process).



作者:doyoubi
链接:https://www.jianshu.com/p/e1b6683dae67
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

第三章:

3.1.1 从语句中重新建立production tree是为了在production tree的结构中获得语义

二叉树只要叶子数一样,总结点数就一样,令叶子树为n,总结点数为2 * n - 1。

规则右边有且仅有两个结点:2 * n - 1

规则右边可以多过两个结点:小于 2 * n - 1(因为不需要那么多父亲结点)

规则右边有且只有一个结点:小于2 * Cu * n(因为一系列规则可能导致一个词增加常数个父亲结点(Cu-1个))

规则右边可以没有结点:小于 2 * Cn * Cu * n

3.1.2 歧义

有歧义的句子有多个production tree,有歧义的grammar会生成有歧义的句子。

但若多个production tree的语义一样,则这种歧义是spuriously ambiguous。

3.1.3 parse tree 的线性化

leftmost derivation:对parse tree先续遍历

rightmost derivation:对parse tree后续遍历

infix notation:先前n各子结点(再加括号),再父节点,再后剩余的子结点(再加括号)。如(左子节点) 父亲 (右子节点)

当n为1时为 left-corner derivation。

以上的方法线性化后,可以逆向求回 parse tree

3.2 Two Ways to Parse a Sentence

自顶向下 和 自底向上

3.3 Non-Deterministic Automata

Non-Deterministic Automata 若能产生结果,则比正确。

若不能产生结果,则死循环或者走到死路。

正确与否决定于control mechanism

构造constrol

(1)independent of the grammar,

(2)consult the grammar regularly

(3)use large tables precomputed from the grammar

(4)use tables computed from the input string

Constructing the control mechanism, including the tables, from the grammar is

almost always done by a program. Such a program is called a parser generator;

3.4 Recognition and Parsing for Type 0 to Type 4 Grammars

只要一个sentence是由0型grammar生成的,那么我们一定可以用程序将parse tree构建出来。

不能在有限的时间内判断一个sentence是否可以由0型grammar生成,但1型grammar可以。

3.5 An Overview of Context-Free Parsing Methods

3.5.1 方法可分directional和non-directional

3.5.2 方法可分为深度优先搜索和广度优先搜索

3.5.3 General directional method

3.5.4 大部分general的parsing算法需要指数时间,最好也需要3次方的时间。但有些线性时间的算法可以针对特定的grammar

限制NDA的选择到只有一个,就是deterministic automaton,它可达成线性时间。

3.5.5

There is only one deterministic top-down method; it is called LL.The first L stands for Left-to-right, the second for “identifying the Leftmost production”, as directional top-down parsers do.

There are quite a variety of deterministic bottom-up methods, the most powerful being called LR, where again the L stands for Left-to-right, and the R stands for “identifying the Rightmost production”.

3.5.6 Non-Canonical Methods

3.5.7 Generalized Linear Methods

使用广度优先搜索来处理不能造出deterministic automaton的情况

3.6 The “Strength” of a Parsing Technique

一种parsing技术是否更强,取决于他要求原有语法需要多少更改来适应这种parsing技术。

The stronger the parser is, the fewer restrictions the grammars need to obey and the “weaker” they can afford to be.



作者:doyoubi
链接:https://www.jianshu.com/p/0234ca26097d
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

原文地址:https://www.cnblogs.com/cx2016/p/12926199.html