[antlr-interest] Composite Grammar [C Target]

Justin Murray jmurray at aerotech.com
Wed Sep 26 06:05:14 PDT 2012


Hi Jim,

How does that work exactly? If a combined grammar uses the tokenVocab
option, can it still define additional tokens of its own? Where does the
implementation of the shared lexer rule go in this case? In just one of
the grammars, or in both (, or in a third independent grammar file)?

I still need to have two independent lexers, because the grammar is
quite different for each. I only want to share a handful of common
tokens, so that I can reduce duplicate code.

Thanks,

- Justin

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: Tuesday, September 25, 2012 7:51 PM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Composite Grammar [C Target]

To use common tokens, take the .tokens file generated by one of the
grammars, merge it with the other .token files, then use tokenVocab
option. You need to keep the token numbers contiguous and unique.

JIm

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Justin Murray
Sent: Wednesday, September 26, 2012 3:50 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Composite Grammar [C Target]

Hello,

I'm trying to use composite grammars, and can't seem to get off of the
ground. Basically, I have two separate parsers. One preprocesses the
source, and the other compiles it. Naturally, they have a few tokens in
common, such as a newline, and I'd prefer to only define that token
once.
This sounds to me like exactly what composite grammars were designed to
do. So I made a CommonTokens.g:

lexer grammar CommonTokens;

NEWLINE_ :  ('\r'? '\n');

Then in my grammar, I added an import:

grammar AerobasicPreprocessor;
import CommonTokens;

options { language=C;  }

line : SOMEOTHERTOKENS_ NEWLINE_;
...


When I run the ANTLR tool on this grammar, I get some less than helpful
errors:

error(100): AerobasicPreprocessor.g:4:1: syntax error: antlr:
org.antlr.runtime.EarlyExitException
error(150):  grammar file AerobasicPreprocessor.g has no rules
error(100): AerobasicPreprocessor.g:0:1: syntax error: assign.types:
org.antlr.runtime.EarlyExitException
org\antlr\grammar\v3\DefineGrammarItemsWalker.g: node from line 326:1
required (...)+ loop did not match anything at input ';'

What am I doing wrong?

Thanks,

- Justin

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list