[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Questions



fidel viegas wrote:
> States
> normal,comment;
> Tokens
> {normal->comment, comment} ... ;
> 
> what does it mean the expression between { and }.

It means that the lexer will only recognize this token when it is in the
following states: normal, comment.

Furthermore, it will change its internal state to "comment" if this
token is recognized in state "normal".

All this is explained in chapter 4.
(http://www.sable.mcgill.ca/sablecc/thesis.html#PAGE31)

> and for example in {comment, comment->normal}, does it mean that it is in
> state comment and transites to state normal when recognising the regular
> expression that follows it ?

This should be rejected by SableCC, as it would mean contradictory
things: 
When the token is recognized in the "comment" state, <do not change the
state> AND <change the state to "normal">.  (The token is only
recognized when the lexer is in the "comment" state.)

> The second query is regarding productions, eg:
> in the following production :
> 
> prod = P.Package | ...;
> what effect does the P. has on the alternative ?

The "P." construct is only used to disambiguate references.  If you had:

Tokens
  some_name = ...; // [1]

Productions
  some_name = ...; // [2]
  ... = ... some_name  ...; // [3]

Both [1] and [2] define "some_name".  [1] is a token, and [2] is a
production.  This is allowed by SableCC, so you have separate name
spaces for tokens and productions (this is handy sometimes).

Now, in [3], what does "some_name" refers to?  Does it refer to the
token [1] or the production [2]?  This is ambiguous, so SableCC requires
that you specify whether you refer to a token or a production using "T."
and "P." respectively.

e.g.:
  ... = ... T.some_name ...; // token
  ... = ... P.some_name ...; // production
> and for example in
> Token =
> T.Tokens |...;
> what effect does the T. has ?

Have you tried that?  SableCC should gice you an error.  (SableCC's
grammar doesn't allow this syntax).

> Finally, the last one is regarding the ? operator.
> in the production
> 
> program =
> program_heading? ...;
> 
> I know that it has no effect on AST, but my question is how do we manipulate
> it ?
> What does SableCC generates to allows us to manipulate it ?


...(AProgram prog)  // method definition
{
  ...
  ... = prog.getProgramHeading();
  ...
  prog.setProgramHeading(...);
}
 

This is all written in the thesis.  READ carefully chapters 3 to 6
inclusively!  (not to say RTFM...) 

> Do we have to write any extension of the Node class ?

All node types are "final".  So the answer is obviously: no!  In
summary, the idea is to use a hashtable to pair "analysis data" with
specific AST nodes.  The detailed answer is in the mailing list
archive.  Please search the archive.  (If somebody can find the URL of
the appropriate message, please post it.  I have not time to search for
it.)

Have fun.

Etienne
-- 
----------------------------------------------------------------------
Etienne M. Gagnon, M.Sc.                     e-mail: egagnon@j-meg.com
Author of SableCC:                 http://www.sable.mcgill.ca/sablecc/
----------------------------------------------------------------------