source: liacs/coco/assignment2/comp.l@ 144

Last change on this file since 144 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 4.2 KB
Line 
1/* C declarations */
2%{
3
4#include <stdio.h>
5#include <string>
6#include "debug.h"
7#include "Node.h"
8#include "y.tab.h"
9#include "types.h"
10
11/* extern C declarations */
12#if defined(__cplusplus)
13extern "C" {
14#endif
15
16/* should be defined in stdio.h */
17extern int fileno(FILE *);
18
19#if defined(__cplusplus)
20}
21#endif
22
23
24int lineno = 1;
25string lastID;
26string lastNUM;
27string lastOP;
28
29%}
30
31%option nounput
32
33LETTER [a-zA-Z]
34DIGIT [0-9]
35ID {LETTER}({LETTER}|{DIGIT})*
36DIGITS {DIGIT}{DIGIT}*
37
38OPTIONAL_FRACTION ("."{DIGITS})?
39OPTIONAL_EXPONENT (("E"("+"|"-")?{DIGITS}))?
40NUM {DIGITS}{OPTIONAL_FRACTION}{OPTIONAL_EXPONENT}
41
42
43/* Patterns */
44%%
45
46{NUM} {
47 pmesg(90, "Lexer: NUM: %s\n", yytext);
48 lastNUM = (string) yytext;
49 return NUM;
50 }
51
52program {
53 pmesg(90, "Lexer: PROGRAM: %s\n", yytext);
54 return PROGRAM;
55 }
56
57if {
58 pmesg(90, "Lexer: IF: %s\n", yytext);
59 return IF;
60 }
61
62then {
63 pmesg(90, "Lexer: THEN: %s\n", yytext);
64 return THEN;
65 }
66
67else {
68 pmesg(90, "Lexer: ELSE: %s\n", yytext);
69 return ELSE;
70 }
71
72while {
73 pmesg(90, "Lexer: WHILE: %s\n", yytext);
74 return WHILE;
75 }
76
77do {
78 pmesg(90, "Lexer: DO: %s\n", yytext);
79 return DO;
80 }
81
82begin {
83 pmesg(90, "Lexer: PASCALBEGIN: %s\n", yytext);
84 return PASCALBEGIN;
85 }
86
87end {
88 pmesg(90, "Lexer: END: %s\n", yytext);
89 return END;
90 }
91
92procedure {
93 pmesg(90, "Lexer: PROCEDURE: %s\n", yytext);
94 return PROCEDURE;
95 }
96
97function {
98 pmesg(90, "Lexer: FUNCTION: %s\n", yytext);
99 return FUNCTION;
100 }
101
102var {
103 pmesg(90, "Lexer: VAR: %s\n", yytext);
104 return VAR;
105 }
106
107integer {
108 pmesg(90, "Lexer: INTEGER: %s\n", yytext);
109 return INTEGER;
110 }
111
112real {
113 pmesg(90, "Lexer: REAL: %s\n", yytext);
114 return REAL;
115 }
116":=" {
117 pmesg(90, "Lexer: ASSIGNOP: %s\n", yytext);
118 return ASSIGNOP;
119 }
120
121"="|"<"|">"|"<="|">="|"<>" {
122 pmesg(90, "Lexer: RELOP: %s\n", yytext);
123 lastOP = (string)yytext;
124 return RELOP;
125 }
126
127"or" { /* "-" cannot be returned as ADDOP, since it can also
128 be UMINUS; we can't distinguish these at the lex
129 level */
130 pmesg(90, "Lexer: ADDOP: %s\n", yytext);
131 return ADDOP;
132 }
133
134"*"|"/"|"div"|"mod"|"and" {
135 pmesg(90, "Lexer: MULOP: %s\n", yytext);
136 lastOP = (string)yytext;
137 return MULOP;
138 }
139
140"not" {
141 pmesg(90, "Lexer: NOT: %s\n", yytext);
142 return NOT;
143 }
144
145{ID} {
146 pmesg(90, "Lexer: ID: %s\n", yytext);
147 lastID = (string) yytext;
148 return ID;
149 }
150
151"{"[^{}]*"}" { /* Eat comments */
152 int cnt;
153 pmesg(90, "Lexer: Yummie\n");
154 for(cnt = 0; yytext[cnt] != '\0'; cnt++) {
155 if (yytext[cnt] == '\n') {
156 lineno++;
157 }
158 }
159 /* no return */
160 }
161
162\n {
163 lineno++;
164 pmesg(90, "Lexer: lineno == %i\n", lineno);
165 /* no return */
166 }
167
168[ \t]+ { /* eat up whitespace */
169 /* no return */
170 }
171
172. {
173 pmesg(90, "Lexer: Single non-identifier character: %s\n", yytext );
174 return(yytext[0]);
175 }
176
177%%
Note: See TracBrowser for help on using the repository browser.