Lullar    


ビデオジャパンは、最大でランク。

構文解析 Parsing

出典: フリー百科事典『ウィキペディア(Wikipedia)』 Source: material may be challenged Wikipedia encyclopedia (Wikipedia) 』

構文解析 (こうぶんかいせき、Syntactic Analysis)とは、ある文章文法的な関係を説明すること( parse )。 Parse (cough this demarcation, Syntactic Analysis) is to describe the relationship between a grammatical sentence (parse). 計算機科学の世界では、構文解析は字句解析Lexical Analysis )とともに、おもにプログラミング言語などの形式言語の解析に使用される。 In the world of scientific calculator, analysis parsing lexical (Lexical Analysis) and will be used to analyze the language of form and language mainly programming. また、 自然言語処理に応用されることもある。 Also sometimes applied to natural language processing.

コンパイラにおいて構文解析を行う機構を構文解析器 (Parser)と呼ぶ。 Analyzer syntax parsing mechanism to syntax in the compiler (Parser) call.

構文解析は入力テキストを通常、 木構造のデータ構造に変換し、その後の処理に適した形にする。 Usually the input text parsing to convert the structure of the tree data to a form suitable for further processing. 字句解析によって入力文字列から字句を取り出し、それらを構文解析器の入力として、 構文木抽象構文木のようなデータ構造を生成する。 Remove the wording from the input string by the lexical parser as input to them, to generate a data structure such as syntax trees and abstract syntax tree.

Parsing は本来、自然言語の文法で文の図式化を行うことを指す用語であり、 ロマンス諸語ラテン語のような語形変化のある言語の文法の図式化を現在でも Parsing と呼ぶ。 Parsing is essentially a term that refers to sentence diagramming in the grammar of the language naturally, even in the current scheme of the grammar of inflected language like Latin and Romance languages called Parsing.

目次 Contents

[ 編集 ] 形式言語 Formal language

構文解析はコンパイラの一部としてよく使われている。 Syntax analysis is often used as part of the compiler. コンパイラではプログラミング言語ソースコードを構文解析する。 Compiler to parse the source code of a programming language. プログラミング言語以外のコンピュータ言語でも構文解析は必要である。 Language parsing in language other than computer programming is required. プログラミング言語は構文解析器の高速性と効率を考慮して、 文脈自由文法に基づいて言語の文法が決められることが多い。 Consider the programming language is fast and efficient parser syntax, often can be determined based on the grammar of the language context-free grammar. しかし、この場合の文脈自由文法は表現能力が制限されている。 However, in this case, context-free grammar is limited expressive power. というのも、コンピュータ上のプログラムは使用可能なメモリ量が制限されており、制限を越えるような長い構文を扱えないからである。 Because the computer program has been limited amount of available memory, which can not handle such a long syntax beyond the limit. また、例えば言語によっては何らかの名前を参照する前に宣言しておかなければならない。 Also, for example, the language that must be declared before you see any names. しかし、より強力な文法は効率的な構文解析ができない。 However, grammar can not parse more powerful and efficient. そのため、一般に構文解析器は制約を弱めて本来の言語の文法よりも広い範囲の構文を受け付け(つまり、不正な構文も受け付ける)、後で不正なものを排除する。 Therefore, the parser accepts the general syntax of a wide range of languages than the original grammar and weaken the constraints (that is, accept the incorrect syntax), to eliminate bad things later. 必要な言語構文を全て受け付ける文脈自由文法を定義することは簡単だが、正しい構文だけを受け付ける文脈自由文法を定義することは不可能なことが多い。 Accept context-free grammar to define all the necessary language syntax is easy, to define context-free grammar that accepts only the correct syntax is often impossible. 構文解析器は一から作られることは少なく、 コンパイラコンパイラで生成することが多い。 Parser is one that is made from small, often generated by Compiler-compiler.

[ 編集 ] 処理概要 Summary Processing

以下では、字句文法と構文文法という2段階の文法を持つコンピュータ言語の構文解析を例として説明する。 The following is a grammar and syntax of the lexical grammar as an example to explain two computer languages with grammatical parsing stage.

