/* ************************************************
 *                                               *
 *  Module: tokenvalue.h                         *
 *  Description:                                 *
 *      Defines the Tokenvalue union.            *
 *  Author: Van Oostenrijk, A.C.                 *
 *  Modifications:                               *
 *                                               *
 *************************************************
 *                                               *
 *   This program is free software; you can      *
 *   redistribute it and/or modify  it under     *
 *   the terms of the GNU General Public         *
 *   License as published by the Free            *
 *   Software Foundation; either version 2       *
 *   of the License, or (at your option) any     *
 *   later version.                              *
 *                                               *
 ************************************************
*/

#ifndef TOKENVALUE_H
#define  TOKENVALUE_H

#include 
" defs.h "

// 使用一个union来处理各种数据
typedef union
{
    unsigned 
long   uintvalue;
    BOOL    boolvalue;
    
char     * stringvalue;
    
char     charvalue;
    
float    floatvalue;
    
char     * identifier;
} Tokenvalue;

// 将Tokenvalue转化为字符串
char   * TokenvalueToString(  int  node, Tokenvalue tokenvalue );

#endif

 

TokenvalueToString具体实现:

/* ************************************************
 *                                               *
 *  Module: tokenvalue.c                         *
 *  Description:                                 *
 *      Converts token values to strings.        *
 *  Author: Van Oostenrijk, A.C.                 *
 *  Modifications:                               *
 *      JWH: Added NODE_CASE to the              *
 *      TokenvalueToString function              *
 *                                               *
 *************************************************
 *                                               *
 *   This program is free software; you can      *
 *   redistribute it and/or modify  it under     *
 *   the terms of the GNU General Public         *
 *   License as published by the Free            *
 *   Software Foundation; either version 2       *
 *   of the License, or (at your option) any     *
 *   later version.                              *
 *                                               *
 ************************************************
*/

#include 
" tokenvalue.h "
#include 
" nodenames.h "

char   * TokenvalueToString(  int  node, Tokenvalue tokenvalue )
{
    
static   char  value[ 100 ];

    
switch ( node )
    {
    
case  NODE_LIT_INT:
    
case  NODE_CASE:
        sprintf( value, 
" %ld " , tokenvalue.uintvalue );
        
break ;
    
case  NODE_LIT_BOOL:
        sprintf( value, 
" %s " , tokenvalue.boolvalue  ==  TRUE  ?   " true "  :  " false "  );
        
break ;
    
case  NODE_LIT_CHAR:
        sprintf( value, 
" '%c' " , tokenvalue.charvalue );
        
break
    
case  NODE_LIT_STRING:
        sprintf( value, 
" \ " % s\ "" , tokenvalue.stringvalue );
        
break ;
    
case  NODE_LIT_FLOAT:
        sprintf( value, 
" %f " , tokenvalue.floatvalue );
        
break ;
    
case  NODE_LIT_IDENTIFIER:
        sprintf( value, 
" %s " , tokenvalue.identifier );
        
break ;
    
case  NODE_REFERENCE:
        sprintf( value, 
" %ld " , tokenvalue.uintvalue );
        
break ;
    
case  NODE_DIMENSIONBLOCK:
        sprintf( value, 
" %ld " , tokenvalue.uintvalue );
        
break ;
    
case  NODE_INDEX:
        sprintf( value, 
" %ld " , tokenvalue.uintvalue );
        
break ;
    
default :
        sprintf( value, 
"   "  );
    }

    
return ( value );
}




inger支持的所有关键字和操作符:

/*************************************************
 *                                               *
 *  Module: tokens.h                             *
 *  Description:                                 *
 *      Contains token definitions.              *
 *  Author: Van Oostenrijk, A.C.                 *
 *  Modifications:                               *
 *  AO: Rearranged keywords in alphabetic        *
 *      fashion.                                 *
 *  AO: Keyword 'then' removed.                  *
 *  AO: Renamed QUESTIONMARK to OP_TERNARY_IF.   *
 *  AO: Renamed PARENTHESIS_LEFT to LPAREN.      *
 *  AO: Renamed PARENTHESIS_RIGHT to RPAREN.     *
 *  AO: Renamed BRACKET_LEFT to LBRACKET.        *
 *  AO: Renamed BRACKET_RIGHT to RBRACKET.       *
 *  AO: Renamed BLOCK_START to LBRACE.           *
 *  AO: Renamed BLOCK_END to RBRACE.             *
 *  AO: Keyword 'extern' added.                  *
 *  JWH: Renamed "parser.h" to "tokens.h"        *
 *                                               *
 *************************************************
 *                                               *
 *   This program is free software; you can      *
 *   redistribute it and/or modify  it under     *
 *   the terms of the GNU General Public         *
 *   License as published by the Free            *
 *   Software Foundation; either version 2       *
 *   of the License, or (at your option) any     *
 *   later version.                              *
 *                                               *
 ************************************************
*/

