Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make ANTLR4 grammar skip comments

I am trying to write an ANTLR4 grammar to parse actionscript3. I've decided to start with something fairly coarse grained:

grammar actionscriptGrammar;

OBRACE:'{';
CBRACE:'}';
STRING_DELIM:'"';

BLOCK_COMMENT : '/*' .*? '*/' -> skip;
EOL_COMMENT : '//' .*? '/n' -> skip;
WS: [ \n\t\r]+ -> skip;

TEXT: ~[{} \n\t\r"]+;

thing
    : TEXT
    | string_literal
    | OBRACE thing+? CBRACE;

string_literal : STRING_DELIM .+? STRING_DELIM;

start_rule
    : thing+?;

Basically, I want a tree of things grouped by their lexical scope. I want comments to be ignored, and string literals be their own things so that any braces they may include do not affect lexical scope. The string_literal rule works fine (such as it is) but the two comment rules don't appear to have any effect. (i.e. comments aren't being ignored).

What am I missing?

like image 525
spierepf Avatar asked Oct 24 '25 19:10

spierepf


1 Answers

This is from a simplified Java grammar I wrote in ANTLR v4.

WS
    : [ \t\r\n]+ -> channel(HIDDEN)
;

COMMENT
    : '/*' .*? '*/' -> skip
;

LINE_COMMENT
    : '//' ~[\r\n]* -> skip
;

May be this could help you out.

Also, try rearranging your code. Write the Parser Rules first and Lexer Rules last. Follow a Top-Down approach. I find it much more helpful in debugging. It will also look nice when you create an HTML export of your grammar from ANTLR 4 Eclipse Plugin.

Good Luck!

like image 132
Lallu Anthoor Avatar answered Oct 26 '25 23:10

Lallu Anthoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!