Giter Site home page Giter Site logo

ll1-js's Introduction

ll1

ll(1) Grammar Analyzer

A LL1 grammar converter. The non-LL1 grammar is transformed into LL1 grammar by eliminating left recursion, simplifying and extracting left factors.

Start

Install

$ npm install ll1-js --save

Usage

const { translator } = require('ll1-js');
const path = require('path');

const inputPath = path.resolve(__dirname, './g.txt');

const ll1Produtions = translator({ inputPath });

Example

code

// g.txt

E->T|E+T;
T->F|T*F;
F->i|(E);
// ./test/index.js

const path = require('path');
const filePath = path.resolve(__dirname, './g.txt');
const { translator, Grammar, utils } = require('../index');
(() => {
  const config = {
    displayProcess: true,
    inputPath: filePath,
    outputPath: false,
    startSymbol: 'E',
  }

  const { productions, nonTerminator, terminator, first, follow, table } = translator(config);

  // all data export from `translator`
})()

result

===== (1) start creating grammar =====
Grammar:
Terminal: +,*,i,(,)
NonTerminal: E,T,F
StartSymbol: E
ProductionFormula:
E->T|E+T;
T->F|T*F;
F->i|(E);
===== (1) completed =====

===== (2) start eliminating left recursion =====
Grammar:
Terminal: i,(,),*,~,+
NonTerminal: E,T,A,F,R
StartSymbol: E
ProductionFormula:
E->TA;
T->FR;
F->i|(E);
R->*FR|~;
A->+TA|~;
===== (2) completed =====

===== (3) start simplifying grammar =====
Grammar:
Terminal: i,(,),*,~,+
NonTerminal: E,T,A,F,R
StartSymbol: E
ProductionFormula:
E->TA;
T->FR;
F->i|(E);
R->*FR|~;
A->+TA|~;
===== (3) completed =====

===== (4) start extracting left common factor  =====
Grammar:
Terminal: i,(,),*,~,+
NonTerminal: E,T,A,F,R
StartSymbol: E
ProductionFormula:
E->TA;
T->FR;
F->i|(E);
R->*FR|~;
A->+TA|~;
===== (4) completed =====

===== (5) start generateFirsts  =====
Grammar:
Terminal: i,(,),*,~,+
NonTerminal: E,T,A,F,R
StartSymbol: E
ProductionFormula:
E->TA;
T->FR;
F->i|(E);
R->*FR|~;
A->+TA|~;
===== (5) completed =====

===== (6) start generateFollows  =====
Grammar:
Terminal: i,(,),*,~,+
NonTerminal: E,T,A,F,R
StartSymbol: E
ProductionFormula:
E->TA;
T->FR;
F->i|(E);
R->*FR|~;
A->+TA|~;
===== (6) completed =====

===== (7) start generateTable  =====
Grammar:
Terminal: i,(,),*,~,+
NonTerminal: E,T,A,F,R
StartSymbol: E
ProductionFormula:
E->TA;
T->FR;
F->i|(E);
R->*FR|~;
A->+TA|~;
===== (7) completed =====

===== !over! =====

Config

example

{
  inputPath: '',
  displayProcess: false,
  outputPath: './output.txt',
  startSymbol: 'S',
}

explain

inputPath [String] *required

The path where the grammar file is located

tip: use 'path' package to resolve the relative path to absolute path;

displayProcess [Boolean]: true | false

Whether or not the translation process is displayed

The translator takes four steps to convert a non-ll1 grammar into LL1 grammar:

  1. Generating grammar from productions.
  2. Eliminatie left recursion.
  3. Simplify.
  4. Extracte left common factor.
  5. generateFirsts
  6. generateFollows
  7. generateTable

When you set this attribute to truthy, you'will see the every step result on the terminal.

outputPath [String | Boolean]: use false to disable it

The output path of the transformed grammar

startSymbol [Char]

A correct grammar requires a startSymbol. :)

Issue

Welcome to submit issue on my Github

Github

https://github.com/wingsico

ll1-js's People

Contributors

wzono avatar lynxife avatar

Stargazers

 avatar Aylin  avatar  avatar

Watchers

James Cloos avatar

ll1-js's Issues

Starting and running the project

Hello. How can we run this app to see the result? I am just a beginner in JS field. After I run the npm install command to install the packages, and run the index.js file to see the results, the below error is shown up:

D:\Compiler\ll1>node ./test/index.js
D:\Compiler\ll1\test\index.js:2
const filePath = path.resolve(__dirname, './g.txt');
                      ^

TypeError: path.resolve is not a function
    at Object.<anonymous> (D:\Compiler\ll1\test\index.js:2:23)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.14.0

Thanks!

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.