-
Notifications
You must be signed in to change notification settings - Fork 1
/
BabyC.lex
56 lines (44 loc) · 1.16 KB
/
BabyC.lex
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
%{
#include <stdio.h>
#include <string.h>
#include "BabyC.tab.h"
#include "compiler.h"
void yyerror (const char *s)
{
printf("ERROR on line %d: %s.\n", yylineno, s);
exit(1);
}
%}
/*Option to get the line number*/
%option yylineno
/*We are not using yywrap. So, use this option to disable it and supress a compile error*/
%option noyywrap
%%
"," return ',';
";" return ';';
"+" return '+';
"-" return '-';
"*" return '*';
"/" return '/';
"<" return '<';
">" return '>';
"=" return '=';
"(" return '(';
")" return ')';
"{" return '{';
"}" return '}';
"||" return OR;
"&&" return AND;
"==" return EQ;
"!=" return NE;
"<=" return LE;
">=" return GE;
"int" return INT;
"main" return MAIN;
"if" return IF;
"else" return ELSE;
"while" return WHILE;
[a-z][a-z0-9]* yylval.string = strdup(yytext); return IDENT; // This is the action for IDENT. Write the regular expression before the action.
[1-9][0-9]*|0 yylval.num = atoi(yytext); return NUM; // This is the action for NUM. Write the regular expression before the action.
[ \t\n]+ //Whitespace is ignored
. printf( "ERROR on Line %d: Unrecognized token \n", yylineno ); exit(1); //No match. Fatal error.