Question

I'm trying to parse N3 using ANTLR4;

In pseudo-ANTLR:

{plain_is = plain_of = false}

document : (keywords | triple)* ;
keywords : '@keywords' NCNAME (',' NCNAME)* '.'
  {plain_is = 'is' in ncnames
   plain_of = 'of' in ncnames} ;
triple : value predicate value '.' ;
predicate : value | KW_IS value KW_OF ;
value : QNAME | NCNAME ;
QNAME : [a-z]* ':' [a-z]+
NCNAME : [a-z]+ ;
KW_IS : {plain_is}? 'is' | {!plain_is}? '@is' ;
KW_OF : {plain_of}? 'of' | {!plain_of}? '@of' ;
WS : [ \t\r\n]+ -> skip ;

Ignoring the @keywords semantics for now, this is a valid document:

:x :y :z.
:z is :y of :z.

But if @keywords is used, all keywords not specified in the list must be prefixed with @, which allows using them as NCNAMEs:

@keywords is.
:z is :y @of of.

Is there a way to describe this in ANTLR? i.e. after parsing the keywords production, pass some flags (like plain_is) back to the lexer?

Currently I'm using a permissive grammar and check after parsing, converting the non-listed keywords back to NCNAMEs.

Was it helpful?

Solution

The parser does not have the ability to change the operation of the lexer. Although initiated by the parser, The lexer will process the entire input stream before the parser begins its evaluation of the parser rules.

You may be looking for lexer 'modes', aka lexical modes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top