[antlr-interest] Can this non-determinism be removed?

Jeff Vincent JVincent at Novell.Com
Mon Oct 27 13:42:23 PST 2003


I'm not sure I follow completely.  By reflection, do you mean calling
getType() on the token?  I am just using the default object types so
true Java "reflection" doesn't appear to help me in this case.
 
When the parser does some lookahead, it needs to determine the type of
"identifier" (class declaration type or class member reference) in order
to be able to choose whether or not to follow the declaration or
statement sub-rules.  The only way I know how to distinguish between the
two identifier types is by its context (what follows it).  I have a
declaration sub-rule called "variableDeclList" that follows a class
declaration type.  I am tried putting the follwing predicate:
 
 start! :
  (   ((identifier variableDeclList )=> d:declaration
   |   s:statement
   )*
  )
  ;

but this will only descend the declaration sub-rule if it is a class
declaration and try to follow the statement sub-rule if it encounters
any of the other declaration tokens like BOOL or STRING and fail to
match anything.  If I do:
 
 start! :
  (   ( (BOOL | CHAR | BYTE | SHORT | INT | LONG | FLOAT | DOUBLE |
STRING | STRUCT | VECTOR | identifier variableDeclList )=> declaration
   |   s:statement
   )*
  )
  ;

it seems to work but looks messy and requires changes in two places if
additional language types are added.  Is this the best/easiest way?
 
Thanks,
 
Jeff
 
>>> dan.stanger at ieee.org 10/27/2003 12:44:50 PM >>>

I did a project like this.  One thing that you can do is
to do the reflection on the class and determine what
kind of object it is.  Then use a semantic predicate.

Jeff Vincent wrote:

>  Hey, I am building a run-time scripting language that can
instantiate
> Java classes and call methods on the objects (using reflection API).
> Today, when a class type is specified, it has to be preceeded with
the
> keyword "class", like: class java.lang.String s; However, I want to
> change it so that the "class" keyword is not required, and looks
more
> like real java: java.lang.String s; The problem is that the class
name
> has the same format as class member reference identifier and matches
> the rule.  identifier :
>      IDENTIFIER^ ( arrayNotation )? ( DOT identifier )?
>   ;So the parser is nondeterministic at my start rule because
> "java.lang.String" could either be part of a class instance
> declaration or a class member reference statement.  My parser's
start
> rule is:  start :
>   (   ( d:declaration
>      |   s:statement
>      )*
>   )
>   ; Can predicates be used to remove this?  I tried adding one at
the
> start rule, but my attempts failed.  I can provide more information
on
> other rules in my parser, but I'm not sure what additional
information
> is needed, if indeed the ambiguity can be removed.   It may be
> possible to move the class declaration out of the declaration
sub-rule
> and make it part of the start rule, but I am hoping to keep all the
> declaration rules together for clarity and readability. Thanks, Jeff
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.




Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/ 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20031027/dfb8151d/attachment.html


More information about the antlr-interest mailing list