aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 6c323e9db9bbfc2e668601aeda075b7e924484b6 (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
# vartan

vartan provides a compiler that generates a LALR(1) or SLR(1) parsing table and a driver for golang.

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

## Status

🚧 Now Developing

## Installation

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

## Usage

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

```
expr
    : expr add_op term
    | term
    ;
term
    : term mul_op factor
    | factor
    ;
factor
    : number
    | id
    ;

whitespaces: "[\u{0009}\u{0020}]+" #skip;
number: "[0-9]+";
id: "[A-Za-z_]+";
add_op: '+';
mul_op: '*';
```

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

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

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

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 + bar * baz * 100' | vartan parse expr.json
expr
├─ expr
│  └─ term
│     └─ factor
│        └─ id "foo"
├─ add_op "+"
└─ term
   ├─ term
     ├─ term
       └─ factor
          └─ id "bar"
     ├─ mul_op "*"
     └─ factor
        └─ id "baz"
   ├─ mul_op "*"
   └─ factor
      └─ number "100"
```

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

## Debug

`vartan compile` command also generates a description file having `.desc` suffix along with a parsing table. This file describes each state in the parsing table in detail. If your grammar contains conflicts, see `Conflicts` section of this file.