Class Parser


  • public final class Parser
    extends java.lang.Object
    The Parser class is responsible for performing syntax analysis on a List of Tokens, discovering whether or not the structure of the program conforms to the rules of the source languages grammar.

    It creates a ParseTree that holds the Statements and Expressions which make up the structure of the input code. During this stage any Expressions that are unrecognised by the source language are identified and reported as an error.

    The List of Tokens produced by the Lexer is retrieved, with its contents being parsed sequentially. Each individual Token is inspected and recursively analysed in an attempt to discover the structure of its parent expression or statement, with the result being added to a List of Statements held by a SourceStatement object. This represents the entire program.

    Any compilation errors that occur during this stage are passed to the ErrorHandler in the form of SyntaxError objects.

    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      Parser​(Lexer lexer, ErrorHandler errorHandler, boolean replMode)
      Constructs a Parser object initialised with the tokens from the Lexer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ParseTree getParseTree()
      Returns a ParseTree object generated from the Lexer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Parser

        public Parser​(Lexer lexer,
                      ErrorHandler errorHandler,
                      boolean replMode)
        Constructs a Parser object initialised with the tokens from the Lexer.

        The Lexer always begins at position 0 in List of Tokens, and adds any errors to the errorHandler.

        If REPL mode is true, the Parser will only parse a single line and disallow the use of multiline Statements (e.g. conditionals, loops, blocks). If REPL mode is false, the Parser will parse multiple lines and allow the use of multiline Statements. In summary, the former is for single-line parsing, whilst the latter is for multiline parsing.

        Parameters:
        lexer - The Lexer used to generate the List of Tokens
        errorHandler - The ErrorHandler to store any errors that occur
        replMode - The boolean indicating the Parser should run in REPL mode
    • Method Detail

      • getParseTree

        public ParseTree getParseTree()
        Returns a ParseTree object generated from the Lexer.

        The List of Token objects is retrieved from the Lexer, parsed into a series of Statements and Expressions that represent the structure of the input code, and then returned in the form of a ParseTree.

        Returns:
        A ParseTree containing the root Statement