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