まず第一段階として字句(トークン)を生成する。 First stage of lexical (token) to generate. これを字句解析と呼ぶ。 Called lexical analysis it. 入力文字列は正規表現による文法で定義された意味のあるシンボルに分割される。 The input string is split into a meaningful symbol in the grammar defined by regular expressions. 例えば、電卓プログラムに "12*(3+4)^2" と入力されたとき、これを 12, *, (, 3, +, 4, ), ^, 2 という字句(トークン)に分割する。 For example, the calculator program "12 * (3 +4) ^ 2" when typing it 12, *, (, 3, +, 4,), ^, that the two lexical (token) to split. 各トークンは電卓プログラムの数式として意味のあるシンボルである。 Each token is a meaningful symbol of the program as a formula calculator. 字句解析を含む構文解析器は *, +, ^, (, ) といった文字が新たなトークンの先頭になるという規則を持っているため、"12*" や "(3" といった無意味な字句の切り分けは発生しない。 Including lexical analysis parser *, +, ^, (,) has a rule that to be the first in a new token and character, "12 *" and "(3" and meaningless lexical isolation does not occur.

次の段階で実際の構文解析を行う。 Actually do the parsing of the next stage. 構文解析では、トークンの並びが文法に照らして正しい表現となっているかを判定する。 Parsing is to determine whether the expression is correct grammar in light of the sequence of tokens. このため、 文脈自由文法を参照して再帰的に規則を適用していく。 Therefore, we have applied the rule to refer to the recursive context-free grammar. ただし、プログラミング言語の構文規則は文脈自由文法だけでは表現できない。 However, the only programming language syntax rules for context-free grammar can not be expressed. 例えば、データ型が正しいか、識別子の宣言が正しいかなどは文脈自由文法の範囲外の話である。 For example, the data type is correct, and correct declaration of the identifier is the story outside the scope of context-free grammar. そのような規則は形式的には属性文法で表される。 Such rules are formally represented by attribute grammars.

最後に、意味的な解析が行われ、構文が確認された表現の意味を識別して、適当な行動をとる。 Finally, semantic analysis is performed, identify the meaning of the expression syntax is confirmed, take appropriate action. 電卓プログラムの場合、適当な行動とは式の評価(計算)であり、コンパイラならコードの生成である。 If the calculator program, and appropriate actions will evaluate the expression (calculation), and if the compiler generated code. 属性文法もそのような行動決定に関与する。 Such action involved in the decision attribute grammars.

[ 編集 ] 手法 Technique

構文解析にはさまざまな手法が提案されており、それぞれの構文解析法に対して適用可能な文法の範囲が存在する。 The parsing has been proposed by various methods, there is a range of grammar parsing method can be applied to each. 構文解析法は大まかに演算子順位法トップダウン構文解析法ボトムアップ構文解析法に分類でき、歴史的には、演算子順位法、トップダウン構文解析法は構文解析理論によって後から説明が加えられ、ボトムアップ構文解析法は理論主導で作成された。 Child rank method syntax analysis operations in the rough, analysis syntax top-down method can be divided into analysis of bottom-up syntax, historically, the operator precedence method, top-down parsing method is described later by parsing theory been made, the bottom-up parsing method was created under the leadership theory.

演算子順位法とトップダウン構文解析法の手続きは人力で作成されることがコンパイラの初期の時代にはあった。 Top-down parsing process of law is law and order during the initial operator of the compiler that was being created by humans. 特に、トップダウン構文解析法である再帰下降構文解析法はそのプログラムの実際のコードが文法の記述によく一致することが知られている。 In particular, recursive descent parsing method of parsing method top-down are well known to match the actual description of the grammar of the program code. しかし、一般にボトムアップ構文解析は非常に多くの内部状態とその間の遷移規則を必要とし、その手続きを人力で作成するのは困難である。 However, bottom-up parsing rules generally require a transition between the internal state and so many of its procedures to create a human is difficult.

