1

I'm trying to write a parser with ANTLR4 that should parse a very simple model file from hyperledger:

asset Car identified by id {
  o String id
  o String model
  --> Owner owner
}

participant Owner identified by id {
  o String id
  o String name
}

transaction Auction {
  --> Car asset
  o String newValue
}

I'm having a problem within the transaction: I can't give the variable of type Car the name 'asset', which is valid in the language specification. The ANTLR parser doesn't expect that. I get an exception similar like that: mismatched input 'o' expecting {'[', IDENTIFIER} I know the cause is the assetDeclaration in my grammar, which detects the asset keyword in the transaction as well.

Is there a way to ignore the asset within the transaction? Or can I make the declaration more specific, so that it is triggered when the asset is at the start and followed by letters?

This is my asset declaration:

assetDeclaration: ASSET IDENTIFIER
  (EXTENDS typeType)?
  IDENTIFIED
  IDENTIFIER
  classBody;

And my transaction declaration:

transactionDeclaration: TRANSACTION IDENTIFIER
  classBody
;

Asset, Transaction and so on defined as:

 ASSET: 'asset';
 TRANSACTION: 'transaction';
 IDENTIFIED: 'identified by';
 PARTICIPANT: 'participant';

The rest of the grammar is similar to the JavaGrammar which is available at GitHub

Spindizzy
  • 7,244
  • 1
  • 19
  • 33
  • The name for this is "non-reserved keyword" - a word that has some special meaning to the language, but may also be used as an identifier. – Jiri Tousek Nov 05 '18 at 08:17

1 Answers1

2

If asset is allowed to appear anywhere where an identifier is allowed, you can just define identifier: IDENTIFIER | ASSET; and then use identifier everywhere instead of IDENTIFIER.

If asset is only allowed in certain places where identifiers are allowed, you should explicitly write (IDENTIFIER | ASSET) in those places.

The same applies to any other keywords that are also legal as identifiers.

sepp2k
  • 363,768
  • 54
  • 674
  • 675