[antlr-interest] Using code inserts in syntactic predicates
Mike Gadsdon
m.gadsdon at rheagroup.com
Tue Mar 15 09:44:13 PST 2005
Hi all,
I'm trying to use syntactic predicates in conjuction with testing token text
to resolve parsing ambiguity. Problem is that the code inserts I use end up
surrounded by a condition on inputState.guessing==0, which means that my
token text code only runs when the rule is NOT being run as a syntactic
predicate, but I need it to run always.
Is there any way I can insert code such that it runs independent of the
guessing state?
I did think I could workaround by resetting inputState.guessing to zero by a
code insert in the syntactic predicate call and then setting back to its
correct value after making my checks, but this code insert is also
conditional on inputState.guessing = 0 !
any other ideas ?
Grammar extract is :-
standardObjectPropertyName
: (sopnConfirmationStatus) => sopnConfirmationStatus
| (sopnValidityStatus) => sopnValidityStatus
| (sopnValue) => sopnValue
| ({inputState.guessing = 0;}sopnMonitoringStatus) => sopnMonitoringStatus
| (sopnStatusConsistencyCheckStatus) => sopnStatusConsistencyCheckStatus
| (sopnLimitCheckStatus) => sopnLimitCheckStatus
| (sopnDeltaCheckStatus) => sopnDeltaCheckStatus
| sopnExpectedCheckStatus
;
// following rules define the standard object properties, checking the token
text is
// correct in each case
sopnConfirmationStatus
: a:CONFIRMATION b:IDENTIFIER_WORD
{if (!b.getText().equalsIgnoreCase("status")) {throw new
RecognitionException();}}
{a_AST.setType(Parse.IDENTIFIER_WORD);}
;
sopnValidityStatus
: a:IDENTIFIER_WORD b:IDENTIFIER_WORD
{if (!a.getText().equalsIgnoreCase("validity") ||
!b.getText().equalsIgnoreCase("status")) {throw new
RecognitionException();}}
;
sopnValue
: a:VALUE
{a_AST.setType(Parse.IDENTIFIER_WORD);}
;
sopnMonitoringStatus
: a:IDENTIFIER_WORD b:IDENTIFIER_WORD
{if (!a.getText().equalsIgnoreCase("monitoring") ||
!b.getText().equalsIgnoreCase("status")) {throw new
RecognitionException();}}
;
Code generated is (for one rule) :-
public final void sopnMonitoringStatus() throws RecognitionException,
TokenStreamException {
returnAST = null;
ASTPair currentAST = new ASTPair();
AST sopnMonitoringStatus_AST = null;
Token a = null;
AST a_AST = null;
Token b = null;
AST b_AST = null;
try { // for error handling
a = LT(1);
a_AST = astFactory.create(a);
astFactory.addASTChild(currentAST, a_AST);
match(IDENTIFIER_WORD);
b = LT(1);
b_AST = astFactory.create(b);
astFactory.addASTChild(currentAST, b_AST);
match(IDENTIFIER_WORD);
if ( inputState.guessing==0 ) {
if (!a.getText().equalsIgnoreCase("monitoring") ||
!b.getText().equalsIgnoreCase("status")) {throw new RecognitionException();}
}
sopnMonitoringStatus_AST = (AST)currentAST.root;
}
catch (RecognitionException ex) {
if (inputState.guessing==0) {
reportError(ex);
consume();
consumeUntil(_tokenSet_49);
} else {
throw ex;
}
}
returnAST = sopnMonitoringStatus_AST;
}
==============================================================
Mike Gadsdon mailto:m.gadsdon at rheagroup.com
Rhea System S.A Tel +44 (0)20 8891 0702
Fax +44 (0)20 8891 6702
==============================================================
More information about the antlr-interest
mailing list