LMU ☀️ CMSI 3802
LANGUAGES AND AUTOMATA II
HOMEWORK #1 PARTIAL ANSWERS
  1. let [x, y] = [0, 0];
    console.log(93.8 * 2 ** x + y);
    

    js-ast-4.png

  2. import x from "x"
    console.log(93.8 * {x} << x.r[z])
    

    js-ast-1.png

  3. x / {[x]: `${x}`}[x]
    

    js-ast-2.png

  4. class C {
      f({a}) {return ([a,f]) => C}
    }
    

    js-ast-3.png

  5. The expression -2**2
    1. Will ultimately evaluate to 4 if negation has higher precedence than exponentiation:
      Exp     = Exp "+" Term | Term
      Term    = Term "*" Factor | Factor
      Factor  = Primary "**" Factor | Primary
      Primary = "-" Primary | num | id | "(" Exp ")"
      
    2. Will ultimately evaluate to -4 if negation has lower precedence than exponentiation:
      Exp     = "-"? (Exp "+" Term | Term)
      Term    = Term "*" Factor | Factor
      Factor  = Primary "**" Factor | Primary
      Primary = num | id | "(" Exp ")"
      
    3. Will be a syntax error if negation has the same precedence as exponentiation:
      Exp     = Exp "+" Term | Term
      Term    = Term "*" Factor | Factor
      Factor  = Primary "**" Factor | "-"? Primary
      Primary = num | id | "(" Exp ")"
      
  6. Here is a grammar for Astro++ that enforces the appearance of break statements only within while loops. (In practice, most people don't actually capture this in the grammar, but it's a good exercise to actually do it!)
    Astro {
      Program       = StmtNoBreak+
      StmtNoBreak   = Assignment
                    | PrintStmt
                    | IfStmtNoBreak
                    | WhileStmt
      Stmt          = Assignment
                    | PrintStmt
                    | IfStmt
                    | WhileStmt
                    | BreakStmt
      Assignment    = id "=" Exp ";"
      PrintStmt     = print Exp ";"
      BreakStmt     = break ";"
      WhileStmt     = while Exp Block
      IfStmtNoBreak = if Exp BlockNoBreak else BlockNoBreak
                    | if Exp BlockNoBreak else IfStmtNoBreak
                    | if Exp BlockNoBreak
      IfStmt        = if Exp Block else Block
                    | if Exp Block else IfStmt
                    | if Exp Block
      BlockNoBreak  = "{" StmtNoBreak* "}"
      Block         = "{" Stmt* "}"
      Exp           = Exp1 ("<=" | "<" | "==" | "!=" | ">=" | ">") Exp1  --relational
                    | Exp1
      Exp1          = Exp1 ("+" | "-") Term           --additive
                    | Term
      Term          = Term ("*" | "/" | "%") Factor   --multiplicative
                    | Factor
      Factor        = Primary "**" Factor             --exponentiation
                    | "-" Primary                     --negation
                    | Primary  
      Primary       = id "(" ListOf<Exp, ","> ")"     --call
                    | numeral                         --num
                    | id                              --id
                    | "(" Exp ")"                     --parens
      numeral       = digit+ ("." digit+)? (("E" | "e") ("+" | "-")? digit+)?
      print         = "print" ~idchar
      while         = "while" ~idchar
      if            = "if" ~idchar
      else          = "else" ~idchar
      break         = "break" ~idchar
      keyword       = print | while | if | else | break
      idchar        = letter | digit | "_"
      id            = ~keyword letter idchar*
      space        += "//" (~"\n" any)*               --comment
    }