You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-220Lines changed: 9 additions & 220 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,73 +76,17 @@ constexpr auto ruleset = RulesDef(d_digit, d_number);
76
76
77
77
### Parser Initialization and Usage
78
78
79
+
See [`docs/USAGE.md`](docs/USAGE.md)
80
+
79
81
Parser/lexer configuration flags are described [in `docs/CONFIGURATION.md`](docs/CONFIGURATION.md)
80
82
81
-
Once you have defined your grammar, you can create and use the parser:
83
+
## Symbols and operators
82
84
83
-
```cpp
84
-
// ...
85
+
`Term(name)` is a terminal character. Currently it fully supports only single-character terminals
85
86
86
-
// rules definition
87
-
88
-
// Define string container types for your parser
89
-
using VStr = StdStr<char>; // Variable string class inherited from std::string<TChar>
90
-
using TokenType = StdStr<char>; // Class used for storing a token type in runtime
91
-
92
-
// Configure the parser with desired options
93
-
constexprauto conf = mk_sr_parser_conf<
94
-
SRConfEnum::PrettyPrint, // Enable pretty printing for debugging
95
-
SRConfEnum::Lookahead, // Enable lookahead(1)
96
-
SRConfEnum::ReducibilityChecker>(); // Enable RC(1), which checks for reducibility for one step ahead
97
-
98
-
// Initialize the lexer
99
-
// There are two lexer types available:
100
-
101
-
// 1. Legacy Lexer - simpler but with limitations
102
-
// Use this for simple grammars where tokens don't appear in multiple rules
103
-
auto legacy_lexer = make_lexer<VStr, TokenType>(ruleset, mk_lexer_conf<LexerConfEnum::Legacy>());
104
-
105
-
// 2. Advanced Lexer - more powerful but slightly slower
106
-
// Use this for complex grammars where the same token may appear in different rules
107
-
// (e.g., in JSON grammar where ',' appears in both object members and other contexts)
108
-
109
-
// HandleDuplicates flag enables terms range support and tokens which are present in >2 rules at once. Imposes significant compile-time overhead on grammars with high number of terminals
110
-
// HandleDupInRuntime flag moves symbols intersections handling to the lexer initialization in runtime
111
-
112
-
auto advanced_lexer = make_lexer<VStr, TokenType>(ruleset, mk_lexer_conf<LexerConfEnum::AdvancedLexer, LexerConfEnum::HandleDuplicates>());
113
-
114
-
115
-
// Create the shift-reduce parser
116
-
// TreeNode<VStr> is the AST class
117
-
auto parser = make_sr_parser<VStr, TokenType, TreeNode<VStr>>(ruleset, advanced_lexer, conf);
`TermsRange(start, end)` is a range of terminals, which lexicographically iterates over the range `[start, end]`. Note that the exact order depends on the char type.
144
88
145
-
## Operators
89
+
`NTerm(name)` is a nonterminal with a unique name, which describes its type.
146
90
147
91
### Basic Operators
148
92
@@ -170,163 +114,8 @@ if (ok) {
170
114
171
115
## Grammar serialization
172
116
173
-
SuperCFG can serialize grammar rules to a custom EBNF-like notation at compile-time. The serialization is done through the `.bake()` method:
174
-
175
-
```cpp
176
-
// Define your grammar rules class
177
-
constexpr EBNFBakery rules;
178
-
179
-
// Define non-terminals and terminals
180
-
constexpr auto nozero = NTerm(cs<"digit excluding zero">());
SuperCFG can serialize grammar rules to a custom EBNF-like notation at compile-time. See [`docs/USAGE.md`](docs/USAGE.md#grammar-serialization)
330
118
331
-
JSON parser without escape characters and whitespaces can be found in `examples/json.cpp`. Example strings: `42`, `"hello"`, `[1,2,3]`, `[1,["abc",2],["d","e","f"]]`, `{"a":123,"b":456}`, `{"a":[1,2,"asdf"];"b":["q","w","e"]}`, `{"a":{"b":42,"c":"abc"};"qwerty":{1:"uiop",42:10}}`
119
+
## Examples
332
120
121
+
Some of the examples can be found [in `docs/EXAMPLES.md`](docs/EXAMPLES.md). The code is located in `examples/json.cpp`
JSON parser without escape characters and whitespaces can be found in `examples/json.cpp`. Example strings: `42`, `"hello"`, `[1,2,3]`, `[1,["abc",2],["d","e","f"]]`, `{"a":123,"b":456}`, `{"a":[1,2,"asdf"],"b":["q","w","e"]}`, `{"a":{"b":42,"c":"abc"},"qwerty":{1:"uiop",42:10}}`
0 commit comments