0
votes

I am working on an ANTLRv4 grammar for BUGS - my repo is here, the link points to a particular commit so shouldn't go out of date.

Minimum code example below.

I would like the input rule to go along t route if input is T(, but to go along the id route if the input is T for the grammar below.

grammar temp;

input: t | id;
t: T '(';
id: ID;

T: 'T' {_input.LA(1)==(}?;

ID: [a-zA-Z][a-zA-Z0-9._]*;

My ANLTRv4 specification of BUGS grammar was obtained heavily inspired with the FLEX+BISON lexing and parsing grammar incorporated in JAGS 4.3.0 source code, in files src/lib/compiler/parser.yy and src/lib/compiler/scanner.ll.

The way they accomplish it is by using the trailing context in the lexer, e.g. r/s. The way to do it in ANTLR is given here, but I cannot get it to work.

I need it to work this way because another part of the grammar depends on this mechanism - relevant code fragment here.

You can recreate my particular issue by cloning my repo and running make - this will give list of tokens lexed and error in parsing stage. In the tokens list the letter T is lexed as token 'T' rather than ID as I'd like it to be.

I feel there is much more natural/correct way to do it in ANTLR, but I'm new to this and cannot figure out a way.

PS If you have an idea how to better name this question please edit it.

1

1 Answers

1
votes

If I understand the problem correctly the following code will work fine:

grammar temp;

input: t | id;
t:     T '(';
id:    ID | T;

T:      'T';
LPAREN: '(';
ID: [a-zA-Z][a-zA-Z0-9._]*;