Commit 8f24c3f5 authored by Peter Faber's avatar Peter Faber
Browse files

init t-08

parent 12989b13
all: tokens
OBJS = tokens.o
LLVMCONFIG = llvm-config-6.0
CPPFLAGS = -g `$(LLVMCONFIG) --cppflags` -std=c++11
LDFLAGS = `$(LLVMCONFIG) --ldflags` -rdynamic
LIBS = `$(LLVMCONFIG) --libs`
clean:
$(RM) -rf tokens.cpp tokens.hpp tokens $(OBJS)
tokens.cpp: tokens.l
flex --header-file=$(subst .cpp,.hpp,$@) -o $@ $^
tokens.hpp: tokens.cpp
%.o: %.cpp
$(CXX) -c $(CPPFLAGS) -o $@ $<
tokens: $(OBJS)
$(CXX) -o $@ $(OBJS) $(LIBS) $(LDFLAGS)
test: tokens example.txt
cat example.txt | ./$^
distclean: clean
$(RM) -rf .depend ./tokens.cpp ./tokens.hpp
.PHONY: clean distclean
depend: .depend
.depend: tokens.cpp tokens.hpp
$(CXX) `$(LLVMCONFIG) --cppflags` $(FLAGS) $(INCLUDE) -M *.cpp > .depend || true
$(CXX) `$(LLVMCONFIG) --cppflags` $(FLAGS) $(INCLUDE) -M *.hpp >> .depend || true
-include .depend
extern int printi(int val);
int do_math(int a) {
int x = a * 5;
return x + 3;
}
printi(do_math(8)-1);
%{
#include <cstdio>
#include <string>
/* Will later be replaced by parser definitions! */
int yylval;
enum token_t
{
TINT_T = 258,
TVOID_T = 259,
TIDENTIFIER = 260,
TINTEGER = 261,
TASSIGN = 262,
TNE = 263,
TLT = 264,
TLE = 265,
TGT = 266,
TGE = 267,
TEQ = 268,
TLPAREN = 269,
TRPAREN = 270,
TLBRACE = 271,
TRBRACE = 272,
TSEMICOL = 273,
TCOMMA = 274,
TPLUS = 275,
TMINUS = 276,
TMUL = 277,
TDIV = 278,
TRETURN = 279,
TEXTERN = 280,
TIF = 281,
TELSE = 282,
UMINUS = 283,
TNONE = 0
};
%}
%option noyywrap
%option yylineno
%%
[ \t\n] ;
"//".*$ ;
"extern" {return TEXTERN;}
"return" {return TRETURN;}
"int" {return TINT_T;}
"if" {return TIF;}
"else" {return TELSE;}
[a-zA-Z_][a-zA-Z0-9_]* {return TIDENTIFIER;}
[0-9]+ {yylval = std::stoi(yytext);return TINTEGER;}
"=" {return TASSIGN;}
"==" {return TEQ;}
"!=" {return TNE;}
"<" {return TLT;}
"<=" {return TLE;}
">" {return TGT;}
">=" {return TGE;}
"(" {return TLPAREN;}
")" {return TRPAREN;}
"{" {return TLBRACE;}
"}" {return TRBRACE;}
"," {return TCOMMA;}
";" {return TSEMICOL;}
"+" {return TPLUS;}
"-" {return TMINUS;}
"*" {return TMUL;}
"/" {return TDIV;}
<<EOF>> {return TNONE;}
. {fprintf(stderr,"Unknown token '%s' in line %d!\n",yytext,yylineno); yyterminate();}
%%
int main(){
int token;
while(TNONE!=(token=yylex())){
printf("read '%s' of length %d -- which is of type %d (value is %d)\n",yytext,yyleng,token,yylval);
}
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment