aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 712bb162fcec6f680895fe3a55a48089e7d9fffa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# vartan

vartan is a parser generator for golang and supports LALR(1) and SLR(1). vartan also provides a command to perform syntax analysis to allow easy debugging of your grammar.

[![Test](https://github.com/nihei9/vartan/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/nihei9/vartan/actions/workflows/test.yml)

## Installation

Compiler:

```sh
$ go install github.com/nihei9/vartan/cmd/vartan@latest
```

Code Generator:

```sh
$ go install github.com/nihei9/vartan/cmd/vartan-go@latest
```

## Usage

### 1. Define your grammar

vartan uses BNF-like DSL to define your grammar. As an example, let's write a grammar that represents a simple expression.

```
%name expr

%left mul div
%left add sub

expr
	: expr add expr
	| expr sub expr
	| expr mul expr
	| expr div expr
	| func_call
	| int
	| id
	| '(' expr ')' #ast expr
	;
func_call
	: id '(' args ')' #ast id args
	| id '(' ')'      #ast id
	;
args
	: args ',' expr #ast args... expr
	| expr
	;

ws #skip
	: "[\u{0009}\u{0020}]+";
int
	: "0|[1-9][0-9]*";
id
	: "[A-Za-z_][0-9A-Za-z_]*";
add
	: '+';
sub
	: '-';
mul
	: '*';
div
	: '/';
```

Save the above grammar to a file in UTF-8. In this explanation, the file name is `expr.vr`.

⚠️ The input file must be encoded in UTF-8.

### 2. Compile the grammar

Next, generate a parsing table using `vartan compile` command.

```sh
$ vartan compile -g expr.vr -o expr.json
16 conflicts
```

### 3. Debug

#### 3.1. Parse

If you want to make sure that the grammar behaves as expected, you can use `vartan parse` command to try parse without implementing a driver.

⚠️ An encoding that `vartan parse` command and the driver can handle is only UTF-8.

```sh
$ echo -n 'foo(10, bar(a)) + 99 * x' | vartan parse expr.json
expr
├─ expr
│  └─ func_call
│     ├─ id "foo"     └─ args
│        ├─ expr
│          └─ int "10"        └─ expr
│           └─ func_call
│              ├─ id "bar"              └─ args
│                 └─ expr
│                    └─ id "a"
├─ add "+"
└─ expr
   ├─ expr
     └─ int "99"
   ├─ mul "*"
   └─ expr
      └─ id "x"
```

When `vartan parse` command successfully parses the input data, it prints a CST or an AST (if any).

#### 3.2. Resolve conflicts

`vartan compile` command also generates a description file having `-description.json` suffix along with a parsing table. This file describes each state in the parsing table in detail. If your grammar contains conflicts, see `Conflicts` and `States` sections of this file. Using `vartan show` command, you can see the description file in a readable format.

```sh
$ vartan show expr-description.json
# Class

LALR(1)

# Conflicts

16 conflicts were detected.

# Terminals

   1  - - <eof>
   2  - - error
   3  - - x_1 (\()
   4  - - x_2 (\))
   5  - - x_3 (,)
   6  - - ws
   7  - - int
   8  - - id
   9  2 l add (+)
  10  2 l sub (-)
  11  1 l mul (*)
  12  1 l div (/)

# Productions

   1  - - expr' → expr
   2  2 l expr → expr + expr
   3  2 l expr → expr - expr
   4  1 l expr → expr * expr
   5  1 l expr → expr / expr
   6  - - expr → func_call
   7  - - expr → int
   8  - - expr → id
   9  - - expr → \( expr \)
  10  - - func_call → id \( args \)
  11  - - func_call → id \( \)
  12  - - args → args , expr
  13  - - args → expr

# States

## State 0

   1 expr'   expr

shift     3 on \(
shift     4 on int
shift     5 on id
goto      1 on expr
goto      2 on func_call


## State 1

   1 expr'  expr    2 expr  expr  + expr
   3 expr  expr  - expr
   4 expr  expr  * expr
   5 expr  expr  / expr

shift     6 on +
shift     7 on -
shift     8 on *
shift     9 on /
reduce    1 on <eof>


## State 2

   6 expr  func_call ・

reduce    6 on <eof>, \), ,, +, -, *, /

...
```

### 4. Generate a parser

Using `vartan-go` command, you can generate a source code of a parser to recognize your grammar.

```sh
$ vartan-go expr.json
```

Then you will get the following files.

* `expr_parser.go`
* `expr_lexer.go`
* `expr_semantic_action.go`

You need to implement a driver to use the parser. An example is below.

```go
package main

import (
	"fmt"
	"io"
	"os"
)

func main() {
	toks, err := NewTokenStream(os.Stdin)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	gram := NewGrammar()
	treeAct := NewSyntaxTreeActionSet(gram, true, false)
	p, err := NewParser(toks, gram, SemanticAction(treeAct))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	err = p.Parse()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	synErrs := p.SyntaxErrors()
	if len(synErrs) > 0 {
		for _, synErr := range synErrs {
			printSyntaxError(os.Stderr, synErr, gram)
		}
		os.Exit(1)
	}
	fmt.Println("accepted")
	PrintTree(os.Stdout, treeAct.AST())
}

func printSyntaxError(w io.Writer, synErr *SyntaxError, gram Grammar) {
	var msg string
	tok := synErr.Token
	switch {
	case tok.EOF():
		msg = "<eof>"
	case tok.Invalid():
		msg = fmt.Sprintf("'%v' (<invalid>)", string(tok.Lexeme()))
	default:
		if alias := gram.TerminalAlias(tok.TerminalID()); alias != "" {
			msg = fmt.Sprintf("'%v' (%v)", string(tok.Lexeme()), alias)
		} else {
			msg = fmt.Sprintf("'%v' (%v)", string(tok.Lexeme()), gram.Terminal(tok.TerminalID()))
		}
	}
	fmt.Fprintf(w, "%v:%v: %v: %v", synErr.Row+1, synErr.Col+1, synErr.Message, msg)

	if len(synErr.ExpectedTerminals) > 0 {
		fmt.Fprintf(w, "; expected: %v", synErr.ExpectedTerminals[0])
		for _, t := range synErr.ExpectedTerminals[1:] {
			fmt.Fprintf(w, ", %v", t)
		}
	}

	fmt.Fprintf(w, "\n")
}
```

Please save the above source code to `main.go` and create a directory structure like the following.

```
/project_root
├── expr_parser.go
├── expr_lexer.go
├── expr_semantic_action.go
└── main.go (the driver you implemented)
```

Now, you can perform the syntax analysis.

```sh
$ echo -n 'foo+99' | go run .
accepted
expr
├─ expr
│  └─ id "foo"
├─ add "+"
└─ expr
   └─ int "99"
```

```sh
$ echo -n 'foo+99?' | go run .
1:7: unexpected token: '?' (<invalid>); expected: <eof>, +, -, *, /
exit status 1
```