Copyright © 1998, Compiler Resources, Inc.
Dummy Token Example

Yacc++ and the Language Objects Library

Mimicking Dummy Tokens -- An Example

We assume you are already familiar with Tutorial KEYWORD in the Tutorial Guide.

Grammar

//////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 1998, Compiler Resources, Inc.
//
//  FILENAME:             dummy.yxx
//
//  FILE DESCRIPTION:     example for dummy tokens
//
//  MODIFICATION HISTORY:
//
//    10/08/98  bz        modify keyword example to provide a dummy token 
//                        example for returning specialized token types.
//
//////////////////////////////////////////////////////////////////////////////

class dummy;

lexer

keyword        char  float  int

               // one way to get dummy tokens in the lexer class is to make 
               // keywords that have spellings one will never actually lex.

               char_identifier  "`"
               float_identifier "``"
               int_identifier   "```"
               ;

token          identifier;
discard token  white_space;

identifier   :  ("a" .. "z" | "A" .. "Z")+ ;
    { 
      //  given spelling and length, yy_symtab_lookup_kw()
      //  returns the symbol from the hash table

      yy_lex_rslt().as_sym_ptr = yy_symtab_lookup_kw(yy_lex_token(),
          yy_lex_len(), identifier_);
      yy_lex_rdc() = yy_lex_rslt().as_sym_ptr -> yy_sym_tkn_type();

      //  if not a keyword, it is an identifier.  What type of identifier to return 
      //  depends on how it was declared.  The type information was stored in the 
      //  semantic type member in the symbol at the time of the declaration.

      if ( identifier_ == yy_lex_rdc() ) {
          if (yy_sym_dflt_obj::yy_sym_type_init_ != yy_lex_rslt().as_sym_ptr -> yy_sym_type()) {
              yy_lex_rdc() = yy_lex_rslt().as_sym_ptr -> yy_sym_type();
              }
          }
    }

white_space  :  (' ' | '\t' 
                | '\n'  { ++yy_buf_lineno();  }
                 )+
             ;

parser

global {
#include "iostream.h"
#include "yy_sym.h"
#include "yy_mylex.h"
}

union {
    yy_sym_ptr  as_sym_ptr;
}

grammar      :  (declaration | expression)+
             ;

declaration  :  char identifier   

{ 
  yy_psr_ref( yy_psr_last()).as_sym_ptr->yy_sym_type(yy_lex_dummy_obj::char_identifier_);
}

             |  float identifier
{ 
  yy_psr_ref( yy_psr_last()).as_sym_ptr->yy_sym_type(yy_lex_dummy_obj::float_identifier_);
}

             |  int identifier
{ 
  yy_psr_ref( yy_psr_last()).as_sym_ptr->yy_sym_type(yy_lex_dummy_obj::int_identifier_);
}
             ;

//
//  Expressions of different types will get a syntax error
//

expression   :  int_identifier "+" int_identifier
                { cout << " Integer Addition " << endl; }

             |  float_identifier "+" float_identifier
                { cout << " Float Addition " << endl; }

             |  char_identifier "+" char_identifier
                { cout << " Char Addition " << endl; }

             ;

Test Case

char stringA
char stringB
int  intA
int  intB
float  floatA
float  floatB
intA + intB
stringA + stringB
floatA + floatB
floatA + stringB

Test Output

dummy dummy.tst
 Integer Addition 
 Char Addition 
 Float Addition 
"dummy.txt", line 10: error 100: SYNTAX ERROR while processing "stringB".

Expected... float_identifier.

1 Errors, 0 Warnings, Highest Severity 2


Last updated on October 8, 1998. To send email to Compiler Resources, Inc.

Return to Yacc++ Home Page