-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.c
More file actions
107 lines (85 loc) · 2.3 KB
/
lex.c
File metadata and controls
107 lines (85 loc) · 2.3 KB
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
/*
* A simple lexical analyzer
*
* Copyright (C) 2018 Nicolas Mauger
* This file is subject to the terms and conditions of the
* CeCILL free software license agreement. See the file
* LICENSE in the main directory of T7 for more details.
*/
#include <stdio.h>
#include <ctype.h>
#include "lex.h"
char *yytext = ""; /* Lexeme (not '\0' terminated */
int yyleng = 0; /* Lexeme length */
int yylineno = 0; /* Input line number */
int lex()
{
static char input_buffer[128];
char *current;
current = yytext + yyleng;
while (1)
{
while ( !*current)
{
/* Get new lines, skipping any leading white space on the line, until a nonblank line is found. */
current = input_buffer;
if (!fgets(input_buffer, sizeof(input_buffer), stdin))
{
current = '\0';
return EOI;
}
++yylineno;
while (isspace(*current)) ++current;
}
for (; *current ; ++current)
{
/* Get the next token */
yytext = current;
yyleng = 1;
switch (*current)
{
case EOF:
return EOI ;
case ';':
return SEMI;
case '+':
return PLUS;
case '*':
return TIMES;
case '(':
return LP;
case ')':
return RP;
case '\n':
case '\t':
case '\r':
case ' ':
break;
default:
if (!isalnum(*current))
{
fprintf(stderr, "Ignore illegal input <%c>\n", *current);
}
else
{
while (isalnum(*current)) ++current;
yyleng = current - yytext;
return NUM_OR_ID;
}
break;
}
}
}
}
static int Lookahead = -1; /* Lookahead token */
int match (int token )
{
/* Return true if "token" matches the current lookahead symbol. */
if (Lookahead == -1) Lookahead = lex();
return token == Lookahead;
}
void advance()
{
/* Advance the lookahead to the next input symbol. */
Lookahead = lex();
}