| 1 |
%{ |
| 2 |
|
| 3 |
/* Copyright (C) 1989-1991 James A. Roskind, All rights reserved. |
| 4 |
This lexer description was written by James A. Roskind. Copying |
| 5 |
of this file, as a whole, is permitted providing this notice is |
| 6 |
intact and applicable in all complete copies. Direct |
| 7 |
translations as a whole to other lexer generator input languages |
| 8 |
(or lexical description languages) is permitted provided that |
| 9 |
this notice is intact and applicable in all such copies, along |
| 10 |
with a disclaimer that the contents are a translation. The |
| 11 |
reproduction of derived files or text, such as modified versions |
| 12 |
of this file, or the output of scanner generators, is permitted, |
| 13 |
provided the resulting work includes the copyright notice |
| 14 |
"Portions Copyright (c) 1989, 1990 James A. Roskind". Derived |
| 15 |
products must also provide the notice "Portions Copyright (c) |
| 16 |
1989, 1990 James A. Roskind" in a manner appropriate to the |
| 17 |
utility, and in keeping with copyright law (e.g.: EITHER |
| 18 |
displayed when first invoked/executed; OR displayed continuously |
| 19 |
on display terminal; OR via placement in the object code in form |
| 20 |
readable in a printout, with or near the title of the work, or at |
| 21 |
the end of the file). No royalties, licenses or commissions of |
| 22 |
any kind are required to copy this file, its translations, or |
| 23 |
derivative products, when the copies are made in compliance with |
| 24 |
this notice. Persons or corporations that do make copies in |
| 25 |
compliance with this notice may charge whatever price is |
| 26 |
agreeable to a buyer, for such copies or derivative works. THIS |
| 27 |
FILE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 28 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES |
| 29 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 30 |
|
| 31 |
James A. Roskind |
| 32 |
Independent Consultant |
| 33 |
516 Latania Palm Drive |
| 34 |
Indialantic FL, 32903 |
| 35 |
(407)729-4348 |
| 36 |
[email protected] |
| 37 |
or ...!uunet!leafusa!jar |
| 38 |
|
| 39 |
---end of copyright notice--- |
| 40 |
|
| 41 |
|
| 42 |
COMMENTS- |
| 43 |
|
| 44 |
My goal is to see software developers adopt my C++ grammar as a |
| 45 |
standard until such time as a better standard is accessible. The |
| 46 |
only way to get it to become a standard, is to be sure that people |
| 47 |
know that derivations are based on a specific work. The intent of |
| 48 |
releasing this Flex input file is to facilitate experimentation with |
| 49 |
my C++ grammar. The intent of the copyright notice is to allow |
| 50 |
arbitrary commercial and non-commercial use of this file, as long as |
| 51 |
reference is given to my standardization effort. Without reference |
| 52 |
to a specific standard, many alternative grammars would develop. By |
| 53 |
referring to the standard, the C++ grammar is given publicity, which |
| 54 |
should lead to further use in compatible products and systems. The |
| 55 |
benefits of such a standard to commercial products (browsers, |
| 56 |
beautifiers, translators, compilers, ...) should be obvious to the |
| 57 |
developers, in that other compatible products will emerge, and the |
| 58 |
value of all conforming products will rise. Most developers are |
| 59 |
aware of the value of acquiring a fairly complete grammar for a |
| 60 |
language, and the copyright notice (and the resulting affiliation |
| 61 |
with my work) should not be too high a price to pay. By copyrighting |
| 62 |
my work, I have some minor control over what this standard is, and I |
| 63 |
can (hopefully) keep it from degrading without my approval. I will |
| 64 |
consistently attempt to provide upgraded grammars that are compliant |
| 65 |
with the current art, and the ANSI C++ Committee recommendation in |
| 66 |
particular. A developer is never prevented from modifying the |
| 67 |
grammar or this file to improve it in whatever way is seen fit. There |
| 68 |
is also no restriction on the sale of copies, or derivative works, |
| 69 |
providing the request in the copyright notice are satisfied. |
| 70 |
|
| 71 |
If you are not "copying" my work, but are rather only abstracting |
| 72 |
some of my work, an acknowledgment with references to such a standard |
| 73 |
would be appreciated. Specifically, agreements with my grammar and |
| 74 |
its resolution of otherwise ambiguous constructs, should be noted. |
| 75 |
|
| 76 |
Simply put: "make whatever use you would like of the grammar and this |
| 77 |
file, but include the ``portions Copyright ...'' as a reference to |
| 78 |
this standard." |
| 79 |
|
| 80 |
|
| 81 |
*/ |
| 82 |
|
| 83 |
/* Last modified 7/4/91, Version 2.0 */ |
| 84 |
|
| 85 |
/* File cpp5.l, becomes lex.yy.c after processing by FLEX */ |
| 86 |
|
| 87 |
/* This file is a dramatically cut down version of the FLEX input file |
| 88 |
used in my ANSI C Preprocessor. The executable version of my |
| 89 |
preprocessor is available on many platforms (shareware), but this is |
| 90 |
the only source extract that is currently being distributed. If you |
| 91 |
need a full ANSI C preprocessor, with extensive diagnostic |
| 92 |
capabilities and customization facilities, please contact me at the |
| 93 |
addresses given above. Current platforms include IBMPC (DOS/OS2), Sun |
| 94 |
(SPARC and Motorola), and IBM R/6000. ... end of commercial |
| 95 |
announcement. |
| 96 |
|
| 97 |
This file is being distributed to facilitate experimentation and use |
| 98 |
of my C and C++ grammar. |
| 99 |
|
| 100 |
|
| 101 |
Comment removal must be done during the lexing, as context (such as |
| 102 |
enclosure in string literals) must be observed. For this cut-down |
| 103 |
lexer, we will assume that comments have been removed (don't assume |
| 104 |
this if you are writing a compiler or browser!). |
| 105 |
|
| 106 |
|
| 107 |
/* For each IDENTIFIER like string that is found, there are several |
| 108 |
distinct interpretations that can be applied: |
| 109 |
|
| 110 |
1) The preprocessor may interpret the string as a "keyword" in a |
| 111 |
directive (eg: "pragma" or "include", "defined"). |
| 112 |
|
| 113 |
2) The parser may interpret the string as a keyword. (eg: "int"). |
| 114 |
|
| 115 |
3) Both parser and preprocessor may interpret the string as a keyword |
| 116 |
(eg: "if"). |
| 117 |
|
| 118 |
Since this file is based on source that actually lexically analyses |
| 119 |
text for both preprocessing and parsing, macro definitions were used |
| 120 |
throughout. The macro definitions supplied here have been customized |
| 121 |
to a C++ parse only, and all preprocessor keywords are passed as |
| 122 |
IDENTIFIER or TYPEDEFname. Also, since there is no symbol table to |
| 123 |
interrogate to decide whether a string is a TYPEDEFname, I simply |
| 124 |
assume that any identifier beginning with an upper case letter is a |
| 125 |
TYPEDEFname. This hack should allow you to check out how code |
| 126 |
segments are parsed using my grammar. Unfortunately, if you really |
| 127 |
want to parse major league code, you have to write a symbol table, and |
| 128 |
maintain appropriate scoping information. |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
*/ |
| 133 |
|
| 134 |
|
| 135 |
/* Included code before lex code */ |
| 136 |
/*************** Includes and Defines *****************************/ |
| 137 |
|
| 138 |
|
| 139 |
#include "y.tab.h" /* YACC generated definitions based on C++ grammar */ |
| 140 |
|
| 141 |
typedef char * YYSTYPE; /* interface with lexer: should be in header |
| 142 |
file*/ |
| 143 |
|
| 144 |
extern char * yylval; /* We will always point at the text of the lexeme. |
| 145 |
This makes it easy to print out nice trees when YYDEBUG is |
| 146 |
enabled. (see C++ grammar file and its definition of |
| 147 |
YYDEBUG_LEXER_TEXT to be "yylval" */ |
| 148 |
|
| 149 |
#include <stdlib.h> |
| 150 |
#include <string.h> |
| 151 |
|
| 152 |
/* Prototypes */ |
| 153 |
|
| 154 |
|
| 155 |
#define WHITE_RETURN(x) /* do nothing */ |
| 156 |
|
| 157 |
#define NEW_LINE_RETURN() WHITE_RETURN('\n') |
| 158 |
|
| 159 |
#define PA_KEYWORD_RETURN(x) RETURN_VAL(x) /* standard C PArser Keyword */ |
| 160 |
#define CPP_KEYWORD_RETURN(x) PA_KEYWORD_RETURN(x) /* C++ keyword */ |
| 161 |
#define PPPA_KEYWORD_RETURN(x) RETURN_VAL(x) /* both PreProcessor and PArser keyword */ |
| 162 |
#define PP_KEYWORD_RETURN(x) IDENTIFIER_RETURN() |
| 163 |
|
| 164 |
#define IDENTIFIER_RETURN() RETURN_VAL(IDENTIFIER) |
| 165 |
|
| 166 |
#define PPOP_RETURN(x) RETURN_VAL((int)*yytext) /* PreProcess and Parser operator */ |
| 167 |
#define NAMED_PPOP_RETURN(x) RETURN_VAL(x) |
| 168 |
#define ASCIIOP_RETURN(x) RETURN_VAL((int)*yytext) /* a single character operator */ |
| 169 |
#define NAMEDOP_RETURN(x) RETURN_VAL(x) /* a multichar operator, with a name */ |
| 170 |
|
| 171 |
#define NUMERICAL_RETURN(x) RETURN_VAL(x) /* some sort of constant */ |
| 172 |
#define LITERAL_RETURN(x) RETURN_VAL(x) /* a string literal */ |
| 173 |
#define C_COMMENT_RETURN(x) RETURN_VAL(x) /* C Style comment */ |
| 174 |
#define RETURN_VAL(x) return(x); |
| 175 |
#define yywrap() 1 |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
%} |
| 180 |
|
| 181 |
%option yylineno |
| 182 |
|
| 183 |
identifier [a-zA-Z_][0-9a-zA-Z_]* |
| 184 |
|
| 185 |
exponent_part [eE][-+]?[0-9]+ |
| 186 |
fractional_constant ([0-9]*"."[0-9]+)|([0-9]+".") |
| 187 |
floating_constant (({fractional_constant}{exponent_part}?)|([0-9]+{exponent_part}))[FfLl]? |
| 188 |
|
| 189 |
integer_suffix_opt ([uU]?[lL]?)|([lL][uU]) |
| 190 |
decimal_constant [1-9][0-9]*{integer_suffix_opt} |
| 191 |
octal_constant "0"[0-7]*{integer_suffix_opt} |
| 192 |
hex_constant "0"[xX][0-9a-fA-F]+{integer_suffix_opt} |
| 193 |
|
| 194 |
simple_escape [abfnrtv'"?\\] |
| 195 |
octal_escape [0-7]{1,3} |
| 196 |
hex_escape "x"[0-9a-fA-F]+ |
| 197 |
|
| 198 |
escape_sequence [\\]({simple_escape}|{octal_escape}|{hex_escape}) |
| 199 |
c_char [^'\\\n]|{escape_sequence} |
| 200 |
s_char [^"\\\n]|{escape_sequence} |
| 201 |
|
| 202 |
|
| 203 |
h_tab [\011] |
| 204 |
form_feed [\014] |
| 205 |
v_tab [\013] |
| 206 |
c_return [\015] |
| 207 |
|
| 208 |
horizontal_white [ ]|{h_tab} |
| 209 |
|
| 210 |
|
| 211 |
%% |
| 212 |
|
| 213 |
"/*" { |
| 214 |
/* Handle C-Style comment */ |
| 215 |
register int c; |
| 216 |
if(m_keepComments) m_comment += yytext; |
| 217 |
for ( ; ; ) |
| 218 |
{ |
| 219 |
while ( (c = yyinput()) != '*' && c != EOF ) |
| 220 |
{ |
| 221 |
if(m_keepComments) m_comment += c; |
| 222 |
|
| 223 |
; /* eat up text of comment */ |
| 224 |
} |
| 225 |
if(m_keepComments) m_comment += c; |
| 226 |
if ( c == '*' ) |
| 227 |
{ |
| 228 |
while ( (c = yyinput()) == '*' ) |
| 229 |
{ |
| 230 |
if(m_keepComments) m_comment += c; |
| 231 |
; |
| 232 |
} |
| 233 |
if(m_keepComments) m_comment += c; |
| 234 |
if ( c == '/' ) |
| 235 |
{ |
| 236 |
break; /* found the end */ |
| 237 |
} |
| 238 |
} |
| 239 |
if ( c == EOF ) |
| 240 |
{ |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
if(m_keepComments ) return CComment; |
| 245 |
} |
| 246 |
|
| 247 |
"//" { |
| 248 |
/* Handle CPP style comment */ |
| 249 |
/* Constume everything until the newline */ |
| 250 |
register int c; |
| 251 |
if(m_keepComments) m_comment += yytext; |
| 252 |
while((c = yyinput()) != '\n' && c != EOF) |
| 253 |
{ |
| 254 |
if(m_keepComments) m_comment += c; |
| 255 |
} |
| 256 |
if(m_keepComments) m_comment += c; |
| 257 |
if(m_keepComments ) return CPPComment; |
| 258 |
} |
| 259 |
|
| 260 |
{horizontal_white}+ { |
| 261 |
if(!m_returnWhite) |
| 262 |
WHITE_RETURN(' '); |
| 263 |
else |
| 264 |
return ' '; |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
({v_tab}|{c_return}|{form_feed})+ { |
| 269 |
if(!m_returnWhite) |
| 270 |
WHITE_RETURN(' '); |
| 271 |
else |
| 272 |
return ' '; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
({horizontal_white}|{v_tab}|{c_return}|{form_feed})*"\n" { |
| 277 |
if(!m_returnWhite) |
| 278 |
NEW_LINE_RETURN(); |
| 279 |
else |
| 280 |
return '\n'; |
| 281 |
} |
| 282 |
|
| 283 |
auto {PA_KEYWORD_RETURN(AUTO);} |
| 284 |
break {PA_KEYWORD_RETURN(BREAK);} |
| 285 |
case {PA_KEYWORD_RETURN(CASE);} |
| 286 |
char {PA_KEYWORD_RETURN(lexCHAR);} |
| 287 |
const {PA_KEYWORD_RETURN(lexCONST);} |
| 288 |
continue {PA_KEYWORD_RETURN(CONTINUE);} |
| 289 |
default {PA_KEYWORD_RETURN(lexDEFAULT);} |
| 290 |
define {PP_KEYWORD_RETURN(DEFINE);} |
| 291 |
defined {PP_KEYWORD_RETURN(OPDEFINED);} |
| 292 |
do {PA_KEYWORD_RETURN(DO);} |
| 293 |
double {PA_KEYWORD_RETURN(lexDOUBLE);} |
| 294 |
elif {PP_KEYWORD_RETURN(ELIF);} |
| 295 |
else {PPPA_KEYWORD_RETURN(ELSE);} |
| 296 |
endif {PP_KEYWORD_RETURN(ENDIF);} |
| 297 |
enum {PA_KEYWORD_RETURN(ENUM);} |
| 298 |
error {PP_KEYWORD_RETURN(ERROR);} |
| 299 |
extern {PA_KEYWORD_RETURN(EXTERN);} |
| 300 |
float {PA_KEYWORD_RETURN(lexFLOAT);} |
| 301 |
for {PA_KEYWORD_RETURN(FOR);} |
| 302 |
goto {PA_KEYWORD_RETURN(GOTO);} |
| 303 |
if {PPPA_KEYWORD_RETURN(IF);} |
| 304 |
ifdef {PP_KEYWORD_RETURN(IFDEF);} |
| 305 |
ifndef {PP_KEYWORD_RETURN(IFNDEF);} |
| 306 |
include {PP_KEYWORD_RETURN(INCLUDE); } |
| 307 |
int {PA_KEYWORD_RETURN(lexINT);} |
| 308 |
line {PP_KEYWORD_RETURN(LINE);} |
| 309 |
long {PA_KEYWORD_RETURN(lexLONG);} |
| 310 |
pragma {PP_KEYWORD_RETURN(PRAGMA);} |
| 311 |
register {PA_KEYWORD_RETURN(REGISTER);} |
| 312 |
return {PA_KEYWORD_RETURN(RETURN);} |
| 313 |
short {PA_KEYWORD_RETURN(SHORT);} |
| 314 |
signed {PA_KEYWORD_RETURN(SIGNED);} |
| 315 |
sizeof {PA_KEYWORD_RETURN(SIZEOF);} |
| 316 |
static {PA_KEYWORD_RETURN(STATIC);} |
| 317 |
struct {PA_KEYWORD_RETURN(lexSTRUCT);} |
| 318 |
switch {PA_KEYWORD_RETURN(SWITCH);} |
| 319 |
typedef {PA_KEYWORD_RETURN(TYPEDEF);} |
| 320 |
undef {PP_KEYWORD_RETURN(UNDEF);} |
| 321 |
union {PA_KEYWORD_RETURN(UNION);} |
| 322 |
unsigned {PA_KEYWORD_RETURN(UNSIGNED);} |
| 323 |
void {PA_KEYWORD_RETURN(lexVOID);} |
| 324 |
volatile {PA_KEYWORD_RETURN(VOLATILE);} |
| 325 |
while {PA_KEYWORD_RETURN(WHILE);} |
| 326 |
|
| 327 |
|
| 328 |
class {CPP_KEYWORD_RETURN(lexCLASS);} |
| 329 |
namespace {CPP_KEYWORD_RETURN(lexNAMESPACE);} |
| 330 |
delete {CPP_KEYWORD_RETURN(lexDELETE);} |
| 331 |
friend {CPP_KEYWORD_RETURN(FRIEND);} |
| 332 |
inline {CPP_KEYWORD_RETURN(INLINE);} |
| 333 |
new {CPP_KEYWORD_RETURN(NEW);} |
| 334 |
operator {CPP_KEYWORD_RETURN(OPERATOR);} |
| 335 |
overload {CPP_KEYWORD_RETURN(OVERLOAD);} |
| 336 |
protected {CPP_KEYWORD_RETURN(lexPROTECTED);} |
| 337 |
private {CPP_KEYWORD_RETURN(lexPRIVATE);} |
| 338 |
public {CPP_KEYWORD_RETURN(lexPUBLIC);} |
| 339 |
this {CPP_KEYWORD_RETURN(lexTHIS);} |
| 340 |
virtual {CPP_KEYWORD_RETURN(VIRTUAL);} |
| 341 |
|
| 342 |
{identifier} {IDENTIFIER_RETURN();} |
| 343 |
|
| 344 |
{decimal_constant} {NUMERICAL_RETURN(INTEGERconstant);} |
| 345 |
{octal_constant} {NUMERICAL_RETURN(OCTALconstant);} |
| 346 |
{hex_constant} {NUMERICAL_RETURN(HEXconstant);} |
| 347 |
{floating_constant} {NUMERICAL_RETURN(FLOATINGconstant);} |
| 348 |
|
| 349 |
|
| 350 |
"L"?[']{c_char}+['] { |
| 351 |
NUMERICAL_RETURN(CHARACTERconstant); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
"L"?["]{s_char}*["] { |
| 356 |
LITERAL_RETURN(STRINGliteral);} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
"(" {PPOP_RETURN(LP);} |
| 362 |
")" {PPOP_RETURN(RP);} |
| 363 |
"," {PPOP_RETURN(COMMA);} |
| 364 |
"#" {NAMED_PPOP_RETURN('#') ;} |
| 365 |
"##" {NAMED_PPOP_RETURN(POUNDPOUND);} |
| 366 |
|
| 367 |
"{" {ASCIIOP_RETURN(LC);} |
| 368 |
"}" {ASCIIOP_RETURN(RC);} |
| 369 |
"[" {ASCIIOP_RETURN(LB);} |
| 370 |
"]" {ASCIIOP_RETURN(RB);} |
| 371 |
"." {ASCIIOP_RETURN(DOT);} |
| 372 |
"&" {ASCIIOP_RETURN(AND);} |
| 373 |
"*" {ASCIIOP_RETURN(STAR);} |
| 374 |
"+" {ASCIIOP_RETURN(PLUS);} |
| 375 |
"-" {ASCIIOP_RETURN(MINUS);} |
| 376 |
"~" {ASCIIOP_RETURN(NEGATE);} |
| 377 |
"!" {ASCIIOP_RETURN(NOT);} |
| 378 |
"/" {ASCIIOP_RETURN(DIV);} |
| 379 |
"%" {ASCIIOP_RETURN(MOD);} |
| 380 |
"<" {ASCIIOP_RETURN(LT);} |
| 381 |
">" {ASCIIOP_RETURN(GT);} |
| 382 |
"^" {ASCIIOP_RETURN(XOR);} |
| 383 |
"|" {ASCIIOP_RETURN(PIPE);} |
| 384 |
"?" {ASCIIOP_RETURN(QUESTION);} |
| 385 |
":" {ASCIIOP_RETURN(COLON);} |
| 386 |
";" {ASCIIOP_RETURN(SEMICOLON);} |
| 387 |
"=" {ASCIIOP_RETURN(ASSIGN);} |
| 388 |
|
| 389 |
".*" {NAMEDOP_RETURN(DOTstar);} |
| 390 |
"::" {NAMEDOP_RETURN(CLCL);} |
| 391 |
"->" {NAMEDOP_RETURN(ARROW);} |
| 392 |
"->*" {NAMEDOP_RETURN(ARROWstar);} |
| 393 |
"++" {NAMEDOP_RETURN(ICR);} |
| 394 |
"--" {NAMEDOP_RETURN(DECR);} |
| 395 |
"<<" {NAMEDOP_RETURN(LS);} |
| 396 |
">>" {NAMEDOP_RETURN(RS);} |
| 397 |
"<=" {NAMEDOP_RETURN(LE);} |
| 398 |
">=" {NAMEDOP_RETURN(GE);} |
| 399 |
"==" {NAMEDOP_RETURN(EQ);} |
| 400 |
"!=" {NAMEDOP_RETURN(NE);} |
| 401 |
"&&" {NAMEDOP_RETURN(ANDAND);} |
| 402 |
"||" {NAMEDOP_RETURN(OROR);} |
| 403 |
"*=" {NAMEDOP_RETURN(MULTassign);} |
| 404 |
"/=" {NAMEDOP_RETURN(DIVassign);} |
| 405 |
"%=" {NAMEDOP_RETURN(MODassign);} |
| 406 |
"+=" {NAMEDOP_RETURN(PLUSassign);} |
| 407 |
"-=" {NAMEDOP_RETURN(MINUSassign);} |
| 408 |
"<<=" {NAMEDOP_RETURN(LSassign);} |
| 409 |
">>=" {NAMEDOP_RETURN(RSassign);} |
| 410 |
"&=" {NAMEDOP_RETURN(ANDassign);} |
| 411 |
"^=" {NAMEDOP_RETURN(ERassign);} |
| 412 |
"|=" {NAMEDOP_RETURN(ORassign);} |
| 413 |
"..." {NAMEDOP_RETURN(ELLIPSIS);} |
| 414 |
. {return yytext[0];} |
| 415 |
|
| 416 |
|
| 417 |
%% |
| 418 |
|
| 419 |
/*******************************************************************/ |