0% found this document useful (0 votes)
26 views4 pages

Preinc Calc L

The document defines lexical rules for a lexer to tokenize arithmetic expressions and assignments containing increment/decrement operators. It includes token definitions for operators, parentheses, identifiers, integers, and other tokens. Example expressions are lexed and the tokens and text are printed to demonstrate the lexer working as intended.

Uploaded by

wfmoloney
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Preinc Calc L

The document defines lexical rules for a lexer to tokenize arithmetic expressions and assignments containing increment/decrement operators. It includes token definitions for operators, parentheses, identifiers, integers, and other tokens. Example expressions are lexed and the tokens and text are printed to demonstrate the lexer working as intended.

Uploaded by

wfmoloney
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Quit

%{
#define ADDOP '+'
#define SUBOP '-'
#define MULOP '*'
#define DIVOP '/'
#define LPAREN '('
#define RPAREN ')'
#define ASGNOP '='
#define DOT '.'
#define LBRACE '{'
#define RBRACE '}'

#define ID 257
#define INT 258
#define EOLN 259
#define BAD 260
#define REPEAT 261
#define END 262
#define PREINC 263

%}

%%

"+++" {unput('+');
unput('+');
return(ADDOP);}

"+" return(ADDOP);

"++" return(PREINC);

"-" return(SUBOP);
"*" return(MULOP);

"/" return(DIVOP);

"(" return(LPAREN);

")" return(RPAREN);

"{" return(LBRACE);

"}" return(RBRACE);

"=" return(ASGNOP);

"." return(DOT);

'\n' return(EOLN);

[0-9][0-9]* return(INT);

[A-Za-z][0-9A-Za-z]* return(ID);

"" |
"\t" ;

[^ \{\}\.\t\n\+\-\*\/\(\)\=]* return(BAD);
%%
int yywrap(){
return(1);
}
int main(){
int tok;
while ((tok = yylex()) != EOF){
printf("token number is \t %d, \t %s\n", tok, yytext);
}
printf("\n Program Done\n");
}

% lex preinc_calc.l
% make lex.yy
cc lex.yy.c -o lex.yy
% lex.yy
3+2
token number is 258, 3
token number is 43, +
token number is 258, 2

f=++b
token number is 257, f
token number is 61, =
token number is 263, ++
token number is 257, b

f=23+++d
token number is 257, f
token number is 61, =
token number is 258, 23
token number is 43, +++
token number is 263, ++
token number is 257, d
g=g+++e
token number is 257, g
token number is 61, =
token number is 257, g
token number is 43, +++
token number is 263, ++
token number is 257, e

You might also like