1.1.3 • Published 5 years ago

tscc-compiler v1.1.3

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

tscc-compiler

An LALR(1) compiler generator written in typescript. It generates both the tokenizer and the parser. Currently it can generate parsers written in javascript and typescript. More target languages will be supported in future releases.

Check out wiki for tscc-compiler!

Installation

To use it in node, you can install tscc-compiler via npm:

npm install tscc-compiler -g

Or, to use it in browsers, just download the file tscc.js or tscc.min.js and reference them with a script tag. The latter is a compressed version of the former.

Usage

From command line interface (CLI)

To generate the corresponding parser whose grammar is specified in test.y, for example, you could use the following command:

tscc-compiler test.y

The output files actually depends on the target language. For js and ts, test.ts or test.js would be generated respectively, plus a report file test.output, which contains the lexical DFA tables and LALR parse table.

Options for CLI

OptionDescriptionArgument
-o, --outputSpecify the output file to print DFA and parse tableName of the output file
-t, --testParse (no lexical analyse) the given input string. Parsing process will be printed. See below for explanation.The string to be parsed.
-d, --detail-timePrint a detailed list of time costs of different generation phases.No
-h, --helpPrint help message and quitNo

From module

This project uses module bundler rollup to create a source file tscc.js that contains the entail source code for tscc-compiler. You may import it as a module by var tscc = require('tscc-compiler'); or include tscc.js with a script tag in browsers. A simple way to invoke tscc-compiler is calling tscc.main with the argument being an object that contains various options. It returns 0 if no error ocurrs, otherwise it returns -1. Options are listed below:

OptionRequiredTypeDescription
inputFileYesstringName of the input file
inputYesstringContent of the input file
outputFileNostringName of output file (not to be confused with output parser). If not specified, the output file won't be generated.
stdoutYestscc.io.OutputStreamAn interface object to output all the messages. This object must contain write(s) and writeln(s).
writeFileYes(path: string, content: string) => anyA callback to write files.
testInputNostringTest input. If specified, the result will be printed. See below for explanation.
printDetailedTimeYesbooleanWhether to print the detailed time cost list.
printDFANobooleanWhether to print lexical DFA tables in the output file.
showlahNobooleanWhether to show look-ahead tokens of items when printing parse table.
showFullItemsetsNobooleanWhether to show full item sets when printing parse table. If not specified or set to false, only kernel items will be printed.

Where type notations in Typescript are used.

Here's a simple example:

var fs = require('fs');
var tscc = require('tscc-compiler').main;
tscc({
    inputFile: 'example.y',
    input: fs.readFileSync('example.y', 'utf-8'),
    outputFile: 'example.output',
    stdout: {
    	write: function(s){ process.stdout.write(s); },
        writeln: function(s){ console.log(s || ''); }
    },
    writeFile: function(path, content){
        fs.writeFileSync(path, content);
    },
    printDetailedTime: true
});

The module also provides a more flexible way to use it.

Test input

You can give the tscc-compiler a test input string to test if the grammar works. Input string consists of the following two elements, seperated by spaces:

  • An identifier parenthesised by <> is a token, referenced by its name;
  • A raw string is also a token, but referenced by alias;

For example, to test the calculator grammar (see examples/calculator/):

tscc-compiler caculator.y -t "<CONST> + <CONST> * <CONST>"

The output should be:

preparing for test
| <CONST> "+" <CONST> "*" <CONST> 
<CONST> | "+" <CONST> "*" <CONST> 
expr | "+" <CONST> "*" <CONST> 
expr "+" | <CONST> "*" <CONST> 
expr "+" <CONST> | "*" <CONST> 
expr "+" expr | "*" <CONST> 
expr "+" expr "*" | <CONST> 
expr "+" expr "*" <CONST> | 
expr "+" expr "*" expr | 
expr "+" expr | 
expr | 
start | 
accepted!
compilation done in 0.071s
0 warning(s), 0 error(s)

Grammar file

The syntax of the grammar specifying file used by tscc is similiar to yacc. Checkout wiki for tscc-compiler for a specification of grammar file, and examples/ for explicit usages.

Syntax highlight

A syntax highlight mode of grammar file for CodeMirror can be found at web-demo/lib/tscc-highlight-codemirror.js. Feel free to check it out and use it.

Demo

An online demo for tscc-compiler can be found here.

License

MIT.