現在は、主にボトムアップ構文解析法であるLALR (1)を使用した構文解析器がコンパイラコンパイラによって生成されることが多い。 Now is primarily bottom-up parsing method LALR (1) is often generated by the parser using the Compiler-compiler. この手法を使用するコンパイラコンパイラにはyaccbisonなどがあり、どちらも代表的なコンパイラコンパイラである。 Compiler-compiler to use this technique to yacc and bison and there is neither a typical Compiler-compiler. これらのコンパイラコンパイラがこの手法を採用する理由としては、適用可能な文法範囲が十分に大きく、効率もそこそこ悪くないことなどが挙げられる。 Reason to adopt this approach as these Compiler-compiler, the grammar is large enough range that can be applied, there is much bad as it reasonably efficient. その他に、トップダウン構文解析法であるLL法が使用・生成される場合もままある。 In addition, the top-down parsing method that LL also be generated without using the law.

[ 編集 ] 具体例 Examples

たとえば、 インターネット上のWebページメールアドレスをあらわすURLは、次のような構文をもっている: For example, on the Internet Web represents the mail page or URL, which have the following syntax:

  • http://サーバ名/パス名 (webページをあらわす場合, "http://www.yahoo.com/index.html"など) http:// server name / path name (web page if you represent, "http://www.yahoo.com/index.html" etc.)
  • mailto:ユーザ名@ドメイン名 (電子メールアドレスをあらわす場合, mailto:god@heaven.milなど) mailto: username @ domain name (if representing an e-mail address, mailto: god@heaven.mil, etc.)

Webブラウザ"http://ja.wikipedia.org/index.html"などの文字列を入力した場合、ブラウザはそこからサーバ名"ja.wikipedia.org"とパス名"index.html"を読みとらなければならない。 Web browser "http://ja.wikipedia.org/index.html" If you enter a string, such as the server name from which the browser "ja.wikipedia.org" path name "index.html" to read must take. これは構文解析の非常に簡単な例である。 This is a very simple example of parsing.

より複雑な構文解析は、 CJavaなどのプログラミング言語において次のような数式を計算する場合に必要となる: More complex parsing, C and Java required when calculating the following formula in the programming language such as:

2 + ( a + b × c ) ÷ ( d - e ) 2 + (a + b × c) ÷ (d - e)

この場合、コンピュータはどこから計算を始めなければならないかというと、まずかっこの中を先に計算しなければならない。 In this case, the computer and say the calculation should start from where you have to first calculate Among Mazukatsu. しかし数学では同一のかっこの中でもかけ算を足し算より先に行わなければならないという規則がある( 演算子の優先順位 )。 In mathematics, but there are rules that must be made before any addition to multiply in the parentheses of the same (priority of an operator). このため、実際にコンピュータが計算する順序は次のようになるだろう: Therefore, the computer calculates the actual order will be as follows:

  • まずb × cの値を計算する。 First b × c to calculate the value.
  • そこにaの値を足し、これをかりにXと名づける。 Add a value to it, the borrower X which X and clepe.
  • それとは別個にd - eの値を計算し、これをかりにYと名づける。 It is a separate and d - e to calculate the value of the borrower Y which the Y clepe.
  • X ÷ Yを計算する。 X ÷ Y be calculated.
  • そこに2を足す。 Two and a there.

しかしこの順序は式を一見しただけではなかなかわからない。 But this order is not hard to just look at the expression. そこで構文解析器 (パーサ)は与えられた数式に以下のような「註釈」をつけ、それぞれの項の関係をより明確な形で表現する: Analyzer where syntax (parser) is given following the formula "annotated" put a clearer way to express the relationship of each section:

数値:2 + 式:( 式:( 変数:a + 式:( 変数:b × 変数:c ) ) ÷ 式:( 変数:d - 変数:e ) ) Numbers: 2 + expression: (expression: (variables: a + expression: (variables: b × variables: c)) ÷ formula: (variables: d - variable: e))

構文解析の結果は構文木としても表すことができる。 Parsing results can be represented as a syntax tree. 構文解析はコンパイラにおけるもっとも中心的な過程のひとつであり、その手法は従来からさまざまな研究がなされている。 Parsing is one of the most central process in the compiler, the approach has been made from a variety of traditional research.

