Post on 12-Jul-2015
ANTLR 4
Parser Structure
Error RecoveringBy Alexander Vasiltsov
class Grammar_NameParser : Parser {
class RuleContext : ParserRuleContext {...}
RuleContext rule();
class SubruleContext : ParserRuleContext {...}
class LabelContext : SubruleContext {...}
class Label_altContext : SubruleContext {...}
SubruleContext : subrule();
class LoopingContext : ParserRuleContext {...}
LoopingContext looping();
}
grammar Grammar_Name;
rule: subrule TOKEN;
subrule: looping+ # label
| TOKEN # label_alt
;
looping: looping
| TOKEN;
TOKEN: ‘TOKEN’;
Structure of generated parser
grammar Grammar_Name;
rule: subrule TOKEN;
subrule: looping+ # label
| TOKEN # label_alt
;
looping: looping
| TOKEN;
TOKEN: ‘TOKEN’;
class Grammar_NameParser : Parser {
class RuleContext : ParserRuleContext {...}
RuleContext rule();
class SubruleContext : ParserRuleContext {...}
class LabelContext : SubruleContext {...}
class Label_altContext : SubruleContext {...}
SubruleContext : subrule();
class LoopingContext : ParserRuleContext {...}
LoopingContext looping();
}
Structure of Context classes (1)
rule: subrule TOKEN;
class RuleContext : ParserRuleContext {
RuleContext(ParserRuleContext parent, int invokingState) {...}
SubruleContext subrule() {...}
ITerminalNode TOKEN() {...}
void EnterRule(IParseTreeListener listener) {...}
void ExitRule(IParseTreeListener listener) {...}
TResult Accept<TResult>(IParseTreeVisitor<TResult> visitor) {...}
}
Structure of Context classes (2)subrule: looping+ # label
| TOKEN # label_alt
;
class SubruleContext : ParserRuleContext {
SubruleContext(ParserRuleContext parent, int invokingState) {...}
}
class LabelContext : SubruleContext {
LabelContext(ParserRuleContext parent, int invokingState) {...}
IReadOnlyList<LoopingContext> looping() {...}
LoopingContext looping(int i) {...}
void EnterRule(IParseTreeListener listener) {...}
void ExitRule(IParseTreeListener listener) {...}
TResult Accept<TResult>(IParseTreeVisitor<TResult> visitor) {...}
}
class Label_altContext : SubruleContext {
Label_altContext(ParserRuleContext parent, int invokingState) {...}
ITerminalNode TOKEN() {...}
void EnterRule(IParseTreeListener listener) {...}
void ExitRule(IParseTreeListener listener) {...}
TResult Accept<TResult>(IParseTreeVisitor<TResult> visitor) {...}
}
Structure of Context classes (3)
looping: looping
| TOKEN;
class LoopingContext : ParserRuleContext {
LoopingContext(ParserRuleContext parent, int invokingState) {...}
LoopingContext loopig() {...}
ITerminalNode TOKEN() {...}
void EnterRule(IParseTreeListener listener) {...}
void ExitRule(IParseTreeListener listener) {...}
TResult Accept<TResult>(IParseTreeVisitor<TResult> visitor) {...}
}
ITree
ParserRuleContext
IToken
ANTLR namespaces
Antlr4.Runtime
org.antlr.v4.runtime
Contains the most commonly used classes and interfaces, such
as the hierarchies for input streams, character and token
buffers, error handling, token construction, lexing, and parsing
Antlr4.Runtime.Atn
org.antlr.v4.atn
This is used internally for ANTLR’s Adaptive LL(*) lexing and
parsing strategy1
Antlr4.Runtime.Dfa
org.antlr.v4.dfa
Holds all of the DFA2 implementation classes
Antlr4.Runtime.Misc
org.antlr.v4.misc
Holds miscellaneous data structures but also the commonly
used TestRig class
Antlr4.Runtime.Sharpen Holds different helpers for C# target language
Antlr4.Runtime.Tree
org.antlr.v4.tree
This package holds all of the classes and interfaces needed to
implement parser trees
org.antlr.v4.runtime.tree.gui A basic parse tree viewer
1. http://en.wikipedia.org/wiki/Augmented_transition_network
2. http://en.wikipedia.org/wiki/Deterministic_finite_automaton
Recognizer
IIntStream
ITokenFactory
IAntlrErrorListener class diagram
IAntlrErrorStrategy
RecognitionException
Let’s look at example
Error Recovery
Recovery process
● Each generated method is wrapped by try-
catch block
● reoprtError() - reports ErrorListener about
parsing error
● recover() - consumes tokens until it finds one
present in resynchronization set
Resynchronization set
Resynchronization set is the union of rule
reference following sets for all the rules on the
invocation stack
Invocation Stack: [group, expr, atom]
Resynchronization Set: { ‘^’, ‘]’ }
Recovering from mismatched tokens
class 9 T { int i; } class { int i; }
Recovering from errors in subrules
Subrule start: ● Parsers attempt single token
deletion
● Parsers don’t attempt single
token insertion
Looping subrule continuation
test:● Acts inside subrule that is looping
construct (...)+
● Parser tries to stay in the loop after
recovery
● Consumes tokens until it finds a token
consistent with one of following sets:
a) another iteration of the loop
b) what follows the loop
c) the resynchronization set of the
current