Skip to content

Instantly share code, notes, and snippets.

@yydai
yydai / Calc.java
Last active January 4, 2024 13:23
chapter4 of antlr4---Building a Calculator Using a Visitor
/***
* Excerpted from "The Definitive ANTLR 4 Reference",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/tpantlr2 for more book information.
***/
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
@yydai
yydai / ArrayInit.g4
Created February 5, 2017 03:17
Chapter3 of antlr4----convert initialized Java short arrays to strings(like {1,2,3} to "\u0001\u0002\u0003")
/** Grammars always start with a grammar header. This grammar is called
* ArrayInit and must match the filename: ArrayInit.g4
*/
grammar ArrayInit;
/** A rule called init that matches comma-separated values between {...}. */
init : '{' value (',' value)* '}' ; // must match at least one value
/** A value can be either a nested array/struct or a simple integer (INT) */
value : init
@yydai
yydai / get().js
Last active May 30, 2017 13:53
Javascript HTTP GET response
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
jQuery.ajax({
type: 'GET',
dataType:'json',
url: 'https://api.github.com/repos/yydai/yydai.github.io/issues/1/comments',
success: function(data){
//marker = JSON.stringify(data);
//alert(marker);
var base_url = 'https://api.github.com';
var title = document.title;
var owner = 'yydai';
var repo = 'yydai.github.io';
var search_issues = base_url + '/search/issues?q=' + title + '+user:' + owner;
function test() {
jQuery.ajax({
type: 'GET',
pub_date = `date +'%y.%m.%d %H:%M:%S'`
messages = "Auto published by yydai at $(pub_date)"
publish:
@echo "======================================"
@echo "Begin publish the site, please wait..."
@echo "======================================"
@git add .
@-git commit -m $(messages)
#coding:utf-8
import re
import requests
from bs4 import BeautifulSoup
def join_str(contents):
strings = []
for content in contents:
if isinstance(content, basestring):
strings.append(content)
@yydai
yydai / kafka-py.py
Last active September 27, 2018 16:58
python kafka consumer producer
# from: https://github.com/dpkp/kafka-python/blob/master/example.py
import threading, logging, time
import multiprocessing
from kafka import KafkaConsumer, KafkaProducer
class Producer(threading.Thread):
def __init__(self):
@yydai
yydai / plot1.py
Last active May 17, 2018 08:02
Plot
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 200) # x data, shape=(100, 1)
y1 = 2 * x #...
y2 = x ** 2 + 2
y3 = sin(x)
plt.figure(1, figsize=(8, 6))
plt.subplot(221)
@yydai
yydai / trie-spellcheck-autocomplete.cpp
Created June 4, 2018 04:26
使用trie进行补全和检查错误。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#define N 256
using namespace std;
struct TreeNode {
bool isEnd;