Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/izackwu/opg-analyzer
OPG (Operator Precedence Grammar) analyzer/parser. 算符优先文法分析器
https://github.com/izackwu/opg-analyzer
grammar-parser
Last synced: 5 days ago
JSON representation
OPG (Operator Precedence Grammar) analyzer/parser. 算符优先文法分析器
- Host: GitHub
- URL: https://github.com/izackwu/opg-analyzer
- Owner: izackwu
- License: mit
- Created: 2020-05-18T03:27:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-24T08:24:38.000Z (over 4 years ago)
- Last Synced: 2024-05-22T02:12:19.499Z (6 months ago)
- Topics: grammar-parser
- Language: Go
- Homepage:
- Size: 31.3 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-cs - @keithnull, 2020 Spring
README
# opg-analyzer
OPG (Operator Precedence Grammar) analyzer. You may refer to [Wikipedia](https://en.wikipedia.org/wiki/Operator-precedence_grammar) for its definition.## How to run it?
First, make sure Golang has been properly installed on your machine.
Use command `go get` to fetch this repository with ease:
```bash
go get github.com/keithnull/opg-analyzer
```Checkout to its directory:
```bash
cd $GOPATH/src/github.com/keithnull/opg-analyzer
```Build it:
```bash
go build .
```An executable `opg-analyzer` would now have been generated. Then you can run it with arguments:
```bash
Usage of opg-analyzer:
-grammar string
input: OPG file (default "example_grammar.txt")
-sentences string
input: sentences to parse in a file
-table string
output: OP table file (default "example_table.txt")
```## Examples
In file `example_grammar.txt`, there's a simple grammar (note that tokens are separated by spaces):
```
E -> E + T | T
T -> T * F | F
F -> ( E ) | i
```If you pass this grammar to the grammar, it would generate an operator precedence table like this (printed to both `stdout` and file):
```
The OP table is:
Terminals: [( ) i $ + *]
Relations:
( ) i $ + *
( < = < < <
) > > > >
i > > > >
$ < < = < <
+ < > < > > <
* < > < > > >
```With that, if you also pass sentences in a file to the program, for example, `exmaple_sentences.txt`:
```
i + i + i
i + i * i
( i + i ) * i
i * ( i + i
```The parsing result would be:
```
Start to parse [i + i + i $]
----------------------------------------------
Iteration Stack Input Action
1 [$] [i + i + i $] Shift
2 [$ i] [+ i + i $] Reduce
3 [$ X] [+ i + i $] Shift
4 [$ X +] [i + i $] Shift
5 [$ X + i] [+ i $] Reduce
6 [$ X + X] [+ i $] Reduce
7 [$ X] [+ i $] Shift
8 [$ X +] [i $] Shift
9 [$ X + i] [$] Reduce
10 [$ X + X] [$] Reduce
11 [$ X] [$] Accept
----------------------------------------------
Start to parse [i + i * i $]
----------------------------------------------
Iteration Stack Input Action
1 [$] [i + i * i $] Shift
2 [$ i] [+ i * i $] Reduce
3 [$ X] [+ i * i $] Shift
4 [$ X +] [i * i $] Shift
5 [$ X + i] [* i $] Reduce
6 [$ X + X] [* i $] Shift
7 [$ X + X *] [i $] Shift
8 [$ X + X * i] [$] Reduce
9 [$ X + X * X] [$] Reduce
10 [$ X + X] [$] Reduce
11 [$ X] [$] Accept
----------------------------------------------
Start to parse [( i + i ) * i $]
------------------------------------------------------
Iteration Stack Input Action
1 [$] [( i + i ) * i $] Shift
2 [$ (] [i + i ) * i $] Shift
3 [$ ( i] [+ i ) * i $] Reduce
4 [$ ( X] [+ i ) * i $] Shift
5 [$ ( X +] [i ) * i $] Shift
6 [$ ( X + i] [) * i $] Reduce
7 [$ ( X + X] [) * i $] Reduce
8 [$ ( X] [) * i $] Shift
9 [$ ( X )] [* i $] Reduce
10 [$ X] [* i $] Shift
11 [$ X *] [i $] Shift
12 [$ X * i] [$] Reduce
13 [$ X * X] [$] Reduce
14 [$ X] [$] Accept
------------------------------------------------------
Start to parse [i * ( i + i $]
--------------------------------------------------
Iteration Stack Input Action
1 [$] [i * ( i + i $] Shift
2 [$ i] [* ( i + i $] Reduce
3 [$ X] [* ( i + i $] Shift
4 [$ X *] [( i + i $] Shift
5 [$ X * (] [i + i $] Shift
6 [$ X * ( i] [+ i $] Reduce
7 [$ X * ( X] [+ i $] Shift
8 [$ X * ( X +] [i $] Shift
9 [$ X * ( X + i] [$] Reduce
10 [$ X * ( X + X] [$] Reduce
11 [$ X * ( X] [$] Error
--------------------------------------------------
Failed to parse sentences:
invalid sentence: [i * ( i + i $]
```