diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-02 22:03:12 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-08-02 22:06:00 +0900 |
commit | d8a13197900a62b0ff35e57779bf8f54756b559e (patch) | |
tree | 6ef32535403685e9aff942a2062510fe53ec102f /README.md | |
parent | Print expected terminals on a parse error (diff) | |
download | cotia-d8a13197900a62b0ff35e57779bf8f54756b559e.tar.gz cotia-d8a13197900a62b0ff35e57779bf8f54756b559e.tar.xz |
Update README
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 75 |
1 files changed, 73 insertions, 2 deletions
@@ -1,9 +1,80 @@ # vartan -vartan provides a compiler that generates a parsing table and a driver for golang. +vartan provides a compiler that generates a SLR parsing table and a driver for golang. [](https://github.com/nihei9/vartan/actions/workflows/test.yml) ## Status -Now Developing +🚧 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 +Accepted +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. |