forked from mbbill/flexbison
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cppcalc.l
50 lines (38 loc) · 1.29 KB
/
cppcalc.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* recognize tokens for the C++ calculator and print them out */
/* Companion source code for "flex & bison", published by O'Reilly
* Media, ISBN 978-0-596-15597-1
* Copyright (c) 2009, Taughannock Networks. All rights reserved.
* See the README file for license conditions and contact info.
* $Header: /home/johnl/flnb/code/RCS/cppcalc.l,v 2.1 2009/11/08 02:53:18 johnl Exp $
*/
%option noyywrap
%{
# include <cstdlib>
# include "cppcalc-ctx.hh"
# include "cppcalc.tab.hh"
#define YY_DECL int yylex(yy::cppcalc::semantic_type *yylval, \
yy::cppcalc::location_type *yylloc, cppcalc_ctx &ctx)
// make location include the current token
# define YY_USER_ACTION yylloc->columns (yyleng);
typedef yy::cppcalc::token token;
extern int myatoi(int radix, char *s);
%}
%%
%{
// start where previous token ended
yylloc->step ();
%}
"+" { return token::ADD; }
"-" { return token::SUB; }
"*" { return token::MUL; }
"/" { return token::DIV; }
"|" { return token::ABS; }
"(" { return token::OP; }
")" { return token::CP; }
[0-9]+ { yylval->ival = myatoi(ctx.getradix(), yytext); return token::NUMBER; }
\n { yylloc->lines(1); return token::EOL; }
/* skip over comments and white space */
"//".* |
[ \t] { yylloc->step (); }
. { printf("Mystery character %c\n", *yytext); }
%%