name alias for lexer and parser rules

644 views
Skip to first unread message

Right_Then

unread,
Oct 17, 2013, 4:29:53 AM10/17/13
to antlr-di...@googlegroups.com
Dear Fellows,

i am merely a hobbyist so please dont mind my observations. but this is what i thought.
why cant that rule names be given multiple names to make grammer more readable for the maker.
specially for lexer rules.

for instance

when function names are similar to variable names

then for function signature rule of kind int fun1 ( int arg ) ;
i cannot write 
functionSignature:
TypeName FunName '(' TypeName ArgName ')'  ';' 

i will have to write it 
ID ID '(' ID ID ')';

since all of them have exactly same criteria of match [a-zA-Z_][0-9]
instead if ID could be defined like this

ID @alias{
 TypeName,  FunName, ArgName
}
: ([a-zA-Z_][0-9])+ ;

now antlr can see that i mean the same thing by different Names.

if such a thing already exist i apologise  may i be pointed to that.

Thanks
Regards

Mike Lischke

unread,
Oct 17, 2013, 5:49:17 AM10/17/13
to antlr-di...@googlegroups.com


when function names are similar to variable names

then for function signature rule of kind int fun1 ( int arg ) ;
i cannot write 
functionSignature:
TypeName FunName '(' TypeName ArgName ')'  ';' 

i will have to write it 
ID ID '(' ID ID ')';

since all of them have exactly same criteria of match [a-zA-Z_][0-9]
instead if ID could be defined like this

ID @alias{
 TypeName,  FunName, ArgName
}
: ([a-zA-Z_][0-9])+ ;

Rules like the function signature are parser rules, so it's easy to write several parser rules which match the same lexer token(s):

functionSignature:
typeName funName '(' typeName argName ')' ';'
;

typeName:
ID
;

funName:
ID
;

etc.

Note: better you define your string literals as explicit lexer rules to have ANTLR assign them speaking names, . e.g.

SEMICOLON: ';';

Right_Then

unread,
Oct 17, 2013, 7:23:01 AM10/17/13
to antlr-di...@googlegroups.com
Thanks Mike Lischke

Above example was just a snippet and not actual grammar.
I am certainly defining all the symbols in Lexer rules explicitly.

although i am not at all experienced in parsing or otherwise as such.

what you suggested is what i was doing earlier in my little grammar.
but i was facing lot of problems because first rule encountered by antlr4 was accepted and it wont go to other rules for checking. 
maybe it was because i was doing the same with lexer rules as well. i will try it again.


Thanks
Regards

Mike Lischke

unread,
Oct 17, 2013, 9:07:34 AM10/17/13
to antlr-di...@googlegroups.com

what you suggested is what i was doing earlier in my little grammar.
but i was facing lot of problems because first rule encountered by antlr4 was accepted and it wont go to other rules for checking. 
maybe it was because i was doing the same with lexer rules as well. i will try it again.

Well, that's true. The first match wins, I suggested only to make your parser rule better readable. But consider this: a parser rule is not to represent the logical structure of your language only, but it must match an input token. So, differentating only by name makes probably no sense, except for documentation purposes.


Reply all
Reply to author
Forward
0 new messages