13.07.2015 Views

SMD135 - lect 17 - approx chap. 9.6-9.8 Today ... - Sm.luth.se

SMD135 - lect 17 - approx chap. 9.6-9.8 Today ... - Sm.luth.se

SMD135 - lect 17 - approx chap. 9.6-9.8 Today ... - Sm.luth.se

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

135 - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong><strong>SMD135</strong> - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong>✞✝<strong>SMD135</strong> - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong>☎✆✞<strong>Today</strong> - Parsing✝☎✆• Accept of boolean expressionsJohan Karlsson• Expression tree• Generating an expression treeEmail: Johan.Karlsson@SM.LUTH.SE• Adding operationsil: Johan.Karlsson@SM.LUTH.SE 1Email: Johan.Karlsson@SM.LUTH.SE 2135 - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong><strong>SMD135</strong> - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong>✞Boolean Expression✝< bexpr > ::= < bterm > ”||” < bexpr > | < bterm >< bterm > ::= < bfactor > ”&&” < bterm > | < bfactor >< bfactor > ::= ”!” < bfactor > | ”(”< bexpr >”)” | < boolconst >< boolconst > ::= ”true” | ”fal<strong>se</strong>”Can be rewritten as on the next slide.☎✆✞Boolean Expression, LL(1) grammar✝< bexpr > ::= < bterm > < bexpr ′ >< bexpr ′ > ::= ”||” < bterm > < bexpr ′ > | ǫ< bterm > ::= < bfactor > < bterm ′ >< bterm ′ > ::= ”&&” < bfactor > < bterm ′ > | ǫ< bfactor > ::= ”!” < bfactor > | ”(”< bexpr >”)” | < boolconst >< boolconst > ::= ”true” | ”fal<strong>se</strong>”See handout for par<strong>se</strong>r/accept code.☎✆il: Johan.Karlsson@SM.LUTH.SE 3Email: Johan.Karlsson@SM.LUTH.SE 4


135 - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong><strong>SMD135</strong> - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong>✞Expression Tree✝☎✆&&✞Generating and Expression Tree✝• A build menthod instead of an accept☎✆• It should return an expression• Each operations is an expression• U<strong>se</strong> a tree to repre<strong>se</strong>nt the expression• Unambiguous• Fig <strong>9.8</strong>true ||fal<strong>se</strong>!&&true• Each terminal is an expression• See BoolExpr in the handoutsfal<strong>se</strong>true&&(fal<strong>se</strong> ||!fal<strong>se</strong>&&true)il: Johan.Karlsson@SM.LUTH.SE 5Email: Johan.Karlsson@SM.LUTH.SE 6135 - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong><strong>SMD135</strong> - <strong>lect</strong> <strong>17</strong> - <strong>approx</strong> <strong>chap</strong>. <strong>9.6</strong>-<strong>9.8</strong>✞Adding Operations✝• We add the binary operation #• and the unary operation #< bexpr > ::= < bterm > < bexpr ′ >< bexpr ′ > ::= ”||” < bterm > < bexpr ′ > |”#” < bterm > < bexpr ′ > | ǫ< bterm > ::= < bfactor > < bterm ′ >< bterm ′ > ::= ”&&” < bfactor > < bterm ′ > | ǫ< bfactor > ::= ”!” < bfactor > | ”#” < bfactor > |”(”< bexpr >”)” | < boolconst >< boolconst > ::= ”true” | ”fal<strong>se</strong>”☎✆✞More additions✝• Expressions may contain identifiers (vairables)• The evaluation method then has to know about an environment(e.g., boolean value(Env env)) in which one can lookup variablesto get the corresponding value• Arithmetic expressions (+, −, ∗, /)• Combining boolean and arithmetic expressoins– , =, ==, ! =☎✆il: Johan.Karlsson@SM.LUTH.SE 7Email: Johan.Karlsson@SM.LUTH.SE 8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!