-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexic.l
118 lines (96 loc) · 2.66 KB
/
lexic.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "reserved_words.h"
#include "syntactic.yy.h"
enum args
{
ARG_EXE = 0,
ARG_FILE,
ARG_SIZE
};
int specific(char* str, int token);
int symbol(char* str);
int lexical(char* str);
int lex_error(char *);
int line = 1;
int column = 1;
FILE* yyin;
#define INC_COL column += strlen(yytext);
#define INC_LIN line++; column = 1;
%}
%option noyywrap
%%
\{.*\} { INC_COL } /* Commentary LALG */
[ \t\r] { INC_COL } /* Line Spacing */
\n { INC_LIN } /* Line Break */
[0-9]+\.[0-9]+ { INC_COL return specific(yytext, REAL); } /* Real Number */
[0-9]+ { INC_COL return specific(yytext, INTEGER); } /* Integer Number */
[:][=] { INC_COL return ASSIGN_SYMBOL; } /* Assign */
[<][>] { INC_COL return DIF_SYMBOL; } /* Comparer */
[>][=] { INC_COL return MAJOR_EQUAL_SYMBOL; } /* Comparer */
[<][=] { INC_COL return MINOR_EQUAL_SYMBOL; } /* Comparer */
[+\-\*\/\(\)\[\]\.,;:=><] { INC_COL return symbol(yytext); } /* Operators and Comparer */
[a-zA-Z][a-zA-Z0-9_]* { INC_COL return lexical(yytext); } /* Identifiers and Reserved Words */
. { lex_error(yytext); } /* Error */
%%
/* Specific lexical, like numbers and identifiers */
int specific(char* str, int token)
{
/* printf("%s - %s\n", str, token); // Print input and lexical */
switch(token)
{
case INTEGER:
yylval.intval = atoi(str);
break;
case REAL:
yylval.realval = atof(str);
break;
case IDENTIFIER:
default:
yylval.strval = strdup(str);
}
return token;
}
/* Symbol lexical, like operators and comparer */
int symbol(char* str)
{
// Look for lexical in perfect hash function
// generated by gperf (see reserved_words.h)
const WORD* hash_word = in_word_set(str, strlen(str));
if(hash_word) // Print if in hash-table
return hash_word->token;
// Otherwise it's an error [it's not supposed to enter here]
return lex_error(str);
}
/* Any other lexical, like keywords and identifiers */
int lexical(char* str)
{
// Look for lexical in perfect hash function
// generated by gperf (see reserved_words.h)
const WORD* hash_word = in_word_set(str, strlen(str));
if(hash_word) // Print if in hash-table
return hash_word->token;
// Otherwise it's an identifier
return specific(str, IDENTIFIER);
}
/* Error */
int lex_error(char *str)
{
printf("\nLexical error! Word: %s at line: %d, column: %d\n", str, line, column);
return -1;
}
/* Main is now useless, all the computation is made in syntactic
int main(int argc, char *argv[])
{
if(argc == ARG_SIZE) // Read from file if has arguments
{
yyin = fopen(argv[ARG_FILE], "r");
yylex();
fclose(yyin);
}
else // Read from stdin
yylex();
}
*/