Hallo,

folgende einfache ANTLR-Grammatik:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
main:
  (mainSegment)*
  ;
 
mainSegment:
  labelDeclaration
  | constantsDeclaration
  | globalArraysDeclaration
  ;
 
labelDeclaration:
  '.' IDENTIFIER
  ;
  
constantsDeclaration:
  'Const' constantDeclaration (',' constantDeclaration)*
  ;
 
constantDeclaration:
  SIMPLE_TYPED_IDENTIFIER '=' DECIMAL_LITERAL
  ;
 
globalArraysDeclaration:
  'Dim' globalArrayDeclaration (',' globalArrayDeclaration)*
  ;
 
globalArrayDeclaration:
  TYPED_IDENTIFIER '(' DECIMAL_LITERAL (',' DECIMAL_LITERAL)* ')'
  ;
 
DECIMAL_LITERAL:
  ('0'..'9')+
  ;
 
WHITESPACE:
  '\t'..'\n' | ' ' {$channel=HIDDEN;}
  ;
 
TYPED_IDENTIFIER:
  IDENTIFIER '.' IDENTIFIER
  | SIMPLE_TYPED_IDENTIFIER
  | IDENTIFIER
  ;
 
SIMPLE_TYPED_IDENTIFIER:
  IDENTIFIER SIMPLE_TYPE_PREFIX
  | IDENTIFIER
  ;
 
IDENTIFIER:
  ('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '0'..'9' | '_')*
  ;
 
fragment SIMPLE_TYPE_PREFIX:
  '%' | '#' | '$'
  ;

Ein Beispiel dazu:
Code :
1
2
3
.MyLabel
Global MyVariable
Dim MyArray(10)

Ich muss drei Bezeichnerarten unterscheiden:
  • Für Sprungmarken (labelDeclaration) darf nur ein einfacher Bezeichner (IDENTIFIER) verwendet werden, z. B. MyLabel, Test, A
  • Für Konstanten (constantDeclaration) darf nur ein einfach-getypter Bezeichner (SIMPLE_TYPED_IDENTIFIER) verwendet werden, z. B. MyVariable, Test#, A$, B%
  • Für Arrays (globalArrayDeclaration) darf nur ein getypter Bezeichner (TYPED_IDENTIFIER) verwendet werden, z. B. MyArray, Test#, A$, B%, C.Blub

Folgende Warnung kommt bei "TYPED_IDENTIFIER:":
Code :
1
2
3
Decision can match input such as "{'A'..'Z', 'a'..'z'}" using multiple alternatives: 2, 3
As a result, alternative(s) 3 were disabled for that input
 |---> TYPED_IDENTIFIER:

Folgender Fehler kommt bei "TYPED_IDENTIFIER:"
Code :
1
2
The following alternatives can never be matched: 3
 |---> TYPED_IDENTIFIER:

Folgender Fehler kommt bei "IDENTIFIER:"
Code :
1
2
The following token definitions can never be matched because prior tokens match the same input: SIMPLE_TYPED_IDENTIFIER,IDENTIFIER
 |---> IDENTIFIER:

Was ebenfalls nicht funktioniert ist:
Code :
1
2
3
4
5
6
7
8
9
10
11
TYPED_IDENTIFIER:
  IDENTIFIER ('.' IDENTIFIER | SIMPLE_TYPE_PREFIX)?
  ;
 
SIMPLE_TYPED_IDENTIFIER:
  IDENTIFIER (SIMPLE_TYPE_PREFIX)?
  ;
 
fragment IDENTIFIER:
  ('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '0'..'9' | '_')*
  ;

Hat jemand Rat?

Ciao Olli