-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.l
67 lines (57 loc) · 1.74 KB
/
core.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
%{
#include <stdio.h>
#include "y.tab.h"
int lineCounter = 1;
void yyerror (char *s);
int yylex();
%}
INTEGER [-]?[1-9][0-9]*|0
IDENTIFIER [a-z][a-zA-Z0-9_]*
NEWLINE [\n]
STRING ["].*["]
COMMENT [\/][\/].*
%%
"START" return(START);
"END" return(END);
"print" return(PRINT);
"(" return(LPAREN);
")" return(RPAREN);
"{" return(LBRACE);
"}" return(RBRACE);
"+" return(PLUS);
"-" return(MINUS);
"*" return(MULTIPLY);
"/" return(DIVIDE);
"%" return(MOD);
"=" return(ASSIGN);
";" return(SEMICOLON);
"," return(COMMA);
"==" return(EQUAL);
"!=" return(NOTEQUAL);
"<" return(LESS);
"<=" return(LESSEQUAL);
">" return(GREATER);
">=" return(GREATEREQUAL);
"&&" return(AND);
"||" return(OR);
"/n" return(NEWLINE);
"if" return(IF);
"else" return(ELSE);
"else if" return(ELSEIF);
"while" return(WHILE);
{IDENTIFIER} {yylval.id = yytext[0]; return IDENTIFIER;}
{INTEGER} {yylval.num = atoi(yytext); return NUMBER;}
{STRING} {
char* str = malloc(yyleng - 2);
strncpy(str, yytext + 1, yyleng - 2);
yylval.str = str;
return STRING;
}
{COMMENT} return (COMMENT);
{NEWLINE} {lineCounter++;}
[ \t\r] ;
. {yyerror ("unexpected character");}
%%
int yywrap(void) {
return 1;
}