/* Definitions */
Delimiter [ \t\n]
WhiteSpace {Delimiter}+
Letter [A-Za-z]
Digit [0-9]
%%
/* Translation Rules */
{Letter} ({Letter} | {Digit} )
{printf ("\t (Id, %s) \n",
yytext);
{Digit}+
{printf ("\t (Lit, %s) \n",
yytext);
("+"|"-"|"*"|"/"|":=")
{printf ("\t (Op, %s) \n",
yytext);
(";"| "(" | ")")
{printf ("\t (Punct, %s) \n",
yytext);
Here, a Delimiter is defined to be a space or a tab or a newline
character. WhiteSpace is defined as one or more delimiter. No use-written
procedures are shown. There are other ways to find these same tokens using
LEX. When the lexical analyzer is used along with a parser, each lex action should end with a return statement of the form:
return token;
or
return(token);
E.g., we will add return (Id) after the first line in the Translation Rules to return the class Id to the parser generated by YACC.
Send questions and comment to: Karen Lemone