%{ #include #include #include "y.tab.h" extern void yyerror(char*); extern void print_debug(FILE*, char*); extern void print_warning(FILE*, char*); extern YYSTYPE yylval; unsigned int line_count=0; unsigned int get_token_by_identifier(char*); %} COMMENT ("/*"([^"*"]|[\r\n]|("*"+([^"*/"]|[\r\n])))*\*+\/)|("//".*)|(#.*) NEWLINE ([\r?\n]) WHITESPACE ([\t\f ]+) IDENTIFIER ([A-Za-z_][A-Za-z0-9_]*) INT_CONST ({HEX_CONST}|{BIN_CONST}|{OCT_CONST}|{DEC_CONST}) HEX_CONST (0(x|X)[0-9a-fA-F]+) BIN_CONST (0(b|B)[01]+) OCT_CONST (0[0-7]+) DEC_CONST ([0-9][0-9]*) REAL_CONST ([0-9]*\.[0-9]+([eE][-+]?[0-9]+)?) CHAR_CONST ('[\40-\176]') STRING_CONST (\"[\40-\176]*\") UUID (([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}) %% {COMMENT} { if(strncmp(yytext,"//",2)){ yylval.text = yytext; }else{ yylval.text = yytext; } //printf("COMMENT: %s\n",yylval.text); //return tCOMMENT; } {UUID} { yylval.text = yytext; printf("UUID: %s\n",yylval.text); return tUUIDCONST; } {INT_CONST} { yylval.text = yytext; printf("INT_CONST: %s\n",yylval.text); return tINTCONST; } {REAL_CONST} { yylval.text = yytext; printf("REAL_CONST: %s\n",yylval.text); return tREALCONST; } {CHAR_CONST} { yylval.text = yytext; printf("CHAR_CONST: %s\n",yylval.text); return tCHARCONST; } {STRING_CONST} { yylval.text = yytext; printf("STRING_CONST: %s\n",yylval.text); return tSTRINGCONST; } {IDENTIFIER} { yylval.text = yytext; printf("IDENTIFIER: %s\n",yylval.text); return get_token_by_identifier(yytext); } ["\[\](){}%/*+\-;,=&|\^<>:"] { printf("OP: %c\n", *yytext); return *yytext; } {NEWLINE} { line_count++; } {WHITESPACE} ; . { char msg[25]; sprintf(msg,"%s <%s> -- line %d","invalid character",yytext, line_count); yyerror(msg); exit(1); } %% typedef struct { const char *identifier; unsigned int token; } RESERVED_WORD; static const RESERVED_WORD reserved_words[] = { /*Original IDL reserved words*/ {"FALSE", tFALSE}, {"NULL", tNULL}, {"TRUE", tTRUE}, {"boolean", tBOOLEAN}, {"byte", tBYTE}, {"case", tCASE}, {"char", tCHAR}, {"const", tCONST}, {"default", tDEFAULT}, {"double", tDOUBLE}, {"enum", tENUM}, {"error_status_s", tERRORSTATUST}, {"float", tFLOAT}, {"handle_t", tHANDLET}, {"hyper", tHYPER}, {"import", tIMPORT}, {"int", tINT}, {"interface", tINTERFACE}, {"long", tLONG}, {"short", tSHORT}, {"signed", tSIGNED}, {"small", tSMALL}, {"struct", tSTRUCT}, {"switch", tSWITCH}, {"typedef", tTYPEDEF}, {"union", tUNION}, {"unsigned", tUNSIGNED}, {"void", tVOID}, /*MICROSOFT IDL (MIDL) extensions to reserved words*/ {"ms_union", tMSUNION}, {"wchar_t", tWCHART}, #ifdef TRANSFER_SYNTAX_NDR32 {"__int3264", tLONG}, #else {"__int3264", tHYPER}, #endif {"__int8", tSMALL}, {"__int16", tSHORT}, {"__int32", tLONG}, {"__int64", tHYPER}, {"sizeof", tSIZEOF}, {NULL, -1} }; static const RESERVED_WORD reserved_words_attr_ctx[] = { /*Original IDL reserved words in attribute context*/ {"broadcast", tBROADCAST}, {"case", tCASE}, {"context_handle", tCONTEXTHANDLE}, {"endpoint", tENDPOINT}, {"exceptions", tEXCEPTIONS}, {"first_is", tFIRSTIS}, {"handle", tHANDLE}, {"idempotent", tIDEMPOTENT}, {"ignore", tIGNORE}, {"in", tIN}, {"last_is", tLASTIS}, {"length_is", tLENGTHIS}, {"local", tLOCAL}, {"maybe", tMAYBE}, {"max_is", tMAXIS}, {"min_is", tMINIS}, {"out", tOUT}, {"pointer_default", tPOINTERDEFAULT}, {"ptr", tPTR}, {"ref", tREF}, {"reflect_deletions", tREFLECTDELETIONS}, {"size_is", tSIZEIS}, {"string", tSTRING}, {"switch_is", tSWITCHIS}, {"switch_type", tSWITCHTYPE}, {"transmit_as", tTRANSMITAS}, {"unique", tUNIQUE}, {"uuid", tUUID}, {"version", tVERSION}, {"wire_marshal", tWIREMARSHAL}, /*MICROSOFT IDL (MIDL) extensions to reserved words in attribute context*/ {"helpstring", tHELPSTRING}, {"range", tRANGE}, {NULL, -1} }; unsigned int get_token_by_identifier(char* identifier){ int i = 0; while(1){ if(reserved_words[i].identifier == NULL) { break; } if(!strcmp(reserved_words[i].identifier, identifier)){ return reserved_words[i].token; } i++; } i = 0; while(1){ if(reserved_words_attr_ctx[i].identifier == NULL) { break; } if(!strcmp(reserved_words_attr_ctx[i].identifier, identifier)){ return reserved_words_attr_ctx[i].token; } i++; } /*If not a reserved word of any kind then it's a new identifier*/ return tIDENTIFIER; }