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

Re: AW: How to write SableCC Productions



> > Helpers
> >   cr = 13 ;
> >   lf = 10 ;
> > Tokens
> >   colon = ':' ;
> >   underscore = '_';
> >   letter = ['a'..'z'] + ['A'..'Z'];
> >   endofline = cr | lf | cr lf ;
> >
> > Productions
> >   il_instruction = labeltag? il_instruction_rest? eol;
> >   labeltag = label colon;
> >   label = identifier;
> >   il_instruction_rest =
> >     {il_simple_operation} il_simple_operation |
> >     {il_expression} il_expression;
> >
> >   identifier = letter+;   //not true, test only
> >   eol = endofline+;
> >
> > so far so good, but now I get the error:
> > Verifying identifiers.
> > PIlSimpleOperation and TIlSimpleOperation undefined.
>
> Your need either a production rule, or a token called il_simple_operation.
> You will also need one for il_expression. Also, you will probably find it
> simpler to make identifier  a token rather than a production.
> You may want to ignore blank space too. Generally you can cut and paste
> these rules from existing grammars. I think you want something like:
>
> Helpers
>     cr = 13 ;
>     lf = 10 ;
>     tab = 9 ;
>     nondigit = ['_' + [['a' .. 'z'] + ['A' .. 'Z']]];
>     digit = ['0' .. '9'];
>
> Tokens
>     colon = ':' ;
>     eol = cr | lf | cr lf ;
>     blank = ( tab | ' ' )+;
>
>     il_simple_operation = 'operation' ; // no idea why
>     il_expression = 'expression' ; // just made it up
>
>     // important to have this last, so 'operation' and 'expression'
>     // are not analysed as identifiers
>     identifier = nondigit (digit | nondigit)*;
>
> Ignored Tokens
>     blank;
>
> Productions
>     il_instruction = labeltag? il_instruction_rest? eol;
>     labeltag = label colon;
>     label = identifier;
>     il_instruction_rest =
>       {il_simple_operation} il_simple_operation |
>       {il_expression} il_expression;
>
> Have you downloaded Etienne's thesis (it is at www.sablecc.org)? It
> explains everything very well.


May I also suggest you browse and read example grammars from the
grammars subdirectory:

  http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/sablecc/grammars/

The examples may answer most of your questions.


> > In the meantime I tried to convert the identifier from BNF:
> > identifier ::= (letter | ('_' (letter | digit))) {['_'] (letter | digit)}


;o) As I just prepared yet another simple example I will put it here
even though the one above is already posted.
Note, I believe that according to the BNF notation these would not be
valid identifiers:

__, ___, __m, ___m, etc.

whereas according to the definition

   identifier = nondigit (digit | nondigit)*;

they would be.  The example below does not allow __, ___, __m to be valid
identifiers.  Good luck with your project.


Helpers

  cr = 13 ;
  lf = 10 ;

  underscore = '_';
  colon = ':';

  letter = ['a'..'z'] + ['A'..'Z'];
  digit = ['0'..'9'];

  normal = letter | digit;
  alpha = underscore | letter | digit;



Tokens

  colon = colon ;
  underscore = underscore ;
  endofline = cr | lf | cr lf ;

  expression = 'expression'; // made up, not true
  operation = 'operation'; // made up, not true

  identifier = letter alpha* | underscore normal alpha* ;



Productions

  il_instruction = labeltag? il_instruction_rest? eol;
  labeltag = label colon;

  label = identifier;

  il_instruction_rest =
    {il_simple_operation} il_simple_operation |
    {il_expression} il_expression;

  eol = endofline+;

  il_simple_operation = operation ;  // made up, not true
  il_expression = expression ; // made up, not true