Matching empty strings in parser

385 views
Skip to first unread message

vignesh kailas

unread,
Mar 3, 2016, 4:05:18 AM3/3/16
to antlr-discussion
I have a grammar like this in antlr ver4:

csrc_includes :
   (header)*
    ;

header : HASH INCLUDE_LITERAL ANGULAR_OPEN FILE_NAME ANGULAR_CLOSE

The above grammar is throwing an error stating that the csrc_includes can evaluate to a null expression as well. Now, I do understand the implication of this grammar and the meaning of the error. But is there a way that will help me define a grammar like this (implies that there can be no header includes inside a c src file).

Big thanks in advance :)

Mike Lischke

unread,
Mar 3, 2016, 4:34:36 AM3/3/16
to antlr-di...@googlegroups.com
Make this rule optional in the calling context and use this loop for it:

csrc_includes:
header+
;

Mike
--
www.soft-gems.net

Kevin Cummings

unread,
Mar 3, 2016, 9:12:48 AM3/3/16
to antlr-di...@googlegroups.com
On 03/03/16 04:05, vignesh kailas wrote:
> I have a grammar like this in antlr ver4:
>
> csrc_includes :
> (header)*
> ;
>
> header : HASH INCLUDE_LITERAL ANGULAR_OPEN FILE_NAME ANGULAR_CLOSE

First of all., csrc_includes and header are parser rules. Shouldn't
they instead be lexer rules?

Second, instead of using csrc_includes in your grammar, could you
instead use:

CSRC_INCLUDES : (HEADER)+
;

Then, instead of referencing CSRC_INCLUDES in your grammar, you could
replace it with references to (CSRC_INCLUDES)? Make their use
optional, but if used require one or more includes to be present.

> The above grammar is throwing an error stating that the csrc_includes
> can evaluate to a null expression as well. Now, I do understand the
> implication of this grammar and the meaning of the error. But is there a
> way that will help me define a grammar like this (implies that there can
> be no header includes inside a c src file).
>
> Big thanks in advance :)
>
> --
> You received this message because you are subscribed to the Google
> Groups "antlr-discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to antlr-discussi...@googlegroups.com
> <mailto:antlr-discussi...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Kevin J. Cummings
kjc...@verizon.net
cumm...@kjchome.homeip.net
cumm...@kjc386.framingham.ma.us
Registered Linux User #1232 (http://www.linuxcounter.net/)

vignesh kailas

unread,
Mar 4, 2016, 2:29:06 AM3/4/16
to antlr-discussion
Thanks guys :)
Reply all
Reply to author
Forward
0 new messages