Package beaver
Class Parser
- java.lang.Object
-
- beaver.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classParser.EventsThis class "lists" reportable events that might happen during parsing.static classParser.ExceptionclassParser.SimulatorSimulator is a stripped (of action code) version of a parser that will try to parse ahead token stream after a syntax error.classParser.TokenStreamThis class wraps a Scanner and provides a token "accumulator" for a parsing simulation.
-
Field Summary
Fields Modifier and Type Field Description protected Symbol[]_symbolsThe stack of shifted symbols.protected shortaccept_action_idCached ID of the ACCEPT action.protected Parser.EventsreportParsing events notification "gateway"protected short[]statesThe parser's stack.protected ParsingTablestablesThe automaton tables.protected inttopIndex of the stack's top element, i.e.
-
Constructor Summary
Constructors Modifier Constructor Description protectedParser(ParsingTables tables)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract SymbolinvokeReduceAction(int rule_num, int offset)Invoke actual reduce action routine.Objectparse(Scanner source)Parses a source and returns a semantic value of the accepted nonterminalObjectparse(Scanner source, short alt_goal_marker_id)Parses a source and returns a semantic value of the accepted nonterminal.protected voidrecoverFromError(Symbol token, Parser.TokenStream in)Implements parsing error recovery.
-
-
-
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"
-
-
Constructor Detail
-
Parser
protected Parser(ParsingTables tables)
-
-
Method Detail
-
parse
public Object parse(Scanner source) throws IOException, Parser.Exception
Parses a source and returns a semantic value of the accepted nonterminal- Parameters:
source- of tokens - a Scanner- Returns:
- semantic value of the accepted nonterminal
- Throws:
IOExceptionParser.Exception
-
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 Scanneralt_goal_marker_id- ID of a token like symbol that will be used as a marker- Returns:
- semantic value of the accepted nonterminal
- Throws:
IOExceptionParser.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 invokeoffset- 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 parsingin- token stream- Throws:
IOException- propagated from a scanner if it has issues with the sourceParser.Exception- if Parser cannot recover
-
-