building ast's for rpal programs
DESCRIPTION
Building AST's for RPAL Programs. Programming Language Concepts Lecture 14. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. Let's Build a Few Abstract Syntax Trees. Example 1: Factorial, version 3. Example 2: Palindrome. Example 3: Add numbers from list. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/1.jpg)
Building AST's for RPAL Programs
Prepared by
Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida
Programming Language ConceptsLecture 14
![Page 2: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/2.jpg)
Let's Build a Few Abstract Syntax Trees
• Example 1: Factorial, version 3.
• Example 2: Palindrome.
• Example 3: Add numbers from list.
• Example 5: Build tuple of pairs of characters.
![Page 3: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/3.jpg)
![Page 4: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/4.jpg)
![Page 5: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/5.jpg)
![Page 6: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/6.jpg)
![Page 7: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/7.jpg)
Class Project
• Build a recursive descent parser for RPAL (gulp!)
• If done properly, it's easy.• Need lexical analyzer, to be called
repeatedly (important!) by the parser. Avoid scanning the entire input in advance of the parser.
![Page 8: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/8.jpg)
Class Project (cont’d)
• Code up one procedure per nonterminal in RPAL's grammar.
• Pseudo code for some portions of RPAL’s grammar
![Page 9: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/9.jpg)
![Page 10: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/10.jpg)
![Page 11: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/11.jpg)
![Page 12: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/12.jpg)
Class Project (cont’d)
• Implement support module for trees, and stack of trees.• Suggestion: use first-child, next-
sibling binary tree representation of n-ary trees (it works!).
• Preorder traversal of this tree is the same as for n-ary tree.
• Popping n trees from the stack, and building a parent node on top of them, is easy.
![Page 13: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/13.jpg)
![Page 14: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/14.jpg)
![Page 15: Building AST's for RPAL Programs](https://reader035.vdocuments.net/reader035/viewer/2022062500/56814fcb550346895dbd8d25/html5/thumbnails/15.jpg)
Building AST's for RPAL Programs
Prepared by
Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida
Programming Language ConceptsLecture 14