Skip to content

Commit 0943bd9

Browse files
committed
minor antlr3 changes
1 parent 3e96dbb commit 0943bd9

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

antlr3/ANTLRv3.g4

+18-20
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
grammar ANTLRv3;
3030

3131
grammarDef
32-
: DOC_COMMENT? ('lexer' | 'parser' | 'tree')
32+
: DOC_COMMENT? ('lexer' | 'parser' | 'tree')? 'grammar' id ';' optionsSpec? tokensSpec? attrScope* action* rule_+
3333
;
3434

3535
tokensSpec
@@ -67,7 +67,7 @@ optionValue
6767
| STRING_LITERAL
6868
| CHAR_LITERAL
6969
| INT
70-
| s = '*'
70+
| '*'
7171
;
7272

7373
rule_
@@ -93,7 +93,7 @@ block
9393
;
9494

9595
altList
96-
: alternative rewrite ('|' alternative rewrite)*
96+
: alternative rewrite? ('|' alternative rewrite?)*
9797
;
9898

9999
alternative
@@ -232,17 +232,6 @@ id
232232
| RULE_REF
233233
;
234234

235-
236-
SL_COMMENT
237-
: '//' (' $ANTLR ' SRC | ~ ('\r' | '\n')*) '\r'? '\n' -> skip
238-
;
239-
240-
241-
ML_COMMENT
242-
: '/*' ()*? '*/'
243-
;
244-
245-
246235
CHAR_LITERAL
247236
: '\'' LITERAL_CHAR '\''
248237
;
@@ -342,12 +331,6 @@ fragment SRC
342331
: 'src' ' ' ACTION_STRING_LITERAL ' ' INT
343332
;
344333

345-
346-
WS
347-
: (' ' | '\t' | '\r'? '\n') + -> skip
348-
;
349-
350-
351334
fragment WS_LOOP
352335
: (WS | SL_COMMENT | ML_COMMENT)*
353336
;
@@ -542,3 +525,18 @@ RANGE2
542525
REWRITE
543526
: '->'
544527
;
528+
529+
530+
SL_COMMENT
531+
: '//' ~[\r\n]* -> skip
532+
;
533+
534+
535+
ML_COMMENT
536+
: '/*' .*? '*/' -> skip
537+
;
538+
539+
WS
540+
: (' ' | '\t' | '\r'? '\n') + -> skip
541+
;
542+

antlr3/examples/csv.g

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
3+
Copyright 2011 by Nathaniel Harward <[email protected]>
4+
5+
ANTLRv3 grammar for CSV files.
6+
7+
* No trimming of spaces (disallowed in RFC4180)
8+
* No trimming of double-quotes, either to define/end a quoted field or
9+
when embedded inside one
10+
* Handles all/mixed newline formats (MSDOS/Windows; Unix; Mac OS)
11+
12+
If you find any issues please email me so I can correct it.
13+
14+
*/
15+
16+
grammar csv;
17+
18+
file
19+
: record (NEWLINE record)* EOF
20+
;
21+
22+
record
23+
: (quoted_field | unquoted_field) (COMMA (quoted_field | unquoted_field))*
24+
;
25+
26+
quoted_field
27+
: DQUOTE
28+
( CHAR
29+
| COMMA
30+
| DQUOTE DQUOTE
31+
| NEWLINE
32+
)* DQUOTE
33+
;
34+
35+
unquoted_field
36+
: CHAR*
37+
;
38+
39+
CHAR
40+
: '\u0000' .. '\u0009'
41+
| '\u000b' .. '\u000c'
42+
| '\u000e' .. '\u0021'
43+
| '\u0023' .. '\u002b'
44+
| '\u002d' .. '\uffff'
45+
;
46+
47+
COMMA
48+
: '\u002c'
49+
;
50+
51+
DQUOTE
52+
: '\u0022'
53+
;
54+
55+
NEWLINE
56+
: '\u000d'? '\u000a' // DOS/Windows(\r\n) + Unix(\n)
57+
| '\u000d' // Mac OS 9 and earlier(\r)
58+
;

0 commit comments

Comments
 (0)