English 中文(简体)
What s the matter with this Grammar?
原标题:
grammar Test;

IDHEAD: ( a .. z  |  A .. Z  |  _ );
IDTAIL: (IDHEAD |  0 .. 9 );
ID:     (IDHEAD IDTAIL*);
fragment
TYPE:   ( text  |  number  |  bool );

define:  define  ID  as  TYPE;

The problem is that the define rule matches the tokens define, ID, as, but wont match TYPE. I m yielding a MissingTokenException.

If I inline the TYPE, as follows, it works as I m intending:

grammar Test;

IDHEAD: ( a .. z  |  A .. Z  |  _ );
IDTAIL: (IDHEAD |  0 .. 9 );
ID:     (IDHEAD IDTAIL*);
fragment
TYPE:   ( text  |  number  |  bool );

define:  define  ID  as  ( text  |  number  |  bool );

Update: The fragment keyword was added in an effort to resolve another conflict: The following token definitions can never be matched because prior tokens match the same input: TYPE.

最佳回答

In the combined grammar, place your parser rules above your lexer rules. Also, remember that the lexer runs first and only after it s finished the parser runs. The TYPE (lexer) token must be matched before it s known that the define (parser) rule needs it.

Fragment lexer rules do not create tokens, but they can be composed into non-fragment rules that do create tokens. In your example, IDHEAD and IDTAIL are not tokens - they are just used to describe the parts of ID. As such, TYPE and ID are your non-fragment rules, and IDHEAD and IDTAIL are fragment rules.

grammar Test;

define:  define  ID  as  TYPE;

/*
 * Lexer rules only below here
 */

TYPE:   ( text  |  number  |  bool );
ID:     (IDHEAD IDTAIL*);

fragment
IDHEAD: ( a .. z  |  A .. Z  |  _ );

fragment
IDTAIL: (IDHEAD |  0 .. 9 );
问题回答

I thought the lexer rules have a priority of how they are listed. So if you want token TYPE to actually be created, move it above all the other lexer rules.

grammar Test;

fragment
TYPE:   ( text  |  number  |  bool );
IDHEAD: ( a .. z  |  A .. Z  |  _ );
IDTAIL: (IDHEAD |  0 .. 9 );
ID:     (IDHEAD IDTAIL*);

define:  define  ID  as  TYPE;

Isn t it because TYPE is defined as fragment?

Can t test right now, but try and remove fragment, and that should do the trick, plus give you a token to boot.

What is the line with fragment supposed to be doing? I think it should work as you expect if you remove it.





相关问题
ANTLR parser hanging at proxy.handshake call

I am attempting to get a basic ECMAScript parser working, and found a complete ANTLR grammar for ECMAScript 3, which appears to compile ok and produces the appropriate Lexer/Parser/Walker Java files. (...

Will ANTLR Help? Different Suggestion?

Before I dive into ANTLR (because it is apparently not for the faint of heart), I just want to make sure I have made the right decision regarding its usage. I want to create a grammar that will parse ...

How to use ANTLR to parse xml document

can anybody tell how to use ANTLR tool(in java) to create our own grammar for xml documents and how to parse those documents using ANTLR tool(in java)?

JavaCC Problem - Generated code doesn t find all parse errors

Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain ...

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because %s means something to ANTLR: $ cat T.g grammar T; options { language = ...

What s the matter with this Grammar?

grammar Test; IDHEAD: ( a .. z | A .. Z | _ ); IDTAIL: (IDHEAD | 0 .. 9 ); ID: (IDHEAD IDTAIL*); fragment TYPE: ( text | number | bool ); define: define ID as TYPE; The problem ...

热门标签