[antlr-interest] parsing just a subset of a grammar

Alexander Kostikov alex.kostikov at gmail.com
Tue Nov 20 09:42:49 PST 2012


Kevin,

That should be EOL. I've simplified the grammar a bit before the
sending and a typo sneaked in.

-- Alexander

On Mon, Nov 19, 2012 at 6:27 PM, Kevin J. Cummings
<cummings at kjchome.homeip.net> wrote:
> On 11/19/2012 02:23 PM, Alexander Kostikov wrote:
>> Hi,
>>
>> I'm new to ANTLR and I seek for a good advice.
>
>> To ignore all not interesting parts of the config file I defined the
>> grammar this way:
>>
>> /*
>>  * Parser Rules
>>  */
>>
>> config: (acl | any)* EOF;
>> any: (ID|INT)* EOL;
>> acl: 'ip' 'access-list' 'extended'? ID EOL (remark | rule)+ EOF;
>
> Did you really want an EOF at the end of your acl rule?
> Or should that have been an EOL?
>
>> remark: (index)? 'remark' (~EOL)* EOL;
>> rule: (index)? verb protocol source source_port destination
>> destination_port flag? log? EOL;
>>
>> // Not so interesting parser rules here...
>>
>> /*
>>  * Lexer Rules
>>  */
>>
>> fragment
>> CHAR: 'a'..'z' | 'A'..'Z' | '_' | '-' | '.' | '+' | '/' | ':' | '%';
>> fragment
>> NUMBER: '0'..'9';
>> INT: NUMBER+;
>> ID: CHAR (CHAR | NUMBER)*;
>> EOL: ('\r' | '\n')+;
>> WS: (' ' | '\t') { $channel=HIDDEN; };
>> COMMENT: '!' (~('\r' | '\n'))* EOL { $channel=HIDDEN; };
>> ILLEGAL: .;
>>
>> It turns out ANTLR doesn't behave the way I expected =) What I wanted
>> is for ANTLR to parse the following line "no ip bootp server" via
>> 'any' rule but ANTLR finds 'ip' token in the line and treats the line
>> as not correct 'acl' rule. Specifying syntactic predicate "config:
>> (('ip' 'access-list') => acl | any)* EOF" only makes things worse
>> judging by ANTLRWorks output - parser stops almost immediately with an
>> unrecoverable error.
>>
>> My question is - is there a way to achieve the kind of filtering I'm
>> talking about (parse only 'acl', ignore anything else) via ANTLR
>> grammar? What should I use? Syntactic predicate? Several-pass parsing?
>> Custom lexer (how do I even start implementing such beast?)? Parse out
>> all interesting sections from a file via regex before supplying them
>> to ANTLR grammar that is only ACL-oriented (at least I know how to
>> implement this last option)?
>>
>> -- Alexander
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>
> --
> Kevin J. Cummings
> kjchome at verizon.net
> cummings at kjchome.homeip.net
> cummings at kjc386.framingham.ma.us
> Registered Linux User #1232 (http://www.linuxcounter.net/)
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list