ambiguities exist in the lexer

2 views
Skip to first unread message

Ram

unread,
Aug 10, 2005, 1:50:10 AM8/10/05
to pyggy
How does the lexer handle ambiguities?
For example can I do this:

INITIAL:

"FOR" : return 0
"WHILE" : return 1
"[A-Z]+" : return 3

and have that work?

My hope is that rules specified earlier in the file
will take precedence over those specified later. Maybe
this is in the manual, but I looked and could not find
it.

Thanks for any info.
-Ram

David Vallejo Fernández

unread,
Aug 10, 2005, 5:07:20 AM8/10/05
to py...@googlegroups.com
Rules specified earlier in the file are precedence over other rules
specified later. However, the lexer will inform you about the
ambiguities which take place.

Take care.

Ram

unread,
Aug 10, 2005, 10:55:22 AM8/10/05
to pyggy
Thank you. This is exactly what I was looking for.

Perhaps a different message could be used. Having the
message explain the precedence rules may help to reduce
confusion later on.

Also it would be better if common useage cases did not
elicit such messages. I suspect that the style of my first
example is common. And even though there is a conflict
its not one that was accidental or likely to be a user error.

Maybe the message could be printed only when two patterns
with * in them match the same text.

-Ram

Tim Newsham

unread,
Aug 10, 2005, 1:55:36 PM8/10/05
to pyggy
> "FOR" : return 0
> "WHILE" : return 1
> "[A-Z]+" : return 3

Rules specified earlier have precedence (as pointed out), with
the overriding rule that longer matches are preferred over shorter
matches. For example:

FORWHILE

will get you token 3, not tokens 0 and 1. This is usually what
one intends.

> -Ram

Tim Newsham
http://www.lava.net/~newsham/
Reply all
Reply to author
Forward
0 new messages