Skip to content

Instantly share code, notes, and snippets.

Avatar

Justin Meiners justinmeiners

View GitHub Profile
View lalr_vs_recursive_descent_parsers.txt
From: https://old.reddit.com/r/compsci/comments/kgpjv/can_somebody_please_explain_the_difference/
Standard recursive descent parsers are LL(1) parsers, yes (the 1 means that you only look at the very next token to decide which rule to apply). Because of the whole recursive descent thing, LL parsers are top-down parsers (whereas LR parsers are bottom-up).
Their big downside, in parsing programming languages, is in parsing infix operator expressions. For example, suppose you want to have a simple grammar that matches a language in which you can add and multiply binary digits (ignoring operator precedence for now):
Expr <- Expr "*" Digit
| Expr "+" Digit
| Digit
Digit <- "0"
@justinmeiners
justinmeiners / grid_neighbors.cpp
Last active October 27, 2022 19:36
How to iterate neighbors of a grid efficiently and concisely.
View grid_neighbors.cpp
#include <algorithm>
template <typename I>
void rotate_2d(I& a, I& b) {
std::swap(a, b);
b = -b;
}
int main() {
// Suppose we want to iterate over the neighbors of a cell in a 2D grid:
@justinmeiners
justinmeiners / read_entire_file.c
Last active October 25, 2022 20:43
Read an entire file into a buffer (without race conditions or seeking).
View read_entire_file.c
void* fread_all(FILE* file, size_t* out_size) {
const size_t BLOCK_SIZE = 32 * 1024;
const size_t MEDIUM_SIZE = 10 * 1024 * 1024;
*out_size = 0;
size_t cap = 0;
char* data = NULL;
while (1) {
@justinmeiners
justinmeiners / lo.sh
Created August 22, 2022 21:04 — forked from BB9z/lo.sh
Reveal the Latest Simulator Application’s Data Container in Finder.
View lo.sh
#!/bin/sh
cd ~/Library/Developer/CoreSimulator/Devices/
isVaildDeviceDir () {
# echo $1
# $1 is not directory or empty
if ! [[ -d $1 ]] || ! [[ -n $1 ]] ; then
return 1
fi
View intersperse.swift
struct InterspersedSequence<S: Sequence>: Sequence, IteratorProtocol {
var separator: S.Element
var iterator: S.Iterator
enum State {
case separator
case element(nextElement: S.Element?)
}
var state: State
View multiplicity.swift
enum CollectionMultiplicity<C: Collection> {
case none
case one(first: C.Index)
case many(first: C.Index, second: C.Index)
}
extension Collection {
func indexedMultiplicity(where predicate: (Element) -> Bool) -> CollectionMultiplicity<Self> {
guard let i = firstIndex(where: predicate) else {
return .none
@justinmeiners
justinmeiners / FileManager+RemoveIfExists.swift
Last active January 14, 2022 23:41
iOS swift FileManager remove/delete file only if it exists. "No such file or directory" fix.
View FileManager+RemoveIfExists.swift
// Most implementations first check if the file exists, and then do the remove.
// This is subject to race conditions and requires accessing the file system twice.
// A better solution is to ignore that particular exception.
extension FileManager {
func removeItemIfExists(at url: URL) throws {
func isDoesNotExist(error: NSError) -> Bool {
return error.domain == NSPOSIXErrorDomain && error.code == ENOENT
}
View scene.glsl
float g_epsilon = 0.002;
float g_max = 50.0;
float sdSphere( vec3 toCenter, float s )
{
return length(toCenter)-s;
}
float sdBox( vec3 p, vec3 b )
{
@justinmeiners
justinmeiners / generic_algebra.cpp
Created August 21, 2021 19:39
Implementing free group "free reduce" and a few other operations in a generic way. (C++20 concepts)
View generic_algebra.cpp
#include <iostream>
#include <iterator>
// With C++20 concepts
#include <concepts>
template<typename F, typename T>
concept unary_operation = std::invocable<F, T>
&& std::same_as<T, std::invoke_result_t<F, T>>;
View sort.cl
; From Page 149 of "LISP" 1st edition.
; This code has been translated to Common Lisp
; from the dialect used in the book:
; table -> *table*
; (store (table n) x) -> (setf (aref table n) x)
; (boole 1 ...) -> (boole boole-and ...)
; greaterp -> >
; lessp -> <
; (quotient x y) -> (floor x y)