Fixed peglit problem and update README

This commit is contained in:
yhirose 2021-01-21 21:16:47 -05:00
parent 80b20a091f
commit 02f428ab67
3 changed files with 48 additions and 33 deletions

View File

@ -598,9 +598,8 @@ usage: grammar_file_path [source_file_path]
options: options:
--source: source text --source: source text
--ast: show AST tree --ast: show AST tree
--opt, --opt-all: optimaze all AST nodes except nodes selected with --opt-rules --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction
--opt-only: optimaze only AST nodes selected with --opt-rules --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction
--opt-rules rules: CSV definition rules to adjust AST optimazation
--trace: show trace messages --trace: show trace messages
``` ```
@ -662,8 +661,17 @@ Number <- < [0-9]+ >
- Multitive[Number] (3) - Multitive[Number] (3)
``` ```
### Adjust AST optimazation with `no_ast_opt` instruction
``` ```
> peglint --ast --opt --opt-rules "Primary" --source "1 + 2 * 3" a.peg > cat a.peg
Additive <- Multitive '+' Additive / Multitive
Multitive <- Primary '*' Multitive / Primary
Primary <- '(' Additive ')' / Number { no_ast_opt }
Number <- < [0-9]+ >
%whitespace <- [ \t\r\n]*
> peglint --ast --opt --source "1 + 2 * 3" a.peg
+ Additive/0 + Additive/0
+ Multitive/1[Primary] + Multitive/1[Primary]
- Number (1) - Number (1)
@ -672,10 +680,8 @@ Number <- < [0-9]+ >
- Number (2) - Number (2)
+ Multitive/1[Primary] + Multitive/1[Primary]
- Number (3) - Number (3)
```
``` > peglint --ast --opt-only --source "1 + 2 * 3" a.peg
> peglint --ast --opt-only --opt-rules "Primary" --source "1 + 2 * 3" a.peg
+ Additive/0 + Additive/0
+ Multitive/1 + Multitive/1
- Primary/1[Number] (1) - Primary/1[Number] (1)

View File

@ -8,9 +8,8 @@ usage: grammar_file_path [source_file_path]
options: options:
--ast: show AST tree --ast: show AST tree
--opt, --opt-all: optimaze all AST nodes except nodes selected with --opt-rules --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction
--opt-only: optimaze only AST nodes selected with --opt-rules --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction
--opt-rules rules: comma delimitted definition rules for optimazation
--source: source text --source: source text
--trace: show trace messages --trace: show trace messages
``` ```
@ -68,6 +67,8 @@ Number <- < [0-9]+ >
[commendline]:1:3: syntax error [commendline]:1:3: syntax error
``` ```
### AST
``` ```
> cat a.txt > cat a.txt
1 + 2 * 3 1 + 2 * 3
@ -86,6 +87,8 @@ Number <- < [0-9]+ >
- Number (3) - Number (3)
``` ```
### AST optimazation
``` ```
> peglint --ast --opt --source "1 + 2 * 3" a.peg > peglint --ast --opt --source "1 + 2 * 3" a.peg
+ Additive + Additive
@ -95,8 +98,17 @@ Number <- < [0-9]+ >
- Multitive[Number] (3) - Multitive[Number] (3)
``` ```
### Adjust AST optimazation with `no_ast_opt` instruction
``` ```
> peglint --ast --opt --opt-rules "Primary" --source "1 + 2 * 3" a.peg > cat a.peg
Additive <- Multitive '+' Additive / Multitive
Multitive <- Primary '*' Multitive / Primary
Primary <- '(' Additive ')' / Number { no_ast_opt }
Number <- < [0-9]+ >
%whitespace <- [ \t\r\n]*
> peglint --ast --opt --source "1 + 2 * 3" a.peg
+ Additive/0 + Additive/0
+ Multitive/1[Primary] + Multitive/1[Primary]
- Number (1) - Number (1)
@ -105,10 +117,8 @@ Number <- < [0-9]+ >
- Number (2) - Number (2)
+ Multitive/1[Primary] + Multitive/1[Primary]
- Number (3) - Number (3)
```
``` > peglint --ast --opt-only --source "1 + 2 * 3" a.peg
> peglint --ast --opt-only --opt-rules "Primary" --source "1 + 2 * 3" a.peg
+ Additive/0 + Additive/0
+ Multitive/1 + Multitive/1
- Primary/1[Number] (1) - Primary/1[Number] (1)

View File

@ -1,7 +1,7 @@
// //
// peglint.cc // peglint.cc
// //
// Copyright (c) 2015 Yuji Hirose. All rights reserved. // Copyright (c) 2021 Yuji Hirose. All rights reserved.
// MIT License // MIT License
// //
@ -35,7 +35,7 @@ inline vector<string> split(const string &s, char delim) {
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
auto opt_ast = false; auto opt_ast = false;
auto opt_optimize = false; auto opt_optimize = false;
vector<string> opt_rules; auto opt_mode = true;
auto opt_help = false; auto opt_help = false;
auto opt_source = false; auto opt_source = false;
vector<char> source; vector<char> source;
@ -49,8 +49,12 @@ int main(int argc, const char **argv) {
opt_help = true; opt_help = true;
} else if (string("--ast") == arg) { } else if (string("--ast") == arg) {
opt_ast = true; opt_ast = true;
} else if (string("--opt") == arg) { } else if (string("--opt") == arg || string("--opt-all") == arg) {
opt_optimize = true; opt_optimize = true;
opt_mode = true;
} else if (string("--opt-only") == arg) {
opt_optimize = true;
opt_mode = false;
} else if (string("--source") == arg) { } else if (string("--source") == arg) {
opt_source = true; opt_source = true;
if (argi < argc) { if (argi < argc) {
@ -65,21 +69,16 @@ int main(int argc, const char **argv) {
} }
if (path_list.empty() || opt_help) { if (path_list.empty() || opt_help) {
cerr << "usage: grammar_file_path [source_file_path]" << endl cerr << R"(usage: grammar_file_path [source_file_path]
<< endl
<< " options:" << endl options:
<< " --source: source text" << endl --source: source text
<< " --ast: show AST tree" << endl --ast: show AST tree
<< " --opt, --opt-all: optimaze all AST nodes except nodes " --opt, --opt-all: optimaze all AST nodes except nodes selected with `no_ast_opt` instruction
"selected with " --opt-only: optimaze only AST nodes selected with `no_ast_opt` instruction
"--opt-rules" --trace: show trace messages
<< endl )";
<< " --opt-only: optimaze only AST nodes selected with --opt-rules"
<< endl
<< " --opt-rules rules: CSV definition rules to adjust AST "
"optimazation"
<< endl
<< " --trace: show trace messages" << endl;
return 1; return 1;
} }
@ -180,7 +179,7 @@ int main(int argc, const char **argv) {
if (ast) { if (ast) {
if (opt_optimize) { if (opt_optimize) {
ast = parser.optimize_ast(ast); ast = parser.optimize_ast(ast, opt_mode);
} }
std::cout << peg::ast_to_s(ast); std::cout << peg::ast_to_s(ast);
} }