Some templates of LLVM
[toypasc.git] / scanner.l
blob9d3a4f7db997e6295d2b509f565a4f03cc5cef11
1 %{
2 #include <stdio.h>
3 #include "base.h"
4 #include "parser.h"
6 #define uploc   { yylloc->first_column = yylloc->last_column + 1; yylloc->last_column += yyleng; }
8 #define CAST_BOOLEAN(strb)  strcasecmp(strb, "False") ? TRUE : FALSE
9 /*#define YY_DECL int yylex (YYSTYPE *yylval_param)*/
12 %option yylineno
13 %option bison-bridge
14 %option bison-locations
15 %option noyywrap
16 %option nounput
17 %option case-insensitive
20 "{"                         { int c;
21                               while ((c = input())) {
22                                 if (c == '}')
23                                     break;
24                                 else if (c == '\n') {
25                                     yylloc->first_line = yylloc->last_line = yylineno;
26                                     yylloc->first_column = 1;
27                                     yylloc->last_column = 0;
28                                 } else if (c == EOF) {
29                                     fprintf (stderr,
30                                              "Error: unexpected EOF inside comment "
31                                              "at line %d\n",
32                                              yylineno);
33                                     exit (1);
34                                 }
35                               }
36                             }
37 "program"                   { uploc; return T_PROGRAM; }
38 "var"                       { uploc; return T_VAR; }
39 "integer"|"boolean"|"char"  { uploc;
40                               yylval->type = type_get_from_lexeme(yytext);
41                               return TYPE_IDENTIFIER;
42                             }
44 "procedure"                 { uploc; return T_PROCEDURE; }
45 "function"                  { uploc; return T_FUNCTION; }
46 "begin"                     { uploc; return T_BEGIN; }
47 "end"                       { uploc; return T_END; }
49 "printInt"                  { uploc; return T_PRINT_INT; }
50 "printBool"                 { uploc; return T_PRINT_BOOL; }
51 "printChar"                 { uploc; return T_PRINT_CHAR; }
52 "println"                   { uploc; return T_PRINT_LINE; }
54 "if"                        { uploc; return T_IF; }
55 "then"                      { uploc; return T_THEN; }
56 "else"                      { uploc; return T_ELSE; }
57 "while"                     { uploc; return T_WHILE; }
58 "for"                       { uploc; return T_FOR; }
59 "to"                        { uploc; return T_TO; }
60 "do"                        { uploc; return T_DO; }
62 ":="                        { uploc; return T_ASSIGNMENT; }
63 "("                         { uploc; return T_LPAR; }
64 ")"                         { uploc; return T_RPAR; }
65 "+"                         { uploc; yylval->lexeme=strdup(yytext);
66                               return T_PLUS;
67                             }
68 "-"                         { uploc; yylval->lexeme=strdup(yytext);
69                               return T_MINUS;
70                             }
71 "*"                         { uploc; yylval->lexeme=strdup(yytext);
72                               return T_STAR;
73                             }
74 "/"                         { uploc; yylval->lexeme=strdup(yytext);
75                               return T_SLASH;
76                             }
77 "and"                       { uploc; yylval->lexeme=strdup(yytext);
78                               return T_AND;
79                             }
80 "or"                        { uploc; yylval->lexeme=strdup(yytext);
81                               return T_OR;
82                             }
83 "not"                       { uploc; yylval->lexeme=strdup(yytext);
84                               return T_NOT;
85                             }
86 ">"                         { uploc; yylval->lexeme=strdup(yytext);
87                               return T_GREATER;
88                             }
89 "<"                         { uploc; yylval->lexeme=strdup(yytext);
90                               return T_LESSER;
91                             }
92 "="                         { uploc; yylval->lexeme=strdup(yytext);
93                               return T_EQUAL;
94                             }
95 "<>"                        { uploc; yylval->lexeme=strdup(yytext);
96                               return T_NOTEQUAL;
97                             }
98 ">="                        { uploc; yylval->lexeme=strdup(yytext);
99                               return T_GREATEREQUAL;
100                             }
101 "<="                        { uploc; yylval->lexeme=strdup(yytext);
102                               return T_LESSEREQUAL;
103                             }
105 ";"                         { uploc; return T_SEMICOLON; }
106 ":"                         { uploc; return T_COLON; }
107 "."                         { uploc; return T_DOT; }
108 ","                         { uploc; return T_COMMA; }
110 "true"|"false"              { uploc;
111                               yylval->boolean=CAST_BOOLEAN(yytext);
112                               return BOOL_LITERAL;
113                             }
114 "'"."'"                     { uploc; yylval->character=yytext[1];
115                               return CHAR_LITERAL;
116                             }
117 [0-9]+                      { uploc; yylval->integer=atoi(yytext);
118                               return INT_LITERAL;
119                             }
120 [A-Za-z][A-Za-z0-9]*        { uploc; yylval->lexeme=strdup(yytext);
121                               return IDENTIFIER;
122                             }
124 [ \t]+                      /* ignora whitespace */;
125 [\n]                        { yylloc->first_line = yylloc->last_line = yylineno;
126                               yylloc->first_column = 1;
127                               yylloc->last_column = 0;
128                             }
130 .                           { uploc;
131                               fprintf (stderr,
132                                        "Syntax Error: unexpected "
133                                        "character '%s' at line %d\n",
134                                        yytext, yylloc->first_line);
135                             }