tscc-compiler
raw JSON → 1.1.3 verified Fri May 01 auth: no javascript
An LALR(1) compiler generator written in TypeScript that produces both tokenizer and parser from a single grammar specification. Stable version 1.1.3 generates parsers in JavaScript and TypeScript. It implements the Hanolee algorithm for LALR(1) table construction and includes a CLI tool, a module API, and browser support. Unlike many parser generators, it handles lexical analysis generation in addition to parsing, and provides detailed reporting of DFA and LALR parse tables. Maintenance is irregular.
Common errors
error Error: Cannot find module 'tscc-compiler' ↓
cause Package not installed or used in a browser context without including the script.
fix
Run
npm install tscc-compiler for Node.js, or include <script src='tscc.min.js'></script> in HTML. error TypeError: tscc is not a function ↓
cause Attempting to call the default export directly instead of calling `main`.
fix
Use
import { main } from 'tscc-compiler' then call main(...), or use const tscc = require('tscc-compiler'); tscc.main(...). error The 'stdout' option must have 'write' and 'writeln' methods. ↓
cause Missing method in the `stdout` object passed to `main`.
fix
Provide an object with both functions, e.g.,
{ write: (s) => process.stdout.write(s), writeln: (s) => console.log(s) }. Warnings
breaking The main function signature changed between versions: older versions required a callback, now it returns 0/-1. ↓
fix Update call sites to expect a numeric return value (0 success, -1 failure) instead of a callback.
gotcha The `stdout` object must implement both `write` and `writeln` methods. Missing either causes a runtime error. ↓
fix Ensure the `stdout` option object has `write(s)` and `writeln(s)` methods, even if they are empty functions.
gotcha The output file is NOT the generated parser; it's a report file containing DFA and parse tables. The parser itself is written to `inputFile` with `.ts` or `.js` extension. ↓
fix Use `outputFile` for the report; the parser is auto-named based on `inputFile`.
deprecated The `-t` CLI option for test input uses a custom format (tokens in <>, raw strings). This is not validated and can produce confusing errors. ↓
fix For testing, use the programmatic API with `testInput` option instead of CLI.
Install
npm install tscc-compiler yarn add tscc-compiler pnpm add tscc-compiler Imports
- main wrong
const tscc = require('tscc-compiler').main;correctimport { main } from 'tscc-compiler'; - tscc-compiler CLI wrong
tscc-compiler --helpcorrectnpx tscc-compiler file.y - tscc wrong
const tscc = require('tscc-compiler'); tscc.main(...);correctimport tscc from 'tscc-compiler';
Quickstart
import fs from 'fs';
import { main } from 'tscc-compiler';
const input = fs.readFileSync('grammar.y', 'utf-8');
const result = main({
inputFile: 'grammar.y',
input: input,
outputFile: 'grammar.output',
stdout: {
write: (s) => process.stdout.write(s),
writeln: (s) => console.log(s || '')
},
writeFile: (path, content) => fs.writeFileSync(path, content),
printDetailedTime: true,
printDFA: true,
showlah: false,
showFullItemsets: false
});
if (result !== 0) {
console.error('Compilation failed');
}