ACSE 2.0.2
Advanced Compiler System for Education (basic documentation)
Loading...
Searching...
No Matches
scanner.l
Go to the documentation of this file.
1
3
4%{
5#include <string.h>
6#include "list.h"
7#include "scanner.h"
8#include "parser.h"
9#include "parser.tab.h"
10
12
Structure that represents a location in a file.
Definition errors.h:19
t_fileLocation curFileLoc
Definition scanner.l:11
A double-linked list.
Header file associated to parser.y.
Header file associated to scanner.y.
13%}
14
15/* Disable multi-file support. */
16%option noyywrap
17/* Define a new comment state. */
18%x comment
19
20/*
21 * Name definitions
22 */
23
24DIGIT [0-9]
25ID [a-zA-Z_][a-zA-Z0-9_]*
26
27/*
28 * Rules
29 */
30
31%%
32
33"\r\n" { curFileLoc.row++; }
int row
The zero-based index of a line in the file.
Definition errors.h:21
34"\n" { curFileLoc.row++; }
35
36[ \t\f\v]+ { /* Ignore whitespace. */ }
37
38"//"[^\n]* { /* Ignore comment lines. */ }
39"/*" BEGIN(comment);
40
41<comment>[^*\n]*
42<comment>[^*\n]*\n { curFileLoc.row++; }
43<comment>"*"+[^*/\n]*
44<comment>"*"+[^*/\n]*\n { curFileLoc.row++; }
45<comment>"*"+"/" BEGIN(INITIAL);
46
47"{" { return LBRACE; }
48"}" { return RBRACE; }
49"[" { return LSQUARE; }
50"]" { return RSQUARE; }
51"(" { return LPAR; }
52")" { return RPAR; }
53";" { return SEMI; }
54"+" { return PLUS; }
55"-" { return MINUS; }
56"*" { return MUL_OP; }
57"/" { return DIV_OP; }
58"%" { return MOD_OP; }
59"&" { return AND_OP; }
60"^" { return XOR_OP; }
61"|" { return OR_OP; }
62"!" { return NOT_OP; }
63"=" { return ASSIGN; }
64"<" { return LT; }
65">" { return GT; }
66"<<" { return SHL_OP; }
67">>" { return SHR_OP; }
68"==" { return EQ; }
69"!=" { return NOTEQ; }
70"<=" { return LTEQ; }
71">=" { return GTEQ; }
72"&&" { return ANDAND; }
73"||" { return OROR; }
74"," { return COMMA; }
75
76"do" { return DO; }
77"else" { return ELSE; }
78"if" { return IF; }
79"int" { return TYPE; }
80"while" { return WHILE; }
81"return" { return RETURN; }
82"read" { return READ; }
83"write" { return WRITE; }
84
85{ID} {
86 yylval.string = strdup(yytext);
87 return IDENTIFIER;
88 }
89{DIGIT}+ {
90 yylval.integer = atoi(yytext);
91 return NUMBER;
92 }
93
94. {
95 yyerror("unexpected token");
96 return -1;
97 }
void yyerror(const char *msg)
98<INITIAL><<EOF>> {
99 curFileLoc.row = -1;
100 return EOF_TOK;
101 }