org.sc3d.apt.sss.v3
Class Parser

java.lang.Object
  extended by org.sc3d.apt.sss.v3.Parser
Direct Known Subclasses:
Calculator, GrammarParser, Validator

public class Parser
extends java.lang.Object

Represents a parser for a particular Grammar. The algorithm used is a depth-first search using an NDFA. Parsers do not look inside Brackets by default. Subclasses can override the 'postProcess()' method to make Parsers customised for a particular Grammar.


Field Summary
 NDFA ndfa
          An NDFA that represents the grammar passed to the constructor and used by 'parse()'.
 
Constructor Summary
Parser(Grammar grammar)
          Constructs a Parser for 'grammar'.
 
Method Summary
 void interactiveTest()
          Prompts for input on 'System.in', parses it, and prints the parse-tree.
 java.lang.Object parse(Sentence sentence)
          Parses 'sentence' and returns the result of applying 'postProcess()' to its parse-tree.
 Tree parseTokens(NDFA ndfa, Token[] tokens, Token after)
          Parses 'tokens' according to 'ndfa' and returns its parse-tree.
 java.lang.Object postProcess(Tree raw)
          Subclasses which are specialised for a paticular Grammar can override this method in order to post-process the parse-tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ndfa

public final NDFA ndfa
An NDFA that represents the grammar passed to the constructor and used by 'parse()'.

Constructor Detail

Parser

public Parser(Grammar grammar)
Constructs a Parser for 'grammar'.

Method Detail

parse

public java.lang.Object parse(Sentence sentence)
Parses 'sentence' and returns the result of applying 'postProcess()' to its parse-tree. This method first uses 'sentence' to construct a Lex, a Match and an Indentation. They are constructed in that order so as to make the error messages as helpful as possible even if they collide. If there are any errors so far, this method returns 'null' (it is the caller's responsibility to print an error report). Otherwise, it calls 'parseTokens(match.toArray())', and again returns 'null' if there are any errors. Otherwise, it returns the result of passing the parse-tree to 'postProcess()'.


parseTokens

public Tree parseTokens(NDFA ndfa,
                        Token[] tokens,
                        Token after)
Parses 'tokens' according to 'ndfa' and returns its parse-tree. If there are any errors, this method attaches appropriate error messages to the Sentence from which the Tokens came and then returns 'null'. If 'tokens.length' is zero, then there is no way of finding the Sentence, so this method does not generate any error messages and simply returns 'null'.

Parameters:
ndfa - an NDFA representing the grammar.
tokens - the Tokens to parse.
after - the first Token after 'tokens', or 'null' if 'tokens' is a whole Sentence. This provides a place to attach an error message in the case that there is something missing at the end of 'tokens'.
Returns:
the raw parse-tree.

postProcess

public java.lang.Object postProcess(Tree raw)
Subclasses which are specialised for a paticular Grammar can override this method in order to post-process the parse-tree. In particular, they might want to call the parse() methods of Brackets. If it finds any errors, this method must attach appropriate error messages to the Sentence from which the Tokens came and then return 'null'. The default implementation simply returns 'raw'.

Parameters:
raw - the non-null parse-tree.
Returns:
a grammar-specific data structure, or 'null' if there were errors.

interactiveTest

public void interactiveTest()
Prompts for input on 'System.in', parses it, and prints the parse-tree. If you write a subclass of Parser, you may want to provide a 'main()' method that tests the parser by calling this method.

In more detail, this method reads a line at a time until it is given a blank line. It concatenates the non-blank lines into a Sentence, parses it, and prints out the value returned by 'postProcess()'. If 'postProcess()' is not overridden, then the default implementation does not parse inside brackets. The method loops until it finds an "end of file" or encounters an IOException.