Stupid beginners question

54 views
Skip to first unread message

Jochen Wiedmann

unread,
Dec 1, 2016, 5:23:22 AM12/1/16
to antlr-discussion

Hi,

I'd like to parse some fairly simple string, basically an ORDER BY clause. However, when I parse the input string "ORDER BY id ASC, name DESC", I get these error messages/warnings:

line 1:6 extraneous input 'BY' expecting {'AS', WS}
line 1:9 extraneous input 'id' expecting {'AS', WS}
line 1:12 extraneous input 'ASC' expecting {'AS', WS}
line 1:16 extraneous input ' ' expecting ID
line 1:22 extraneous input 'DESC' expecting {'AS', WS}

My grammar (AntLR 4) is below. Glad about any response.

Thanks,

Jochen


grammar OrderBy;

orderType
    : ( DESCENDING | ASCENDING | DESC | ASC )?
    ;

orderBy
    :  ( ORDER WS+ BY)? propertyReference (typeCast)? orderType?
                  (COMMA propertyReference (typeCast)? orderType?)*
    ;

typeCast
     : WS+ 'AS' ID
     ;

propertyReference
     : ID (DOT ID)*
     ;

ID
     : ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*
     ;

WS: [ \n\t\r];

ORDER
: 'ORDER'
;

BY
: 'BY'
;

DESCENDING
: 'DESCENDING'
;

ASCENDING
: 'ASCENDING'
;

DESC
: 'DESC'
;

ASC
: 'ASC'
;

COMMA
: ','
;

DOT
: '.'
;

floor....@aimms.com

unread,
Dec 1, 2016, 9:30:40 AM12/1/16
to antlr-discussion
Hi Jochen, 

The ID rule can match all your keywords, and will, because if more than one lexer rule applies, the one listed first gets picked. So move your ID rule down ( at least beneath all the keyword rules).


Regards, 

Floor

Jochen Wiedmann

unread,
Dec 2, 2016, 9:49:28 AM12/2/16
to antlr-discussion

Hi, Floor,

thanks very much for your suggestion. I did that, but now I am getting

line 1:8 extraneous input ' ' expecting ID
line 1:12 extraneous input 'ASC' expecting {'AS', WS}
line 1:16 extraneous input ' ' expecting ID
line 1:22 extraneous input 'DESC' expecting {'AS', WS}


Thanks,

Jochen

floor....@aimms.com

unread,
Dec 2, 2016, 10:02:54 AM12/2/16
to antlr-discussion
Hi Jochen, 

You probably want to add "-> skip" to your  WS rule, so you do not have to worry about whitespace in your parser rules. 
You can then remove all references to WS in your parser. 

Jochen Wiedmann

unread,
Dec 7, 2016, 6:34:27 PM12/7/16
to antlr-discussion

Thanks very much! That did the trick.
Reply all
Reply to author
Forward
0 new messages