0
votes

i am not experienced and still learning about grammar. i just need an example grammar that support for whitespace and special character like !,",#,$,^,&. Because with my current grammar and the rules inside i cant print out some string that support white space and special symbol. i know whitespace are tossed).

For example if i give input : Hello World!!!!!
the output is HelloWorld <-- the whitespace & char (!) tossed

look at this image : problem

why??

here is my current grammar example.g4

    string
    : '"' (~'"'|'\"')* '"'
    ;

Id
    : Letter(Letter|Digit)*
    ;

Num
    : Digit
    ;

fragment
    Digit
    : [0-9]+
    ;

Letter
    : [a-zA-Z]
    ;


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

i was read this link : ANTLR parse strings (keep whitespaces) and parse normal identifiers

but i cant understand what happen. i need an advice. thanks.

1
There's no lexer rule in your grammar which could match whitespace. Add something like WS: [ \t\r\n]+; - Lucas Trzesniewski
dear friends, thanks for coming here and trying to help me. but i got error on the parsetreewalker after add WS: [ \t\r\n]+; to the grammar, My string become null. Object reference not set to an instance of an object. - Gun Gun Febrianza

1 Answers

2
votes

Finnaly i found someone who give me solution. he is jim

and here his solution : You are declaring your rule for string as a parser rule.
Lexer rules should begin with UPPER CASE: STRING : ....

You Letter rules is not a fragment, so it conflicts with Id
You have not specified what to do with whitespace in your lexer, so it will just not match and give an error.
Find one of the online tutorials, or buy the book, or better still, do both.

Jim


now my problem solved, i can print out string with whitespace inside.