why i missing declaration but still able to complie?

13 views
Skip to first unread message

Peter Cheung

unread,
Aug 23, 2017, 2:05:08 PM8/23/17
to antlr-discussion
Hi

    why i missing declare PETER but still able to complie?

grammar Assembler;

NL : '\r'? '\n';
ID : [a-zA-Z] [a-zA-Z0-9]+;
LINE_COMMENT : ';' ~[\r\n]*;

assemble : lines
| EOF
;

lines : (PETER line NL+)*
;

line : statement
| statement comment
| comment
;

statement : ID+;

comment : LINE_COMMENT
;

Thanks

Kevin Cummings

unread,
Aug 23, 2017, 5:54:50 PM8/23/17
to antlr-di...@googlegroups.com
On 08/23/17 14:05, Peter Cheung wrote:
> Hi
>
> why i missing declare PETER but still able to complie?
>
> grammar Assembler;
>
> NL:'\r'? '\n';
> ID:[a-zA-Z] [a-zA-Z0-9]+;
> LINE_COMMENT:';' ~[\r\n]*;
>
> assemble:lines
> |EOF
> ;
>
> lines:(PETER line NL+)*

At this point, ANTLR sees a new token (PETER) and creates a token id for
it (look at your token types file). You won't see the error until
runtime when your parser can't find a token with that ID because your
lexer does not does not create one. Always declare all of your named
tokens in lexer rules, lest ANTLR create one for you that you can't
match. I believe this is an artifact of how ANTLR handles unnamed
literal tokens.

> ;
>
> line:statement
> |statement comment
> |comment
> ;
>
> statement:ID+;
>
> comment:LINE_COMMENT
> ;
>
> Thanks
>
> --
> 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
cumm...@kjchome.homeip.net
cumm...@kjc386.framingham.ma.us
kjc...@icloud.com
Registered Linux User #1232 (http://www.linuxcounter.net/)
Reply all
Reply to author
Forward
Message has been deleted
0 new messages