Package beaver

Class Parser

  • Direct Known Subclasses:
    JastAddJavaParser

    public abstract class Parser
    extends Object
    Almost complete implementation of a LALR parser. Two components that it lacks to parse a concrete grammar -- rule actions and parsing tables -- are provided by a generated subclass.
    • Field Detail

      • tables

        protected final ParsingTables tables
        The automaton tables.
      • accept_action_id

        protected final short accept_action_id
        Cached ID of the ACCEPT action.
      • states

        protected short[] states
        The parser's stack.
      • top

        protected int top
        Index of the stack's top element, i.e. it's = -1 when the stack is empty;
      • _symbols

        protected Symbol[] _symbols
        The stack of shifted symbols.
      • report

        protected Parser.Events report
        Parsing events notification "gateway"
    • Method Detail

      • parse

        public Object parse​(Scanner source,
                            short alt_goal_marker_id)
                     throws IOException,
                            Parser.Exception
        Parses a source and returns a semantic value of the accepted nonterminal. Before parsing starts injects alternative goal marker into the source to indicate that an alternative goal should be matched.
        Parameters:
        source - of tokens - a Scanner
        alt_goal_marker_id - ID of a token like symbol that will be used as a marker
        Returns:
        semantic value of the accepted nonterminal
        Throws:
        IOException
        Parser.Exception
      • invokeReduceAction

        protected abstract Symbol invokeReduceAction​(int rule_num,
                                                     int offset)
        Invoke actual reduce action routine. Method must be implemented by a generated parser
        Parameters:
        rule_num - ID of a reduce action routine to invoke
        offset - to the symbol before first action routine argument
        Returns:
        reduced nonterminal
      • recoverFromError

        protected void recoverFromError​(Symbol token,
                                        Parser.TokenStream in)
                                 throws IOException,
                                        Parser.Exception
        Implements parsing error recovery. Tries several simple approaches first, like deleting "bad" token or replacing the latter with one of the expected in his state (if possible). If simple methods did not work tries to do error phrase recovery. It is expected that normally descendant parsers do not need to alter this method. In same cases though they may want to override it if they need a different error recovery strategy.
        Parameters:
        token - a lookahead terminal symbol that messed parsing
        in - token stream
        Throws:
        IOException - propagated from a scanner if it has issues with the source
        Parser.Exception - if Parser cannot recover