/*!TOKENSH*/
#ifndef TOKENS_H
#define TOKENS_H

#include 
"defs.h"
/* #include "type.h" */
#include 
"tokenvalue.h"
#include 
"ast.h"

/*
 *
 *  MACROS 
 *
 
*/

/* Define where a line starts (at position 1)
 
*/
#define LINECOUNTBASE 1
/* Define the position of a first character of a line.
 
*/
#define CHARPOSBASE 1
/* Define the block size with which strings are allocated.
 
*/
#define STRING_BLOCK 100 

/*
 *
 *  TYPES
 *
 
*/

/* This enum contains all the keywords and operators
 * used in the language.
 
*/
enum
{
    
/* Keywords */
    KW_BREAK            
= 1000/* "break" keyword */
    KW_CASE,                    
/* "case" keyword */
    KW_CONTINUE,                
/* "continue" keyword */
    KW_DEFAULT,                 
/* "default" keyword */
    KW_DO,                      
/* "do" keyword */
    KW_ELSE,                    
/* "else" keyword */
    KW_EXTERN,                  
/* "extern" keyword */
    KW_GOTO,                    
/* "goto" keyword */
    KW_IF,                      
/* "if" keyword */
    KW_LABEL,                   
/* "label" keyword */
    KW_MODULE,                  
/* "module" keyword */
    KW_RETURN,                  
/* "return"keyword */
    KW_START,                   
/* "start" keyword */
    KW_SWITCH,                  
/* "switch" keyword */
    KW_WHILE,                   
/* "while" keyword */

    
/* Type identifiers */
    KW_BOOL,                    
/* "bool" identifier */
    KW_CHAR,                    
/* "char" identifier */
    KW_FLOAT,                   
/* "float" identifier */
    KW_INT,                     
/* "int" identifier */
    KW_UNTYPED,                 
/* "untyped" identifier */
    KW_VOID,                    
/* "void" identifier */

    
/* Variable lexer tokens */
    LIT_BOOL,                   
/* bool constant */
    LIT_CHAR,                   
/* character constant */
    LIT_FLOAT,                  
/* floating point constant */
    LIT_INT,                    
/* integer constant */
    LIT_STRING,                 
/* string constant */
    IDENTIFIER,                 
/* identifier */

    
/* Operators */
    OP_ADD,                     
/* "+"  */
    OP_ASSIGN,                  
/* "="  */
    OP_BITWISE_AND,             
/* "&" */
    OP_BITWISE_COMPLEMENT,      
/* "~"  */
    OP_BITWISE_LSHIFT,          
/* "<<" */
    OP_BITWISE_OR,              
/* "|" */
    OP_BITWISE_RSHIFT,          
/* ">>" */
    OP_BITWISE_XOR,             
/* "^"  */
    OP_DIVIDE,                  
/* "/"  */
    OP_EQUAL,                   
/* "==" */
    OP_GREATER,                 
/* ">"  */
    OP_GREATEREQUAL,            
/* ">=" */
    OP_LESS,                    
/* "<"  */
    OP_LESSEQUAL,               
/* "<=" */
    OP_LOGICAL_AND,             
/* "&&" */
    OP_LOGICAL_OR,              
/* "||" */
    OP_MODULUS,                 
/* "%"  */
    OP_MULTIPLY,                
/* "*"  */
    OP_NOT,                     
/* "!"  */
    OP_NOTEQUAL,                
/* "!=" */
    OP_SUBTRACT,                
/* "-"  */
    OP_TERNARY_IF,              
/* "?"  */
   
    
/* Delimiters */
    ARROW,                      
/* "->" */
    LBRACE,                     
/* "{"  */
    RBRACE,                     
/* "}"  */
    LBRACKET,                   
/* "["  */
    RBRACKET,                   
/* "]"  */
    COLON,                      
/* ":"  */
    COMMA,                      
/* ","  */
    LPAREN,                     
/* "("  */
    RPAREN,                     
/* ")"  */
    SEMICOLON                   
/* ";"  */
}
tokens;
 
/*
 *
 *  FUNCTION DECLARATIONS
 *
 
*/

TreeNode 
*Parse();

/*
 *
 *  GLOBALS
 *
 
*/

extern Tokenvalue tokenvalue;

#endif
/*!*/