[antlr-interest] ANTLR Grammar error - Illegal token.

rkevinburton at charter.net rkevinburton at charter.net
Mon Aug 4 09:52:33 PDT 2008


If I left factor the grammar I essentially have

classSourceElement
	: classField
	| classMethod
	| classProperty
	;

classCommon
	:  (s=STATIC)? (modifier=(PUBLIC|PRIVATE))?
	;
		 
classField
	: c=classCommon VAR name=Identifier
	-> ^( CFIELD $c $name )
	| c=classCommon VAR name=Identifier COLON type=Identifier
	-> ^( CFIELD $c $name TYPES $type)
	;

classMethod
	: c=classCommon name=Identifier formalParameterList functionBody
	-> ^( $c formalParameterList functionBody )
	;

But I still have the same errors. I am missing something.

Kevin

---- Johannes Luber <jaluber at gmx.de> wrote: 
> rkevinburton at charter.net schrieb:
> > Thank you that gives me much more to go on. I think now I am looking at a look-ahead problem. I am getting the following error(s):
> > 
> > [11:17:59] Checking Grammar...
> > [11:17:59] warning(200): ECMAScript.g:1537:14: Decision can match input such as "{FUNCTION, VAR, PRIVATE, PUBLIC, STATIC, RBRACE}" using multiple alternatives: 1, 2
> > As a result, alternative(s) 2 were disabled for that input
> > [11:17:59] error(201): ECMAScript.g:1537:14: The following alternatives can never be matched: 2
> > 
> > [11:17:59] warning(200): ECMAScript.g:1542:2: Decision can match input such as "STATIC" using multiple alternatives: 1, 2, 3
> > As a result, alternative(s) 2,3 were disabled for that input
> > [11:17:59] warning(200): ECMAScript.g:1542:2: Decision can match input such as "VAR" using multiple alternatives: 1, 3
> > As a result, alternative(s) 3 were disabled for that input
> > [11:17:59] warning(200): ECMAScript.g:1542:2: Decision can match input such as "FUNCTION" using multiple alternatives: 2, 3
> > As a result, alternative(s) 3 were disabled for that input
> > [11:17:59] warning(200): ECMAScript.g:1542:2: Decision can match input such as "PRIVATE" using multiple alternatives: 1, 2, 3
> > As a result, alternative(s) 2,3 were disabled for that input
> > [11:17:59] warning(200): ECMAScript.g:1542:2: Decision can match input such as "PUBLIC" using multiple alternatives: 1, 2, 3
> > As a result, alternative(s) 2,3 were disabled for that input
> > 
> > Feel free to correct me if I am wrong but I think it is complaining that it doesn't know the difference (which path to take) between a static field and a static method. I think this can be solved by allowing look ahead. Can I specify look ahead for just this rule? If so how?
> > 
> > Thanks again.
> > 
> > Kevin
> 
> Try to left-factor the grammar first. Read my tutorial on that here: 
> <http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar>.
> 
> Johannes
> > ---- "John B. Brodie" <jbb at acm.org> wrote: 
> >> Greetings!
> >>
> >> rkevinburton asked:
> >>> I am checking my grammar and getting the following errors:
> >>>
> >>> [09:11:21] error(100): ECMAScript.g:1530:2: syntax error: antlr: ECMAScript.g:1530:2: unexpected token: :
> >>> [09:11:21] error(100): ECMAScript.g:1548:2: syntax error: antlr: ECMAScript.g:1548:2: unexpected token: :
> >>> [09:11:21] error(100): ECMAScript.g:1555:5: syntax error: antlr: ECMAScript.g:1555:5: unexpected token: scope
> >>>
> >>> When I look at the lines indicated I don't see a problem. Moreover I
> >>> don't see a reference to the token that is "unexpected". Would
> >>> someone with more experience with ANTLR than me please look at the
> >>> attached grammar and see if you can see what is wrong?
> >>>
> >>> ...attachment snipped...
> >> I have not actually tried processing your grammar with ANTLR.
> >>
> >> But I see that at those three lines referenced in the above error
> >> messages that you are using the phrase `(scope=STATIC)?` at the
> >> beginning of those three rules.
> >>
> >> So I believe your local variable reference of `scope` clashes with
> >> ANTLR's notion of a dynamic scope, a mechanism for passing information
> >> amongst rules (see chapt 6 of Dr. Parr's book, TDAR), which uses
> >> `scope` as a reserved word to introduce a block of variable
> >> declarations at the beginning of a rule (must appear before the rule's
> >> `:`).
> >>
> >> Try picking another name for your local variable.
> >>
> >> Hope this helps...
> >>    -jbb
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ECMAScript.g
Type: application/octet-stream
Size: 33755 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080804/8d884da3/attachment-0001.obj 


More information about the antlr-interest mailing list