Created: 2023-05-11 Thu 11:06
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
int factorial(int n) {
return n == 0 ? 1 : n * factorial(n - 1);
}
x[0]
, not x.(0)
for indexing stringsx := 1
and x.f <- 1
type<arg>
type<type<arg>>
>>
>>
an operator, or is it part of a template?
> >
in the template casemaybe_fails(34)?
match maybe_fails(34) {
Ok (x) => x,
Err(e) => return Err(e)
}
(let ((a + 1 (* 2 3))) (/ a 2))
let a = 1 + 2 * 3; a / 2
Text -> Lexical structure -> Syntactic structure -> Abstract syntax
Consider a language that has:
def factorial(n) { if (n == 0) then return 1; else return n * factorial(n - 1); } print(factorial(5));
Let's walk through a grammar for this language.
Expression: compute a value
Atlantic headline: "Susan Collins Unveils a Gun-Control Compromise: It would restrict sales to individuals on two terrorist watch lists"
How should we parse 2 * x + y
?
<expr> ::= <id> | <expr> ("-" | "*") <expr> | "(" <expr> ")"
How to parse x - y - z * z
?
How can we force x - y - z * z
to parse as:
(x - y) - (z * z)
<expr> ::= <expr> "-" <expr> | <expr1> <expr1> ::= <expr1> "*" <expr1> | <expr2> <expr2> ::= <id> | "(" <expr> ")"
<expr> ::= <expr> "-" <expr1> | <expr1> <expr1> ::= <expr1> "*" <expr2> | <expr2> <expr2> ::= <id> | "(" <expr> ")"