一般に、 形式言語の構文解析は曖昧でない言語を対象としている。 In general, the parsing of formal languages is that unambiguous language to the target. ある言語が曖昧であるとは、ひとつの文にたいして2通り以上の構文解析が可能であることをいう。 The language is vague and there is two against one statement that can be parsed rather than the street. 初期のプログラミング言語にはよく知られた「elseぶら下がり問題(dangling-else problem)」が存在した。 Early programming language known "else hanging problems (dangling-else problem)" that exists. たとえば以下のような文脈自由文法であらわされる言語を考える: Given the language represented in context-free grammar, such as the following example:

文 ::= if 文 then 文文 ::= if 文 then 文 else 文 Statement:: = if then statement Aya Aya:: = if then statement else statement statement 

すると、以下の文には 2通りの解釈が考えられる: Then, the following two statements are considered as the interpretation of:

if A then if B then C else D if A then if B then C else D

ひとつは「 if A then - 」と「 if B then C else D 」という2つの連なった文からなるとする解釈。 One is if A then - and if B then C else D consists of two interpretations and lined a single statement. もうひとつは「 if A then - else D 」という文の中に「 if B then C 」という文が埋めこまれているとする解釈である。 Another is if A then - else D in the statement that if B then C is interpreted to have been buried in the statement.

ふつうコンピュータの動作には厳密さが要求され、このように複数の解釈があることは許されないため、一般的にはエラーを出すか、elseは一番近いthenと結び付くように定めるなどして、文法の曖昧性を解消している。 Operation of the computer is usually required to be strictly construed in that there is more in this way is not allowed, you generally get an error, else is the closest and then provided to the lead and have to eliminate the ambiguity of the grammar.

[ 編集 ] 自然言語 Natural language

機械翻訳システムや自然言語処理システムでは、人間の言語( 自然言語 )をコンピュータプログラムで構文解析する。 The systems and natural language processing, machine translation system, the language of human language (natural) computer program to parse. 自然言語の文には構文上の曖昧さがあるため、それをプログラムで構文解析することは容易ではない。 Natural language sentence because of ambiguity in the syntax, parsing program that it is not easy. 自然言語データを構文解析するには、まずその言語の文法を選択しなければならない。 To parse natural language data must be selected first grammar of the language. 文法の選択は、言語学的考慮とコンピュータでの扱いやすさを考慮して行われる。 Grammar selection is done taking into consideration the ease of handling of the computer and linguistic considerations. 例えば、いくつかの構文解析システムは語彙機能文法 (LFG)を使用するが、一般にこの種の文法の構文解析はNP完全問題であることが知られている。 For example, some systems parsing lexical functional grammar (LFG) is used to parse the grammar of this type generally are NP-complete problems known to be. 他にも主辞駆動句構造文法 (HPSG)もよく使われるが、最近ではより単純な形式文法の研究が盛んで、Penn Treebankなどが挙げられる。 Grammatical structure and other head-driven phrase (HPSG) is also commonly used in recent years active research in a simpler form of grammar, Penn Treebank, etc. are listed. シャロー(浅い)構文解析では、名詞句などの大まかな構文の境界だけを見つけ出す。 Shallow (shallow) analysis syntax, finding only a rough boundary, such as noun phrase syntax. また、言語学的議論を避けた手法としては依存文法による構文解析がある。 As a method to avoid the discussion of language parsing grammar that depends.

最近の構文解析では統計学的手法を部分的に取り入れている。 Recent parsing is partially incorporated into statistical methods. すなわち、事前に構文解析済みのトレーニング用データ群を使う手法である。 In other words, the methods used for training data sets parsed in advance. この場合、システムは特定の文脈でどのような構文が出現しやすいかという頻度情報を持つことになる( 機械学習参照)。 In this case, the system will have the frequency information of whether any syntax to occur in the context of the particular (see machine learning). この手法では確率文脈自由文法 (PCFG)を使うもの、 最大エントロピー原理を使うもの、 ニューラルネットワークを使うものがある。 This approach is context-free grammar probability (PCFG) are used, which use the principle of entropy maximum, which have used a neural network. 成果を上げているシステムでは、「字句」統計をとっていることが多い(すなわち、 品詞も含めた単語の出現順の統計をとる)。 System that is doing well, "lexical", which often take the statistics (ie, taking the statistics of speech, including the occurrence of a word). しかし、この種のシステムは過剰適合の問題があり、効果を上げるにはある種の平滑化が必要となる。 However, this type of system has a compliance problem too, needed to increase the smoothing effect of some kind.

