Skip to content

Instantly share code, notes, and snippets.

{
"lex": {
"macros": {
"digit": "[0-9]",
"esc": "\\\\",
"int": "-?(?:[0-9]|[1-9][0-9]+)",
"exp": "(?:[eE][-+]?[0-9]+)",
"frac": "(?:\\.[0-9]+)"
},
"rules": [
@DmitrySoshnikov
DmitrySoshnikov / sr-rr-confilct.md
Last active May 13, 2024 13:09
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.

@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 <[email protected]>
* MIT Style License (C) 2015
*
* See this "rock-painting" to get the picture of what we're building here:
*

@kangax's ES6 quiz, explained

@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).

Here we go with the explanations:

Question 1:
(function(x, f = () =&gt; x) {
@DmitrySoshnikov
DmitrySoshnikov / dfs-bfs-non-recursive.js
Created October 19, 2015 05:40
Non-recursive DFS and BFS algorithms
/**
* Depth-first and Breadth-first graph traversals.
*
* In this diff we implement non-recursive algorithms for DFS,
* and BFS maintaining an explicit stack and a queue.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style license
*/
@DmitrySoshnikov
DmitrySoshnikov / process-scheduler.js
Created October 15, 2015 19:58
Educational cooperative processes scheduler
/**
* Cooperative tasks scheduler.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*
* An educational implementation to show the work of a processes scheduler.
* In practice you may find using a library like Task.js, or similar.
*
* Test, Node 4 or transformed with Babel:
@DmitrySoshnikov
DmitrySoshnikov / python-code-sharing.py
Created October 5, 2015 21:27
Python: compiled code sharing between several functions
# Compiled code sharing between
# several functions.
arr = []
for k in [1, 2, 3]:
arr.append(lambda: k)
print(arr[0].__code__ == arr[1].__code__) # True
print(arr[0].__code__.co_code) # bytecode
@DmitrySoshnikov
DmitrySoshnikov / 1-python-defaults.py
Last active October 2, 2015 21:30
Python vs ECMAScript default params semantics
# Python
#
# Closures when they are used as default params, capture bindings
# from the environment one level above the class.
#
x = 1 # global env
def wrap():
@DmitrySoshnikov
DmitrySoshnikov / LL1-parsing-table.js
Last active February 15, 2023 14:55
LL(1) Parser. Parsing table, part 2: building the table from First and Follow sets.
/**
* Building LL(1) parsing table from First and Follow sets.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*
* This diff is a continuation of what we started in the previous two diffs:
*
* Dependencies:
*
@DmitrySoshnikov
DmitrySoshnikov / LL1-parser-first-follow-sets.js
Last active March 27, 2024 07:24
LL(1) Parser. Parsing table, part 1: First and Follow sets.
/**
* LL(1) parser. Building parsing table, part 1: First and Follow sets.
*
* NOTICE: see full implementation in the Syntax tool, here:
* https://github.com/DmitrySoshnikov/syntax/blob/master/src/sets-generator.js
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*
* An LL(1)-parser is a top-down, fast predictive non-recursive parser,