Skip to content

Commit

Permalink
Added example usage to readme. Updated gitignore.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroBone committed Mar 28, 2020
1 parent 76bfaf9 commit e63b312
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ knife.iml
demo/KnifeDemo.iml

out
demo/out
demo/out
META-INF
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,58 @@ Also, as other good parser generation tools, knife uses itself to read the input
2. Run `java -jar knife.jar grammar.kn` where `grammar.kn` is your grammar file.
3. Knife will generate the parsing classes in the same directory.

**Note**: in the grammar file the first non-terminal to be declared is the start symbol. Further ordering doesn't matter.

### Example

This example parses prefix arithmetic expression with operations `+`, `-` and `*`.

Grammar (file `prefix.kn`):

```
%type expr Integer
%type NUM Integer
expr = NUM(n); { v = n; }
expr = PLUS expr(op1) expr(op2); { v = op1 + op2; }
expr = MINUS expr(op1) expr(op2); { v = op1 - op2; }
expr = MUL expr(op1) expr(op2); { v = op1 * op2; }
```

After `java -jar knife.jar prefix.kn` 2 files will be generated - `Parser.java` and `ParseNode.java`.

Example usage:

```java
package net.zerobone.knifeexample;

import net.zerobone.knifeexample.parser.Parser;

public class Main {

public static void main(String[] args) {

Parser parser = new Parser();

// parsing expression + * 5 3 * 4 6
// in infix notation: 5 * 3 + 4 * 6 = 39
parser.parse(Parser.T_PLUS, "+");
parser.parse(Parser.T_MUL, "*");
parser.parse(Parser.T_NUM, 5);
parser.parse(Parser.T_NUM, 3);
parser.parse(Parser.T_MUL, "*");
parser.parse(Parser.T_NUM, 4);
parser.parse(Parser.T_NUM, 6);
parser.parse(Parser.T_EOF, null);

if (parser.successfullyParsed()) {
System.out.println((int)parser.getValue()); // Output: 39
}

}
}
```

## Support

Please [open an issue](https://github.com/ZeroBone/Knife/issues) if you found a bug in Knife.
Expand Down

0 comments on commit e63b312

Please sign in to comment.