salut
voici un parseur tout simple :
en gros je souhaitais que mon parseur accepte la ligne suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 class MonParser extends Parser; options { defaultErrorHandler = false; // Don't generate parser error handlers } // Define some methods and variables to use in the generated parser. { public void parse() throws Exception{ return mainStatement(); } } mainStatement[] returns [] throws InvalidSymbolException, ParseException : "hello" EOF {System.err.println("trouvé coucou ET EOF");} ; constantExpression[] returns [Object exp = null] throws InvalidSymbolException { Object constant = null; Integer aInteger; Double aDouble; String aString;Float aFloat;Long aLong;Character aCharacter; } : ( aDouble = isDoubleLiteral[] { constant = new ConstantExpression(JavaObjectType.DOUBLE, aDouble); } | aFloat = isFloatingPointLiteral[] { constant = new ConstantExpression(JavaObjectType.DOUBLE, aFloat); } | aInteger = isIntegerLiteral[] { constant = new ConstantExpression(JavaObjectType.INTEGER, aInteger); } | aLong = isLongLiteral[] { constant = new ConstantExpression(JavaObjectType.DOUBLE, aLong); } ) { exp = constant; } ; isDoubleLiteral [] returns [Double self = null] : token : NUM_DOUBLE { String text = token.getText(); self = new Double(text); } ; isFloatingPointLiteral [] returns [Float self = null] : token : NUM_FLOAT { String text = token.getText(); self = new Float(text); } ; isIntegerLiteral [] returns [Integer self = null] : token : INT_LITERAL { self = new Integer(token.getText()); } ; isLongLiteral [] returns [Long self = null] : token : LONG_LITERAL { self = new Long(token.getText()); } ; isIdentifier[] returns [Object aIdentifier = null] : token : IDENTIFIER { aIdentifier = token.getText(); } ; class SMSLexer extends Lexer; options { charVocabulary = '\0'..'\377'; testLiterals=false; // don't automatically test for literals k=2; // two characters of lookahead } // @@startrules //--------- // COMMENTS // -------- // Single-line comments COMMENT : "//" (~('\n'|'\r'))* { $setType(Token.SKIP); } ; // multiple-line comments ML_COMMENT : "/*" ( /* '\r' '\n' can be matched in one alternative or by matching '\r' in one iteration and '\n' in another. I am trying to handle any flavor of newline that comes in, but the language that allows both "\r\n" and "\r" and "\n" to all be valid newline is ambiguous. Consequently, the resulting grammar must be ambiguous. I'm shutting this warning off. */ options { generateAmbigWarnings=false; } : { LA(2)!='/' }? '*' | '\r' '\n' {newline();} | '\r' {newline();} | '\n' {newline();} | ~('*'|'\n'|'\r') )* "*/" { $setType(Token.SKIP); } ; //----------------------- // WHITESPACE -- ignored // ---------------------- WS : ( ' ' | '\t' | '\f' // handle newlines | ( "\r\n" // DOS/Windows | '\r' // Macintosh | '\n' // Unix ) // increment the line count in the scanner { newline(); } ) { $setType(Token.SKIP); } ; //------------ // IDENTIFIER // ----------- IDENTIFIER options { testLiterals=true; } : LETTER (LETTER | DIGIT)* ; protected LETTER : ('a'..'z'|'A'..'Z') ; protected DIGIT : ('0'..'9') ; //------------ // LITERALS // ----------- // hexadecimal digit (again, note it's protected!) protected HEX_DIGIT : (DIGIT|'A'..'F'|'a'..'f') ; // a numeric literal INT_LITERAL { boolean isDecimal = false;} : ( MINUS )? ( '.' { $setType(DOT); } (('0'..'9')+ (EXPONENT)? (FLOAT_SUFFIX)? { $setType(NUM_FLOAT); })? | ( '0' { isDecimal = true; } // special case for just '0' ( ('x' | 'X') ( // hex // the 'e'|'E' and float suffix stuff look // like hex digits, hence the (...)+ doesn't // know when to stop: ambig. ANTLR resolves // it correctly by matching immediately. It // is therefor ok to hush warning. options { warnWhenFollowAmbig = false; } : HEX_DIGIT )+ | ('0'..'7')+ // octal )? | ('1'..'9') ('0'..'9')* { isDecimal = true; } // non-zero decimal ) ( ('l' | 'L') { $setType(LONG_LITERAL); } | // only check to see if it's a float if looks like decimal so far { isDecimal }? { $setType(NUM_FLOAT); } ( '.' ('0'..'9')* (EXPONENT)? ( FLOAT_SUFFIX | DOUBLE_SUFFIX { $setType(NUM_DOUBLE); } )? | EXPONENT (FLOAT_SUFFIX | DOUBLE_SUFFIX { $setType(NUM_DOUBLE); })? | FLOAT_SUFFIX | DOUBLE_SUFFIX { $setType(NUM_DOUBLE); } ) )? | ( FLOAT_SUFFIX | DOUBLE_SUFFIX { $setType(NUM_DOUBLE); } )? ) ; protected EXPONENT : ('e'|'E') ('+'|MINUS)? (DIGIT)+ ; protected FLOAT_SUFFIX : 'f'|'F' ; protected DOUBLE_SUFFIX : 'd'|'D' ; //------------------ // OPERATORS //------------------ MINUS : '-' ; DOT : '.' ;
Mais refuse la ligne suivante
Code : Sélectionner tout - Visualiser dans une fenêtre à part hello
Or lorsqu'il fait match(Token.EOF_TYPE); il n'arrive pas a le detecter... avez vous une idée de contournement ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part hello world
Partager