Skip to content

Instantly share code, notes, and snippets.

View hectormethod's full-sized avatar

Scott K. hectormethod

View GitHub Profile
@hectormethod
hectormethod / Asset.txt
Last active September 12, 2017 13:42
[PeekableScanner] #java
import java.util.Scanner;
public class PeekableScanner
{
private Scanner scan;
private String next;
public PeekableScanner( String source )
{
scan = new Scanner( source );
@hectormethod
hectormethod / Simple Expression Parser.txt
Last active September 12, 2017 14:18
[Simple Expression Parser] #java
When we try to parse a phrase that contains a syntax error, we need some way to respond to the error. A convenient way of doing this is to throw an exception. I'll use an exception class called ParseError, defined as follows:
/**
* An object of type ParseError represents a syntax error found in
* the user's input.
*/
private static class ParseError extends Exception {
ParseError(String message) {
super(message);
}
@hectormethod
hectormethod / BNFgrammar.java
Last active September 12, 2017 14:17
[BNFGrammar example]
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/* The trick is to recognize what the 'terminal' nodes, or literal characters are, what would come from scanning the character stream. In this case,
@hectormethod
hectormethod / MyBNFGrammar01.java
Last active September 12, 2017 14:15
[MyBNFGrammar.java ver1] #java
/**
*Created on Oct 21, 2009
*http://www.java-forums.org/new-java/22195-help-bnf-grammar-program-using-recursive-descent-parsing.html
*/
/**
@hectormethod
hectormethod / MyBNFGrammar03.java
Last active September 12, 2017 14:14
MyBNFGrammar.java ver3
/**
*Created on Oct 21, 2009
*http://www.java-forums.org/new-java/22195-help-bnf-grammar-program-using-recursive-descent-parsing.html
*/
/**
@hectormethod
hectormethod / MyBNFGrammar02.java
Last active September 12, 2017 14:16
[MyBNFGrammar.java ver2] #java
/**
*Created on Oct 21, 2009
*http://www.java-forums.org/new-java/22195-help-bnf-grammar-program-using-recursive-descent-parsing.html
*/
/**
@hectormethod
hectormethod / ParseTreeDisplay.rb
Last active September 12, 2017 14:13
[TreePrinter - ver 01] used for printing a text representation of a parse tree #ruby
# TreePrinter
# Prints text representation of parse tree for given statement
# Items in the same vertical column are on the same tree level
# Items one column to the right are children of the item
class ParseTreeDisplay
class Item
def initialize(node)
@node = node
@remaining = node.get_children().length
require 'Token.rb'
require 'Tokenizer.rb'
require 'Node.rb'
require 'TreePrinter.rb'
# Parser class
class Parser
def initialize(tokenizer)
@tokenizer = tokenizer
@hectormethod
hectormethod / Asset.txt
Created September 12, 2017 13:38
phpParse tree
syntax:
[Stmts [Stmt [ id [x]] [ assign [:=]] [Expr]] [op [; ]] [Stmts]]
@hectormethod
hectormethod / Asset.txt
Created September 12, 2017 13:38
WHILE tests
x := 1;
z := 0
--------
x11 := (2 + 3) * 5;
z__ := x11 + 2