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)
|
---|
13 | extern "C" {
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | /* should be defined in stdio.h */
|
---|
17 | extern int fileno(FILE *);
|
---|
18 |
|
---|
19 | #if defined(__cplusplus)
|
---|
20 | }
|
---|
21 | #endif
|
---|
22 |
|
---|
23 |
|
---|
24 | int lineno = 1;
|
---|
25 | string lastID;
|
---|
26 | string lastNUM;
|
---|
27 | string lastOP;
|
---|
28 |
|
---|
29 | %}
|
---|
30 |
|
---|
31 | %option nounput
|
---|
32 |
|
---|
33 | LETTER [a-zA-Z]
|
---|
34 | DIGIT [0-9]
|
---|
35 | ID {LETTER}({LETTER}|{DIGIT})*
|
---|
36 | DIGITS {DIGIT}{DIGIT}*
|
---|
37 |
|
---|
38 | OPTIONAL_FRACTION ("."{DIGITS})?
|
---|
39 | OPTIONAL_EXPONENT (("E"("+"|"-")?{DIGITS}))?
|
---|
40 | NUM {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 |
|
---|
52 | program {
|
---|
53 | pmesg(90, "Lexer: PROGRAM: %s\n", yytext);
|
---|
54 | return PROGRAM;
|
---|
55 | }
|
---|
56 |
|
---|
57 | if {
|
---|
58 | pmesg(90, "Lexer: IF: %s\n", yytext);
|
---|
59 | return IF;
|
---|
60 | }
|
---|
61 |
|
---|
62 | then {
|
---|
63 | pmesg(90, "Lexer: THEN: %s\n", yytext);
|
---|
64 | return THEN;
|
---|
65 | }
|
---|
66 |
|
---|
67 | else {
|
---|
68 | pmesg(90, "Lexer: ELSE: %s\n", yytext);
|
---|
69 | return ELSE;
|
---|
70 | }
|
---|
71 |
|
---|
72 | while {
|
---|
73 | pmesg(90, "Lexer: WHILE: %s\n", yytext);
|
---|
74 | return WHILE;
|
---|
75 | }
|
---|
76 |
|
---|
77 | do {
|
---|
78 | pmesg(90, "Lexer: DO: %s\n", yytext);
|
---|
79 | return DO;
|
---|
80 | }
|
---|
81 |
|
---|
82 | begin {
|
---|
83 | pmesg(90, "Lexer: PASCALBEGIN: %s\n", yytext);
|
---|
84 | return PASCALBEGIN;
|
---|
85 | }
|
---|
86 |
|
---|
87 | end {
|
---|
88 | pmesg(90, "Lexer: END: %s\n", yytext);
|
---|
89 | return END;
|
---|
90 | }
|
---|
91 |
|
---|
92 | procedure {
|
---|
93 | pmesg(90, "Lexer: PROCEDURE: %s\n", yytext);
|
---|
94 | return PROCEDURE;
|
---|
95 | }
|
---|
96 |
|
---|
97 | function {
|
---|
98 | pmesg(90, "Lexer: FUNCTION: %s\n", yytext);
|
---|
99 | return FUNCTION;
|
---|
100 | }
|
---|
101 |
|
---|
102 | var {
|
---|
103 | pmesg(90, "Lexer: VAR: %s\n", yytext);
|
---|
104 | return VAR;
|
---|
105 | }
|
---|
106 |
|
---|
107 | integer {
|
---|
108 | pmesg(90, "Lexer: INTEGER: %s\n", yytext);
|
---|
109 | return INTEGER;
|
---|
110 | }
|
---|
111 |
|
---|
112 | real {
|
---|
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 | %%
|
---|