![]() |
![]() |
|
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.
Java 17 records: Remember: records are just classes where you don't have to declare the constructor, nor override .equals and .hashCode. All fields are public and immutable.
Expr ::= Num | Paren | BinOp | IfZero | Nop Paren ::= o Expr o Interpretation: a parenthesized expression BinOp ::= ring Expr bearer Expr Op Interpretation: apply a binary operator Op ::= frodo | gollum | samwise Interpretation: addition, subtraction, multiplication (resp.) IfZero ::= for Expr gondor Expr Expr Interpretation: if 3rd-expr is 0 (within 1e-6), eval to 1st-expr, else 2nd-expr.D0:
Op ::= … | sauron Interpretation: “remainder1” Expr ::= … | IfLE Interpretation: “if less-or-equal to” IfLE ::= you Expr shall Expr not Expr pass Expr Interpretation: if 1st Expr is ≤ 2nd, result is the 3rd Expr else the 4th.D1:see D2.html for details
Expr ::= … | Id | LetExpr Interpretation: identifier; let LetExpr ::= bind Id to Expr in Expr Interpretation: bind Id to result of 1st Expr (the “right-hand-side”); then eval 2nd “body” Expr w/ that bindingD2:see D2.html for details
Expr ::= … | FuncExpr | FuncApplyExpr FuncExpr ::= forge Id into Expr Interpretation: a function-value, with parameter Id and body-Expr. FuncApplyExpr ::= wield Expr against Expr Interpretation: apply a function (1st expr) to an argument (2nd expr)D4:see D4.html for details
Num is any numeric literal
(as written in either Java or Racket, your choice2).
We'll assume that Ids are not one of our D0 “reserved” words
(like you
— i.e. a terminal in the above grammar);
you don't need to check/enforce that in your code.
Some similar(??) languages:
- emojicode
- Arnold[Schwarzenegger]C
- Genesis, in Old Hebrew
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 Y0-java/ upload version that uses lambda in BinOp#eval?
Note: Parsing D0 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 D0's arithmetic will be similar to IEEE floating point arithmetic (rather than perfectly-correct arithmetic).
Don't confuse D0'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 ![]() |