Code Monkey home page Code Monkey logo

opg-analyzer's Introduction

opg-analyzer

OPG (Operator Precedence Grammar) analyzer. You may refer to Wikipedia 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:

go get github.com/keithnull/opg-analyzer

Checkout to its directory:

cd $GOPATH/src/github.com/keithnull/opg-analyzer

Build it:

go build .

An executable opg-analyzer would now have been generated. Then you can run it with arguments:

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 $]

opg-analyzer's People

Contributors

izackwu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.