/* This file defines the valid tokens for a Jay language program. * If provided as input to the standard UNIX tool lex, it * generates a lexical analyzer that recognizes those tokens * on behalf of the Jay Parser (see jay.yacc). */ /* DEFINITIONS SECTION */ /* First, provide any C code that should appear at the beginning of * the lexical analysis code. In this case, read in the header file * that defines the codes that represent various language * constructs, declares global variables shared by lex and yacc, etc. */ %{ #include #include #include "jayParser.h" #include "y.tab.h" #define INV_TOKEN \ "Token %c on line %d not recognized by lexical analyzer; will be ignored.\n" string_type createCopy(string_type orig) { string_type copy = malloc(strlen(orig) + 1); (void) strcpy(copy, orig); return copy; } %} /* Define white space */ /* Define valid identifiers and valid single digit */ WS [ \t]+ NL [\n|\r|\r\n] DIGIT [0-9] IDENT [A-Za-z][A-Za-z0-9]* %% /* RULES SECTION */ /* WHITESPACE */ {WS} { /* Skip it! */ ; } {NL} { /* NEWLINE -- increment yacc's line count */ line_count++; } /* JAY LANGUAGE KEYWORDS */ main { yylval.keyword = Main; return MAIN; } boolean { yylval.keyword = Boolean; return JAYTYPE; } int { yylval.keyword = Int; return JAYTYPE; } void { yylval.keyword = Void; return JAYTYPE; } if { yylval.keyword = If; return IF; } else { yylval.keyword = Else; return ELSE; } while { yylval.keyword = While; return WHILE; } /* OPERATORS & PUNCTUATION */ "=" { yylval.keyword = Assign; return ASSIGN; } "+" { yylval.keyword = Plus; return ADDSUB; } "-" { yylval.keyword = Minus; return ADDSUB; } ">" { yylval.keyword = Greater; return RELOP; } "(" { yylval.keyword = Lparen; return LPAREN; } ")" { yylval.keyword = Rparen; return RPAREN; } "{" { yylval.keyword = Lbrace; return LBRACE; } "}" { yylval.keyword = Rbrace; return RBRACE; } "," { yylval.keyword = Comma; return COMMA; } ";" { yylval.keyword = Semi; return SEMI; } /* CONSTANTS */ {DIGIT}+ { yylval.stringrep = createCopy(yytext); return INT_CONSTANT; } /* IDENTIFIERS */ {IDENT} { yylval.stringrep = createCopy(yytext); return IDENTIFIER; } /* COMMENTS */ \/\/.* { /* COMMENT -- Skip from // to end of line. */ } /* ANYTHING ELSE */ . { fprintf(stderr, INV_TOKEN, *yytext, line_count); }