CS453 Lecture Lexical Analysis with JLex 1 Plan for Lexical Analysis with Jlex and One Pass Code Gen Overview of the MeggyJava Assignments PA2:Lexer/scanner in MJPA2.jar Expressing tokens with regular expressions – regular expression syntax for JLex – using JLex with JavaCup How do lexer generators work? – Convert regular expressions to NFA – Converting an NFA to DFA – Implementing the DFA PA2: Syntax-directed code generation (MJ.jar) CS453 Lecture Introduction 2 Structure of the MeggyJava Compiler sentencesSynthesis Analysis character stream lexical analysis wordstokens semantic analysis syntactic analysis AST AST and symbol table code gen Atmel assembly code PA1: Write test cases in MeggyJava, and AVR warmup PA2: MeggyJava scanner and setPixel PA3: add exps and control flow (AST) PA4: add methods (symbol table) PA5: add variables and objects PA6: add arrays and register allocation PA2 Scanner/Lexer Look at the assignment writeup and point out the tar ball. Look at the input files. Look at the output files. Look at MJPA2Driver.java. Look at mj.lex. Look at the Makefile. CS453 Lecture Lexical Analysis with JLex 3 CS453 Lecture Lexical Analysis with JLex 4 Specifying Tokens with JLex JLex example input file: package mjparser; import java_cup.runtime.Symbol; %% %line %char %cup %public %eofval{ return new Symbol(sym.EOF, new TokenValue("EOF", yyline, yychar)); %eofval} LETTER=[A-Za-z] DIGIT=[0-9] UNDERSCORE="_" LETT_DIG_UND={LETTER}|{DIGIT}|{UNDERSCORE} ID={LETTER}({LETT_DIG_UND})* ... %% "&&" {return new Symbol(sym.AND, new TokenValue(yytext(), yyline, yychar)); } "+" {return new Symbol(sym.PLUS, ...); } "if" {return new Symbol(sym.IF,...); } {ID} {return new Symbol(sym.ID, new ... {EOL} { /* reset yychar */ … } {WS} { /* ignore */ }
9
Embed
Plan for Lexical Analysis with Jlex and One Pass Code Gen ...cs453/yr2013/Slides/...CS453 Lecture Lexical Analysis with JLex 1 Plan for Lexical Analysis with Jlex and One Pass Code
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CS453 Lecture Lexical Analysis with JLex 1
Plan for Lexical Analysis with Jlex and One Pass Code Gen
Overview of the MeggyJava Assignments
PA2:Lexer/scanner in MJPA2.jar
Expressing tokens with regular expressions – regular expression syntax for JLex – using JLex with JavaCup
How do lexer generators work? – Convert regular expressions to NFA – Converting an NFA to DFA – Implementing the DFA
PA1: Write test cases in MeggyJava, and AVR warmup PA2: MeggyJava scanner and setPixel PA3: add exps and control flow (AST) PA4: add methods (symbol table) PA5: add variables and objects PA6: add arrays and register allocation
PA2 Scanner/Lexer
Look at the assignment writeup and point out the tar ball.
Look at the input files.
Look at the output files.
Look at MJPA2Driver.java.
Look at mj.lex.
Look at the Makefile.
CS453 Lecture Lexical Analysis with JLex 3 CS453 Lecture Lexical Analysis with JLex 4
Specifying Tokens with JLex JLex example input file:
package mjparser; import java_cup.runtime.Symbol;
%% %line %char %cup %public
%eofval{ return new Symbol(sym.EOF, new TokenValue("EOF", yyline, yychar)); %eofval}
Semantic Rules for Expression Example Code Generation versus Interpretation
When interpreting . . . – Each action in the .cup file associates a value with the left hand side of the
non terminal. – Each non terminal on the right hand side has a value associated with it. – This approach will also be useful when we are building the Abstract
Syntax Tree (AST) in PA3.
When doing one pass compilation . . . – Actions output the target code (in this case AVR assembly)