3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++11 or later で文字列のパースをするライブラリのメモ

Last updated at Posted at 2019-12-10

背景

  • C++ でちょっとした文字列をパースしたい(簡易な自前スクリプト言語, 設定ファイル, etc)
  • scanf よりはもう少し高性能なのほしい
  • bison/yacc 系は古いし, インストールが必要でめんどい(C++ だけで完結したい)
  • C++11 regex もあるけど, ちょっと使いづらい...

ライブラリ

cpp-peglib

PEGとC++11で作る言語処理系
https://qiita.com/yhirose/items/6c2660883535c91467b5

any を使っているので(C++11, 14 の場合は自前 any を使う), スクリプト言語っぽくかけますが, しかし C++11 で静的な型に戻すときはちょっと面倒です.

まずは pegdebug を使って, 構文のチェックをしてから(HTML でレポートしてくれるので便利ですが, あまり長い入力だと, 生成される html が増えてブラウザが固まりますので注意ください), その後 cpp-peglib のパーザーコンビネーター形式で C++ に直に書き直すのがいいかもしれません.

author の方から, PEG Playground をお教えいただきました. ありがとうございます :pray:

また, C++ 側のプログラムですが, PEG の grammar の書き方の問題なのか, AST 有効にするとうまくいかないときがあります.

PEGTL

こちらも PEG ライブラリ. template を使っていますが, それほど複雑ではないためか, コンパイル早めなのがよい.

C++11 PEG パーザーライブラリ PEGTL のメモ
https://qiita.com/syoyo/items/cef28f79b180e6781840

scnlib

scanf のナウでヤングなバージョンな感じで使えます.
入力は string_view/span である必要がありますが, C++11, 14 の場合は自前のクラスで対応します.

cmake で組み込みも簡単です.

author の方から, 配列のパースの仕方を教えていただきました. ありがとうございます :pray:

番外編

C ですが, mpc(micro parser combinator library)が便利そうです.

が, よくクラッシュするのが難点ですね...

RE-flex. bison/flex のナウでヤングな代替として使えそう(BSD-3 license)
https://github.com/Genivia/RE-flex

3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?