cs453 spring 12 quiz 2 predictive parsing 1 given an
play

CS453 Spring 12 Quiz 2 Predictive Parsing 1. Given - PDF document

CS453 Spring 12 Quiz 1 Regular Expressions and Finite State Machines 1. Build an NFA for BoolLits and intLits, given the following regular


  1. CS453 ¡Spring ¡12 ¡Quiz ¡1 ¡ Regular ¡Expressions ¡and ¡Finite ¡State ¡Machines ¡ ¡ 1. ¡Build ¡an ¡NFA ¡for ¡BoolLits ¡and ¡intLits, ¡given ¡the ¡following ¡regular ¡definitions: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ dig ¡: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡[0-­‑9] ¡ BoolDig: ¡ ¡ ¡[01] ¡ BoolLit ¡: ¡ ¡ ¡BoolDig+ ¡ ¡ intLit ¡: ¡ ¡ ¡ ¡ ¡ ¡ ¡dig+ ¡ ¡ Priority: ¡if ¡a ¡literal ¡consists ¡of ¡0-­‑s ¡and ¡1-­‑s ¡only, ¡it ¡is ¡a ¡BoolLit ¡ ¡ ¡ 2. ¡Now ¡transform ¡the ¡NFA ¡to ¡a ¡DFA ¡ ¡ CS453 ¡Spring ¡12 ¡Quiz ¡2 ¡ Predictive ¡Parsing ¡ ¡ 1. ¡ ¡Given ¡an ¡augmented, ¡grammar ¡for ¡statements: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡P ¡ à ¡{ ¡L ¡} ¡$ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L ¡ à ¡S ¡ ¡L ¡| ¡ ε ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡ à ¡if ¡ ¡t ¡{ ¡L ¡} ¡| ¡while ¡t ¡{ ¡L ¡} ¡| ¡id ¡= ¡num ¡; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ where ¡P ¡stands ¡for ¡program, ¡L ¡for ¡list ¡ ¡of ¡ ¡statements, ¡and ¡S ¡stands ¡for ¡statement ¡ ¡ a) ¡Produce ¡NULLABLE, ¡FIRST, ¡and ¡FOLLOW ¡ ¡ b) ¡Produce ¡the ¡predictive ¡parse ¡table. ¡ ¡ c) ¡Show ¡the ¡parse ¡tree ¡obtained ¡by ¡predictive ¡parsing: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡while ¡t ¡{ ¡ ¡if ¡ ¡t ¡ ¡{ ¡ ¡id ¡= ¡num ¡; ¡id ¡= ¡num ¡; ¡} ¡} ¡}$ ¡ ¡ CS453 ¡Spring ¡12 ¡Quiz ¡3 ¡ LR(0) ¡ ¡Parsing ¡ 1. ¡ ¡Given ¡an ¡augmented, ¡grammar ¡for ¡single ¡bracket ¡nests: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0: ¡ ¡S’ ¡ à ¡S ¡$ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡1: ¡ ¡S ¡ ¡ à ¡( ¡S ¡ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡2: ¡ ¡S ¡ à ¡id ¡ ¡ a) ¡Show ¡a ¡rightmost ¡derivation ¡of ¡ ¡ ¡ ¡ ¡ ¡(( ¡id ¡)) ¡$ ¡ ¡ b) ¡Show ¡a ¡bottom-­‑up ¡parse ¡of ¡(( ¡id ¡)) ¡$ ¡ ¡ ¡ ¡ ¡(Stack ¡with ¡Grammar ¡Symbols, ¡ ¡ ¡ ¡Input ¡and ¡ ¡ ¡ ¡ Actions), ¡based ¡on ¡its ¡ ¡rightmost ¡derivation ¡ ¡ ¡ Stack ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Input ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Action ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡( ¡( ¡id ¡) ¡) ¡$ ¡ ¡ c) ¡Produce ¡the ¡LR(0) ¡State ¡Diagram ¡for ¡the ¡above ¡grammar ¡ ¡ d) ¡Produce ¡the ¡LR(0) ¡parse ¡table. ¡ ¡ e) ¡Produce ¡the ¡LR(0) ¡parse ¡with ¡Stack ¡(states, ¡Grammar ¡symbols) ¡ ¡Input, ¡and ¡ ¡ ¡ ¡ ¡Action ¡ ¡ ¡of ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ( ¡( ¡id ¡) ¡) ¡$ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡driven ¡by ¡the ¡LR(0) ¡parse ¡table ¡ ¡

  2. CS453 ¡Spring ¡12 ¡Quiz ¡4 ¡ AST ¡and ¡Symbol ¡Table ¡ /* ¡ ¡* ¡CS453 ¡Quiz ¡4 ¡ ¡* ¡ ¡draw ¡the ¡AST ¡and ¡ ¡Symbol ¡Table ¡ ¡after ¡checkTypes ¡and ¡allocVars ¡(base,offset) ¡ ¡*/ ¡ class ¡PA5FunSUM ¡{ ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String[] ¡whatever){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Meggy.setPixel(new ¡C().Sum(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_i; ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ } ¡ ¡ Draw ¡the ¡Run ¡Time ¡Stack ¡and ¡the ¡Heap, ¡after ¡ ¡oy.put(b) ¡has ¡executed ¡but ¡not ¡ returned. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  3. CS453 ¡Spring ¡12 ¡Quiz ¡6 ¡ Memory ¡map: ¡ ¡array ¡descriptors ¡ ¡ 1a) ¡ ¡assuming ¡that ¡the ¡first ¡array ¡element ¡is ¡allocated ¡at ¡address ¡2000, ¡and ¡that ¡ints ¡ occupy ¡2 ¡bytes, ¡create ¡the ¡array ¡descriptor ¡for ¡Sq. ¡ ¡ ¡ 1b) ¡create ¡the ¡array ¡descriptor ¡for ¡the ¡sub ¡array ¡D ¡of ¡Sq ¡consisting ¡of ¡the ¡diagonal ¡ elements ¡of ¡Sq: ¡ ¡D[i] ¡= ¡Sq[i ¡, ¡i]. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ CS453 ¡Spring ¡12 ¡Quiz ¡7 ¡ Register ¡allocation ¡ 1. ¡ ¡Given ¡the ¡following ¡expression: ¡ ¡ (a+b)/(c+1)-­‑(d+2)*(e+f) ¡ ¡ Show ¡how ¡to ¡efficiently ¡allocate ¡registers ¡for ¡this ¡expression. ¡ ¡ 2. ¡Given ¡the ¡following ¡3 ¡address ¡code ¡fragment: ¡ ¡ 1: ¡ a ¡= ¡1 ¡ L1: ¡ 2: ¡ c ¡= ¡a ¡+ ¡5 ¡ 3: ¡ b ¡= ¡c ¡* ¡2 ¡ 4: ¡ a ¡= ¡b+5 ¡ 5: ¡ if ¡a ¡< ¡20 ¡goto ¡L1 ¡ 6: ¡ return ¡c ¡ ¡ a) ¡Draw ¡the ¡control ¡flow ¡graph ¡(with ¡nodes ¡that ¡are ¡single ¡statements) ¡ ¡ b) ¡For ¡ ¡a, ¡b ¡and ¡c ¡show ¡where ¡they ¡are ¡live ¡ ¡(on ¡which ¡edges) ¡ ¡ c) ¡What ¡is ¡the ¡minimal ¡number ¡of ¡registers ¡needed ¡for ¡this ¡code? ¡ ¡Explain ¡ ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend