This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class PeekableScanner | |
{ | |
private Scanner scan; | |
private String next; | |
public PeekableScanner( String source ) | |
{ | |
scan = new Scanner( source ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Created on Oct 21, 2009 | |
*http://www.java-forums.org/new-java/22195-help-bnf-grammar-program-using-recursive-descent-parsing.html | |
*/ | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Created on Oct 21, 2009 | |
*http://www.java-forums.org/new-java/22195-help-bnf-grammar-program-using-recursive-descent-parsing.html | |
*/ | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Created on Oct 21, 2009 | |
*http://www.java-forums.org/new-java/22195-help-bnf-grammar-program-using-recursive-descent-parsing.html | |
*/ | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'Token.rb' | |
require 'Tokenizer.rb' | |
require 'Node.rb' | |
require 'TreePrinter.rb' | |
# Parser class | |
class Parser | |
def initialize(tokenizer) | |
@tokenizer = tokenizer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax: | |
[Stmts [Stmt [ id [x]] [ assign [:=]] [Expr]] [op [; ]] [Stmts]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x := 1; | |
z := 0 | |
-------- | |
x11 := (2 + 3) * 5; | |
z__ := x11 + 2 | |