自然言語の構文解析アルゴリズムは、プログラミング言語の人工的な文法のように「良い」特性を持つ文法に依存することはできない。 Natural language parsing algorithm, as the artificial grammar of programming languages "good" with characteristics that are not dependent on the grammar. 上述のように文法によってはコンピュータが扱いにくい場合がある。 As mentioned above by the grammar is sometimes cumbersome computer. 一般にたとえ目的とする構造が文脈自由でなかったとしても、最初に文脈自由な近似を文法に施して構文解析を行うことがある。 Even free was not a structural context that even if the general purpose and may make alms to parse context-free grammar to a first approximation. 文脈自由文法を使ったアルゴリズムはCKY法に基づく場合が多く、時間を節約するためにそれに何らかのヒューリスティックを加えることが多い( チャートパーサなど)。 Algorithm using a context-free grammar is CKY many cases based on the law, often adding a heuristic to save it for some time (eg, Chart parser). 最近では、構文解析器が複数の解析結果を返し、上位のシステムがそれらの中で最も良いと思われるものを選択する手法もある。 In recent years, multiple returns analysis results parser, some method to select the best in what appears to be higher in those systems.

[ 編集 ] 自然言語の曖昧さの例 Examples of the ambiguity of natural language

構文解析の観点からみた形式言語自然言語のちがいは、それが曖昧であるかないかということである。 Differences in language and natural language format seen from the perspective of the analysis of syntax is that whether or not it is vague.

プログラミング言語とは違って、自然言語は通常何十通りもの解釈が存在する。 Unlike programming languages, natural language interpretation and there are usually many as 10. たとえば形態素解析の項で挙げた「うらにわにはにわとりがいる」のような例の他にも、次のような文: Mentioned in section analyzes morphemes such as "alligator alley is a chicken in" other cases as well, such as the following statement:

美しい 水車小屋の 乙女 Maid of the Mill beautiful

には少なくとも2つの解釈が存在する。 There are at least two to one interpretation. 水車小屋が美しい 」場合と、「 乙女が美しい場合 」である。 "Beautiful water mill" in the case, "If a beautiful girl" is. 自然言語は本質的に曖昧であり、その解釈を決定するにはその文が書かれているまわりの文脈や、それを書いた人の心理までも考慮に入れなければならないこともある。 Natural language is inherently ambiguous, the interpretation is to determine the context and the statement is written around it, there must also be taken into account even the psychology of the person who wrote it.

ここでいう「曖昧」とは、文の意味を知るために、その文が置かれた状況、言い換えるとコンテキスト 、フレーム情報などを考慮しなければ、同定できないということを指す。 , Where "ambiguity" is to know the meaning of the sentence, the sentence situation, the context in other words, such information should be considered a frame, can not identify that point. 決して自然言語では曖昧な表現しかできないという意味ではないことに注意するように。 Natural language means not only a vague sense that is to be noted that not.

日本語の構文解析は、おもに文節間の係り受け構造を発見することである。 Japanese parsing is to be found mainly modification structure between the clause. たとえば、上の文が以下のような係り受け構造をもっている場合、「美しい」のは水車小屋ではなく乙女のほうである(その場合、水車小屋の美しい乙女という表現が可能) For example, if the modification structure with the following statement on "beautiful" the girl is not a better mill (in which case the term can be a beautiful girl mill)

Example of one modification structure

いっぽう、もし文が以下のような係り受け構造をもっていたとすれば、この場合「美しい」のは水車小屋のほうである: The other hand, if it had the modification structure with a statement such as: If this "beauty" of the mill is better:

2 cases of modification structure

[ 編集 ] フリーで入手可能なもの what is available free

[ 編集 ] 関連項目 See also


ビデオジャパンは、最大でランク。




© Lullar