1 / 25

Lecture 6: Building a Compiler – Lessons in Good design

CSC 313 – Advanced Programming Topics. Lecture 6: Building a Compiler – Lessons in Good design. Today’s Goal. Make you forget reading that was assigned I went back & reviewed others; none like this one Highlight process used by modern compilers Consider steps taken & how design works

amy
Download Presentation

Lecture 6: Building a Compiler – Lessons in Good design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 313 – Advanced Programming Topics Lecture 6:Building a Compiler –Lessons in Good design

  2. Today’s Goal • Make you forget reading that was assigned • I went back & reviewed others; none like this one • Highlight process used by modern compilers • Consider steps taken & how design works • Where optimizations occur will be discussed • Review a kick-ass design honed through years • Understand decisions to see good design in action • See design process that results in design patterns

  3. Translations • Translating docs from English to other language • But all of this material selected from college library • Undecipherable & meaningless documents included • Workers highly-trained & so saving time (& $$) critical • Reject untranslatable documents & explain why • Need to give workers chance to correct mistakes • Money important, so limit time on these documents

  4. Translations • Translating docs from English to other language • But all of this material selected from college library • Undecipherable & meaningless documents included • Workers highly-trained & so saving time (& $$) critical • Reject untranslatable documents & explain why • Need to give workers chance to correct mistakes • Money important, so limit time on these documents How can we do this?

  5. What Errors Can Occur?

  6. What Errors Can Occur? • Document read in unreadable or not English • Has “words”, but grammatically incorrect • Meaningless jumble meanscannot translate

  7. What Errors Can Occur? • Document read in unreadable or not English αυηθιμςτϕϖκλσροπ χψτνγδζεεθμςτυ • Has “words”, but grammatically incorrect • Meaningless jumble meanscannot translate

  8. What Errors Can Occur? • Document read in unreadable or not English αυηθιμςτϕϖκλσροπ χψτνγδζεεθμςτυ • Has “words”, but grammatically incorrect 4 u 2 c I sent U pix. ty • Meaningless jumble meanscannot translate

  9. What Errors Can Occur? • Document read in unreadable or not English αυηθιμςτϕϖκλσροπ χψτνγδζεεθμςτυ • Has “words”, but grammatically incorrect 4 u 2 c I sent U pix. ty • Meaningless jumble meanscannot translate Mary flow over the swimming pizza

  10. How To Translate?

  11. How To Translate? BUT WAIT

  12. Money, money, money! • This project useful for many languages • Different alphabetor punctuation may be used • Will need to adjust to different grammar rules • Change in scoping rules & variable declaration types

  13. Money, money, money! • This project useful for many languages • Different alphabetor punctuation may be used • Will need to adjust to different grammar rules • Change in scoping rules & variable declaration types Oops

  14. Money, money, money! • This project useful for many languages • Different alphabetor punctuation may be used • Will need to adjust to different grammar rules • Change in scoping rules & variable declaration types • Could translate to many different languages • (Unless written by Microsoft or in Alabama)

  15. How To Translate?

  16. How To Translate? (Redux) • Split translations process into multiple layers • Lexical analyzer will check legal words & punctuation • Grammar errors found by parser (syntactic analysis) • Semantic analysis will check document meaningful • Optimizations happen here, since program is valid • Code generator “lowers” results into new language

  17. All About the Benjamins • Most layers’ actions language-dependent • Reusing details hard, but algorithms very similar • Between layers, pass language-independent data • Define protocols specifying how layers interact • Each layer independent & can change easily • Limit rewriting, instead reuse whenever possible • Must follow protocols, but these set out clearly • Popular approach: gcc, XCode, Google Translate

  18. Planning Data & Interactions • Data to be passed depends on work layers do • Individual words & punctuation output by lexer • Parser goes through this data & find sentences • Build paragraphs by checking sentences meaningful • Interactionsbased on input size needed • Item-by-item analyses needed to build sentences • Semantic analysis works by using whole module • Entire program needed for optimizations & output

  19. Planning Data & Interactions • Data to be passed depends on work layers do • Individual words & punctuation output by lexer • Parser goes through this data & find sentences • Build paragraphs by checking sentences meaningful • Interactionsbased on input size needed • Item iterationneeded to build sentences • Semantic analysis works by using whole module • Entire program needed for optimizations & output

  20. Resulting Design • Lexer is an Iterator for words & punctuation • Illegal characters found & reported as errors • Parser builds syntax trees for each module • Function, class, or file may be language’s module • Must be conservative: outputs result for entire file • Good syntax tree output by semantic analysis • When use found, variable linked to declaration & type • Match calls with functions & all checked if legal • Result called Higher-order Intermediate Representation

  21. Lexical Analysis In Detail • Creates “tokens” from file being compiled • While it does this, checkscharacters are legal • Keywords identified (for, int, while…) in input • Code parsed to identify literals (strings, numbers) • Does not understand code; only splitting it out • Only layer looking at code, so skips comments

  22. How Does Lexer Work? • Regular expressions defined for items to find • They’re back!! Turned into big finite state machine • Defines enum or hierarchy for returned data struct Symbol { Token type; String value;}enum Token {NUMBER, STRING, WORD, GT, , ... }

  23. Why Use State Machines? • Limits knowledge needed for coding a lexer • Machine has set of states connected by edges • State determines next state, given an input • Need to know initial & accepting states only • States define language; processing unchanged

  24. Why Use State Machines? • Limits knowledge needed for coding a lexer • Machine has set of states connected by edges • State determines next state, given an input • Need to know initial & accepting states only • States define language; processing unchanged • Obviously, this is part of State design pattern (Covered later in term, in case you wondering) • Maximizes reuse of code within lexical analysis • Limits rework to developing regular expressions

  25. For Next Lecture • Read pages 37 – 55 in book • Get back into easier code & design patterns • 1-to-many communication is problem why? • Why is this critical to all event-based coding? • Lindsay Lohan'ssecret to success related to this?

More Related