File tree Expand file tree Collapse file tree 1 file changed +79
-12
lines changed
src/main/kotlin/org/srcgll/grammar/combinator Expand file tree Collapse file tree 1 file changed +79
-12
lines changed Original file line number Diff line number Diff line change 1- ## Declaration:
2- for nonterminals
1+ # Grammar combinator
2+ Kotlin DSL for describing context-free grammars.
3+
4+
5+
6+ ## Declaration
7+
8+ Example for A* grammar
9+
10+ * EBNF*
11+ ```
12+ A = "a"
13+ S = A*
14+ ```
15+ * DSL*
16+ ``` kotlin
17+ class AStar : Grammar () {
18+ var A = Term (" a" )
19+ var S by NT ()
20+
21+ init {
22+ setStart(S )
23+ S = Many (A )
24+ }
25+ }
26+ ```
27+ ### Non-terminals
328
429` val S by NT() `
530
6- for terminals
31+ Non-terminals must be fields of the grammar class. Be sure to declare using delegation ` by NT() ` !!!
32+
33+ Start non-terminal set with method ` setStart(nt) ` . Can be set once for grammar.
34+
35+ ### Terminals
736
837` val A = Term("a") `
9- ## Operation:
38+
39+ ` val B = Term(42) `
40+
41+ Terminal is a generic class. Can store terminals of any type. Terminals are compared based on their content.
42+
43+ They can be declared as fields of a grammar class or directly in productions.
44+
45+ ## Operations
46+ Example for Dyck language
47+
48+ * EBNF*
49+ ```
50+ S = S1 | S2 | S3 | ϵ
51+ S1 = '(' S ')' S
52+ S2 = '[' S ']' S
53+ S3 = '{' S '}' S
54+ ```
55+ * DSL*
56+ ``` kotlin
57+ class DyckGrammar : Grammar () {
58+ var S by NT ()
59+ var S1 by NT ()
60+ var S2 by NT ()
61+ var S3 by NT ()
62+
63+ init {
64+ setStart(S )
65+ S = S1 or S2 or S3 or Epsilon
66+ S1 = Term (" (" ) * S * Term (" )" ) * S
67+ S2 = Term (" [" ) * S * Term (" ]" ) * S
68+ S3 = Term (" {" ) * S * Term (" }" ) * S
69+ }
70+ }
71+ ```
72+ ### Production
73+ A → B = A = B
1074
1175### Concatenation
1276(.): Σ∗ × Σ∗ → Σ∗
1377
1478a . b = a * b
1579
80+ ### Alternative
81+ a | b = a or b
82+
1683### Kleene Star
1784$a* = U_ {i=0}^{\inf}a^i$
1885
1986a* = Many(a)
2087
21- ` todo a += Some(a) `
22-
23- ### Alternative
24- a | b = a or b
25-
26- ### Production
27- A → B = A = B
88+ ` todo: a+ = some(a) `
2889
2990### Optional
3091a? -> a | Epsilon
3192
32- ` todo a? = opt(a) `
93+ Epsilon -- constant terminal with behavior corresponding to the $\epsilon$ terminal (empty string).
94+
95+ ` todo: a? = opt(a) `
96+
97+ ## RSM
98+ DSL allows to get the RSM corresponding to the grammar using the ` getRsm ` method.
99+ The algorithm of RSM construction is based on Brzozowski derivations.
You can’t perform that action at this time.
0 commit comments