[[JAVAの記事一覧]]

[[構文解析の記事一覧]]


&topicpath;

*趣旨 [#x0113801]
jparsecの日本語訳のページがなかったので
適当に気が向いたときに翻訳してみるページです。

英語はそんなに得意じゃないんだけどね。

ソースコード読めばなんとなくわかるから、そっちの方がいいかもね。

*目次 [#bca26b28]
#contents

*オリジナルページURL [#z8ecfe5c]
http://jparsec.codehaus.org/jparsec+Overview

*jparsec の概要 [#w06934a5]
jparsecで記述された典型的な構文解析プログラムで、プログラマーは構文解析木やそれらを寄せ集めた束を作ることができます。これらのパーサはそれぞれ部分部分の構文の解析をします。

*始め方 [#h1540338]
jparsecは、パーサ生成コードを元にパーサを構築します。パーサを作ればあとは次のようにパースすることができます。

 parser.parse("code to parse");
あなたのお好みに合わせて、戻り値は計算結果や構文解析木にすることができます。

では、どのようにパーサを作るのでしょうか?下記に最も重要なクラスを上げます

**Parserクラス [#m29d1670]
A parser encapsulates a piece of parsing logic and simple Parser objects can be combined to create more complex parser.

**Parsersクラス [#o43d4ecd]
パーサが共通に実装してます。

**Scannersクラス [#u3d6113c]
スキャナーが解析対象のソースコードの文字列をスキャンしてパターンによって認識に認識します。

**Terminalsクラス [#l6183fab]
提供するトークナイザは、識別子、整数、科学的な数などの一般的な末端の字句解析器です。

**OperatorTableクラス [#n44bf222]
演算子の構文における優先順位を担当します。 プログラマは演算子を演算子一覧表に使用する演算子を宣言します。そうすることでフレームワークは本格的なパーサの構築する手助けをします。

*パーサ定義をこのフークワーク用に記述し直す例のトップ5[#hff6279d]

**orの記述 A ::= B|C|D [#oa4441a3]
これは論理的などちらかを選択する概念です。次のルール 
 A ::= B|C|D
は次のように記述できます。
 Parser<Foo> a = Parsers.or(b, c, d);

**シーケンスの記述  A ::= BCD[#cd7b12ba]
これは、シーケンスのコンビネータです。 
生成ルール
 "A ::= BCD" 
は次のように記述できます。
 Parser<Foo> a = Parsers.sequence(b,c,d);

**map/sequence [#f99b7d60]
パーサを作る時, we typically want to not only recognize a certain grammar, but also to build some object or perform some computation based on the recognized grammar. This family of map/sequence combinators can be used to perform such computation. For example, in order to use the  parser result of B, C, D to create an object of A, one can implement the callback interface Map3, which accepts the parser result of B, C and D as input parameter and returns the A object as result.

Implementing anonymous class for the Map interfaces could be verbose though. A convenience Mapper class is provided to simplify the syntax. It requires additional dependency on cglib.

**many/many1 [#c02acfad]
These combinators implement the "kleene star" and "kleene cross" logic in BNF.

 "A ::= B*"
は次のように記述できます。
 
 Parser<Foo> foo = ...;
 Parser<Void> a = foo.skipMany();
または

 Parser<Foo> foo = ...;
 Parser<List<Foo>> a = foo.many();
where the latter will additionally return a list of Foo object as the parser result.

**lazy 後での評価 [#e177fd23]
生成規則は再帰処理を記述可能です。 (例えば, an expression with binary operators is represented recursively in production rule). 後で評価するコンビネータはパーサがパーサが設定されるあとで参照されます。

*字句解析対構文解析 [#h7557774]

簡単な例では, スキャン段階ではすべて動作します。 例えば:

 Parser<List<String>> numbers = Scanners.INTEGER.sepBy(Scanners.isChar(','));
 assertEquals(Arrays.asList("1", "2", "3"), numbers.parse("1,2,3"));
しかしながら, 構文の規則が複雑になる時 、それと、スペース文字やコメントを無視するようになる時、解析がぎこちなくなります。次の段階でトークンの解析をします。 

Terminalsクラスは共通のトークン化処理を提供しており、ソース文字列をスキャンしてトークン化します. It also provides corresponding syntactic parsers that recognize these tokens in the syntactical analysis phase.

A syntactical parser takes a list of tokens as input, this list needs to come from the output of a lexer. The Parser.from() API can be used to chain a syntactical parser with a lexer.

*2パスパーサの典型的な構築手順 [#ob164d22]

**手順 1: Terminals 終端 [#t492dde1]

Use the pre-defined tokenizers and terminal syntactical parsers in Terminals to define the atoms of your language.

For example, the following parser parses a list of integers separated by a comma, with hitespaces and block comments ignored.

 Terminals operators = Terminals.operators(","); // only one operator supported so far 
 Parser<?> integerTokenizer = Terminals.IntegerLiteral.TOKENIZER;
 Parser<String> integerSyntacticParser = Terminals.IntegerLiteral.PARSER;
 Parser<?> ignored = Parsers.or(Scanners.JAVA_BLOCK_COMMENT, Scanners.WHITESPACES);
 Parser<?> tokenizer = Parsers.or(operators.tokenizer(), integerTokenizer); // tokenizes the operators and integer
 Parser<List<String>> integers = integerSyntacticParser.sepBy(operators.token(","))
    .from(tokenizer, ignored.skipMany());
 assertEquals(Arrays.asList("1", "2", "3"), integers.parse("1, /*this is comment*/2, 3");

**手順2: 生成ルール [#w9c81006]

次の手順では以下にしめす生成ルールで文法解析機を作ります。The "integers" parser used above is a simple example. Real parsers can be arbitrarily complex. For operator precedence grammar, OperatorTable can be used to declare operator precedences and associativities and construct parser based on the declaration.

As in most recursive descent parsers, left-recursion needs to be avoided. Beware not to write a parser like this:

 Parser.Reference<Expr> ref = Parser.newReference();
 Parser<Expr> expr = Parsers.sequence(ref.lazy(), operators.token("+"), number); // left recursion!
ref.set(expr);
It will fail with stack overflow!

A less obvious left-recursion is a production rule that looks like:

 Parser.Reference<Expr> ref = Parser.newReference();
 Parser<Expr> expr = Parsers.sequence(operators.token("-").many(), ref.lazy());
 ref.set(expr);
As many can occur 0 times, we have a potential left recursion here.

Although left recursive grammar isn't generally supported, the most common case of left recursion stems from left associative binary operator, which is handled by OperatorTable.

*便利技 [#f3702c8c]

Please see jparsec Tips for tips and catches.

http://jparsec.codehaus.org/jparsec+Tips

*Haskell版persecのwikiはこちら [#w16a8de9]
http://www.haskell.org/haskellwiki/Parsec

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS