![]() |
![]() |
|
where Num is any numeric literal (as written in either Java or Racket, your choice2). For the provided parsers to work, whitespace is required between all terminals with the exception of punctuation.Expr ::= Num | Paren | BinOp | IfZero Paren ::= < Expr > Interpretation: a parenthesized expression BinOp ::= [ Op Expr Expr ] Interpretation: apply a binary operator Op ::= add | sub | mul Interpretation: addition, subtraction, multiplication (resp.) IfZero ::= zero Expr ? Expr : Expr Interpretation: if 1st-expr is 0, eval to 2nd-expr, else 3rd-expr. Mnemonic:Z0:
branch iff … zero,ok? … else if :t: …Op ::= … | mod Interpretation: “exponentiation”, “remainder1” Expr ::= … | IfGT Interpretation: “if greater than” IfGT ::= grtr Expr Expr ? Expr : Expr Interpretation: if first Expr is greater than second, result is the third Expr else the fourth.Z1:see ../Z2.rkt for details
Expr ::= … | Id | LetExpr Interpretation: identifier; let LetExpr ::= let Id := Expr in Expr Interpretation: bind Id to (result of) first Expr; then eval 2nd Expr w/ that bindingZ2:see ../Z2.rkt for details
Expr ::= … | FuncExpr | FuncApplyExpr FuncExpr ::= fun Id -> Expr Interpretation: a function-value, with parameter Id and body-Expr. FuncApplyExpr ::= pass Expr |> Expr Interpretation: apply a function (2nd expr) to an argument (1st expr)Z4:see ../Z4.rkt for details
Semantics (interpretation):
Some similar(??) languages:
Note: Download the racket files, and then choose Open… from DrRacket. Do not copy/paste into an empty DrRacket window, since that window is probably using a student-language, and some of the files below use full racket.
Got JDK17?: If you have Java JDK 17 installed, records can simplify our classes: Z0-java17/ (browse the Java17 directory), or see the Java17 .jar.
Once we've talked in class about internal-representation (and given examples of the W programs and corresponding internal-data), then we can discuss the provided-implementation, including recursive-descent parsing:
2018 version: The videos below are based on 2018fall's language, T0. So details of the syntax are different than this semester, and some statements might have different semantics, but overall the content is extremely similar to this semester's language.
todo-ibarland: Post the 2021-spring Z0-java/ upload version that uses lambda in BinOp#eval?
Note: Parsing Z0 is particularly easy because each expression-type's leading-token lets us know exactly what to expect, removing the need for look-ahead, backtracking, or cleverer algorithms.
Similarly, if using Java, the semantics of Z0's arithmetic will be similar to IEEE floating point arithmetic (rather than perfectly-correct arithmetic).
Don't confuse Z0's class Num (which extends Expr) with the existing java.lang.Number, which doesn't extend Expr.
↩This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |