Converting Antlr 4 grammar into Antlr 3.5 - pushMode(), popMode() functionality into Antlr 3.5

262 views
Skip to first unread message

KISHORE KUSHWAHA

unread,
Jul 29, 2016, 6:46:43 AM7/29/16
to antlr-discussion

On searching the PHP lexer grammar for Antlr 3.5, got the grammar for Antlr 4 from link (which is not compatible with Antlr 3.5). 
So trying to make compatible for Antlr 3.5. The only issue of these mode commands e.g.

PHP_Start
    : ('<?' | '<?php' | '<?PHP') -> pushMode(PHP)
    ;
mode PHP;

PHP_END         
    :   '?>' -> popMode ;

So, My point is how to convert these pushMode(), popMode() functionality into equivalent Antlr 3.5??
Thanks. 
PHPLexer.g

Mike Lischke

unread,
Jul 29, 2016, 8:25:48 AM7/29/16
to antlr-di...@googlegroups.com
Well, you cannot fully convert that, but you can try to handle modes yourself. For instance you could have a stack somewhere that keeps track of the active mode. Then combine the rules which are separated by mode in the ANTLR4 grammar and use predicates which check your mode stack to guide the lexer/parser.


KISHORE KUSHWAHA

unread,
Jul 29, 2016, 8:58:55 AM7/29/16
to antlr-di...@googlegroups.com
Thx Mike for a quick reply.
I am little bit confuse in ANTLR 4 grammar, i.e. upto how many mode commands execute/match or take into consideration..like

OPEN
    : '<' -> pushMode(INSIDE)
    ;

mode INSIDE;

HTML_CLOSE           
    : '>' -> popMode
    ;

HTML_SLASH_CLOSE     
    : '/>' -> popMode
    ;

HTML_SLASH      
    : '/' 
    ;

HTML_EQUALS     
    : '=' 
    ;

HTML_QUOTED_STRING     
    : '"' ~[<"]* '"'
    | '\'' ~[<']* '\''
    ;

//HTML_UNQUOTED_STRING
//    : [a..zA..Z] 
//    ;

HTML_HEX
    : '#' HEXDIGIT+
    ;
    
HTML_Name       
    :
    NameStartChar NameChar* 
    ;

i.e. On executing the pushMode() under "OPEN" rule, how many next rules would be matched against INSIDE mode e.g. HTML_CLOSE, HTML_SLASH_CLOSE, HTML_SLASH etc. i.e. what all possible rules would be matched, what are all eligible rules for INSIDE mode i.e. upto which rule??

Mike Lischke

unread,
Jul 29, 2016, 10:14:02 AM7/29/16
to antlr-di...@googlegroups.com

> I am little bit confuse in ANTLR 4 grammar, i.e. upto how many mode commands execute/match or take into consideration..like

Modes are like switches in the grammar. Once a 'mode` command appears every following rule is only considered in this mode - until another `mode` command appears.

Mike
--
www.soft-gems.net

KISHORE KUSHWAHA

unread,
Jul 29, 2016, 10:22:35 AM7/29/16
to antlr-discussion
Ooh..that's good.

Could you suggest something about how the stack approach can be implemented in grammar rule in mode switching!!!
Reply all
Reply to author
Forward
0 new messages