

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These are the Lecture Slides of Compiler Design which includes Representing Types, Type Equality, Type Coercions, Kinds of Type Systems, Primitive Types, Inherited Attributes, Computing Attribute Computations, Describing Type Systems etc. Key important points are: String Lexers, File Lexers, Handling Exceptions, Library Functions, Anonymous Functions, Compile Manager, Full Pathname, Building Lexer, Catching Exceptions in Lexer, Lexer Interactively
Typology: Slides
1 / 41
This page cannot be seen from the preview
Don't miss anything!


































type lexresult = unit; type pos = int; type svalue = int; exception EOF; fun eof () = (print "eof"; raise EOF); %% %% [\t\ ]+ => ( lex() ( ignore whitespace ) ) ; Anne|Bob|Spot => ( print (yytext^": is a proper noun\n")); a|the => ( print(yytext^": is an article\n") ); boy|girl|dog => ( print(yytext^": is a noun\n") ); walked|chased|ran|bit => ( print(yytext^": is a verb\n") ); [a-zA-Z]+ => ( print(yytext^": Might be a noun?\n") ); .|\n => ( print yytext ( Echo the string ) ); lexresult must be defined in every lex program The function eof must be defined in every lex program
val testString = ref "the boy chased the dog"; fun feed n = let val ss = !testString in if String.size ss < n then ( testString := ""; ss ) else ( testString := String.extract(ss,n,NONE); String.extract(ss,0,SOME n) ) end; val lex = Mlex.makeLexer feed;
val lex = let val f = Mlex.makeLexer feed fun lex () = (f ()) handle Mlex.UserDeclarations.EOF => print "\nReached end of file\n" | other => print "Lex Error" in lex end; Things defined in the first section of a lex file appear in the inner library
val lex = Mlex.makeLexer (inputc "test.english");
type lexresult = Token type pos = int; type svalue = int; fun eof () = EOF; %% digit=[0-9]; number={digit}+ ; %% [\t\ ]+ => ( lex() ) ; Anne|Bob|Spot| {number} => ( ProperNoun yytext ); a|the => ( Article yytext ); boy|girl|dog => ( Noun yytext ); walked|chased|ran|bit => ( Verb yytext ); We can name a RE Name = RE Semicolon Use a name by surrounding it in { }s english2.lex Note that all unrecognized input will raise the default exception. Not sure why we need pos and svalue
fun addone [] = [] | addone (x::xs) = (x + 1) :: addone xs fun stringy [] = [] | stringy (x::xs) = (Int.toString x) :: stringy xs fun negL [] = [] | negL (x::xs) = (not x) :: negL xs
val ex1 = map (fn x => (x+1)) [2,3,4]; val ex1 = [3,4,5] : int list val ex2 = map Int.toString [2,5,7]; val ex2 = ["2","5","7"] : string list val ex3 = map not [true, 3 > 4]; val ex3 = [false,true] : bool list