Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / calculator.grammar.js
Last active October 23, 2016 04:33
Calculator grammar
/**
* Calculator grammar for a parser in Python.
*
* syntax-cli -g calculator.grammar.js -m LALR1 -o calcparser.py
*
* >>> import calcparser
* >>> calcparser.parse('2 + 2 * 2')
* >>> 6
*/
import calcparser
print(calcparser.parse('2 + 2 * 2')) # int(6)
@DmitrySoshnikov
DmitrySoshnikov / calculator.ast.js
Last active May 14, 2021 08:36
calculator.ast.js
/**
* Calculator grammar for a parser in JavaScript.
*
* syntax-cli -g calculator.ast.js -m LALR1 -o CalcParser.js
*
* const CalcParser = require('./CalcParser.js');
* console.log(CalcParser.parse('2 + 2 * 2')); // AST
*/
{
@DmitrySoshnikov
DmitrySoshnikov / using-calc-parser.js
Created October 18, 2016 10:05
using-calc-parser.js
const CalcParser = require('./CalcParser.js');
console.log(CalcParser.parse('2 + 2 * 2')); // AST
/*
Result:
{
type: "Binary",
left: {
@DmitrySoshnikov
DmitrySoshnikov / calculator.ast.php.js
Created October 18, 2016 10:12
calculator.ast.php.js
/**
* Calculator grammar for a parser in Python.
*
* syntax-cli -g calculator.ast.php.js -m LALR1 -o CalcParser.php
*
* <?php
*
* require('CalcParser.php');
* var_dump(CalcParser::parse('2 + 2 * 2'));
*/
@DmitrySoshnikov
DmitrySoshnikov / using-calc-parser.php
Last active October 18, 2016 10:14
using-calc-parser.php
<?php
require('CalcParser.php');
var_dump(CalcParser::parse('2 + 2 * 2'));
/*
Result:
@DmitrySoshnikov
DmitrySoshnikov / lex-start-conditions.md
Last active April 26, 2023 11:46
Lexer start conditions

Start conditions of lex rules, and tokenizer states

Start conditions are declared in the definitions (first) section of the input using unindented lines beginning with either %s or %x followed by a list of names. The former declares inclusive start conditions, the latter exclusive start conditions. A start condition is activated using the BEGIN action. Until the next BEGIN action is executed, rules with the given start condition will be active and rules with other start conditions will be inactive. If the start condition is inclusive, then rules with no start conditions at all will also be active. If it is exclusive, then only rules qualified with the start condition will be active. A set of rules contingent on the same exclusive start condition describe a scanner which is independent of any of the other rules in the flex input. Because of this, exclusive start conditions make it easy to specify "mini-scanners" which scan portions of the input that are syntactically different from the res

@DmitrySoshnikov
DmitrySoshnikov / calc-no-assoc.g.js
Created October 24, 2016 01:19
calc-no-assoc.g.js
/**
* Explicit handling of the operator associativity. See also implicit handling
* https://gist.github.com/DmitrySoshnikov/ab6ed4d41505c579cee9af759e77ead9
*
* Calculator grammar for a parser in Python.
*
* syntax-cli -g calc-no-assoc.g.js -m LALR1 -o calcparser.py
*
* >>> import calcparser
* >>> calcparser.parse('2 + 2 * 2')
@DmitrySoshnikov
DmitrySoshnikov / grammar-with-ops-assoc-table.txt
Last active October 24, 2016 01:26
Comparison of parsing tables for grammars with and without operators precedence and assoc
.dmitrys:syntax$ syntax-cli --grammar ~/calculator.grammar.js --mode LALR1 --table
Parsing mode: LALR(1).
Grammar:
0. $accept -> E
----------------
1. E -> E + E
2. | E * E
@DmitrySoshnikov
DmitrySoshnikov / 1.grammar.ll1
Created November 29, 2016 00:33
LL(1) boolean
%%
boolean
: term bool'
;
bool'
: 'OR' term bool'
| /* epsilon */
;