Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / Recursive-descent-backtracking.js
Last active January 3, 2024 17:15
Recursive descent parser with simple backtracking
/**
* = Recursive descent parser =
*
* MIT Style License
* By Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*
* In this short lecture we'll cover the basic (non-predictive, backtracking)
* recursive descent parsing algorithm.
*
* Recursive descent is an LL parser: scan from left to right, doing
@DmitrySoshnikov
DmitrySoshnikov / event-loop.js
Last active December 14, 2023 23:23
Event loop
/**
* Event loop.
*
* Read details here:
* http://dmitrysoshnikov.com/ecmascript/javascript-the-core-2nd-edition/#job
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
/**
@DmitrySoshnikov
DmitrySoshnikov / syntax.s
Created January 27, 2018 02:01 — forked from mishurov/syntax.s
AT&T assembly syntax and IA-32 instructions
# --------
# Hardware
# --------
# Opcode - operational code
# Assebly mnemonic - abbreviation for an operation
# Instruction Code Format (IA-32)
# - Optional instruction prefix
# - Operational code
@DmitrySoshnikov
DmitrySoshnikov / s-expression-parser.js
Last active October 9, 2023 05:59
S-expression parser
/**
* S-expression parser
*
* Recursive descent parser of a simplified sub-set of s-expressions.
*
* NOTE: the format of the programs is used in the "Essentials of interpretation"
* course: https://github.com/DmitrySoshnikov/Essentials-of-interpretation
*
* Grammar:
*
@DmitrySoshnikov
DmitrySoshnikov / sr-rr-confilct.md
Last active September 22, 2023 17:43
Parsing notes: "Shift-reduce" and "Reduce-reduce" conflicts in LR parsing

"Shift-reduce" and "Reduce-reduce" conflicts in LR parsing.

How to determine?

A full parsing table is not needed, only the canonical collection. In the canonical collection, find all final items (and only final items), and see if:

  • There are both shift and reduce in the same item ("shift-reduce", s/r)
  • There are two reduce actions in the same item ("reduce-reduce", r/r)

If none of these is true, there are no conflicts, even in LR(0). If there are some of the above, SLR(1) still may solve it.

/**
* Mark and Sweep Garbage Collection technique.
* MIT Style License
* by Dmitry Soshnikov
*/
// This diff describes the simplest version of mark and sweep
// GC in order to understand the basic idea. In real practice the
// implementation can be much tricker and optimized.
@DmitrySoshnikov
DmitrySoshnikov / static-dynamic-scope.pl
Created November 12, 2010 22:05
Static and dynamic scope in Perl
# Lexical and dynamic scopes in Perl;
# Static (lexical) scope uses model of
# environments with frames; dynamic scope
# uses single global var frame.
$a = 0;
sub foo {
return $a;
}
sub staticScope {
@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 / python-closures.py
Created November 15, 2010 12:03
Understanding Python's closures
# "Understanding Python's closures".
#
# Tested in Python 3.1.2
#
# General points:
#
# 1. Closured lexical environments are stored
# in the property __closure__ of a function
#
# 2. If a function does not use free variables
@DmitrySoshnikov
DmitrySoshnikov / lr0-items.js
Last active February 15, 2023 14:56
LR Parsing: Canonical collection of LR(0) items
/**
* LR-parsing.
*
* Canonical collection of LR(0) items.
*
* by Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
* MIT Style License (C) 2015
*
* See this "rock-painting" to get the picture of what we're building here:
*