Tuesday, September 17, 2024
What questions does your neighbor have?
Can a given syntax have two different semantics (in different languages)?
In formal language theory, a language \(L\) is …
A sentence is …
A (context-free) grammar describes …
BNF (Backus-Naur Form) is …
A grammar consists terminals, non-terminals, and productions:
\[ N \mathrel{::=}\alpha_1 \mid\alpha_2 \mid\cdots \]
where \(N \in \mathcal{N}\) is a non-terminal and \(\alpha \in (\Sigma \cup\mathcal{N})^\ast\).
A lexeme is …
A token is …
\[ e \mathrel{::=}n \mid e + e \mid e * e \]
Give a parsing derivation for 1 + 2 * 3
with \(e \mathrel{::=}n \mid e + e \mid e * e\).
Give a parse tree for 1 + 2 * 3
with \(e \mathrel{::=}n \mid e + e \mid e * e\).
What should 1 + 2 * 3
evaluate to (i.e., what is the semantics of 1 + 2 * 3
)?
Give another parsing derivation for 1 + 2 * 3
with \(e \mathrel{::=}n \mid e + e \mid e * e\).
Give the corresponding parse tree as the previous derivation for 1 + 2 * 3
with \(e \mathrel{::=}n \mid e + e \mid e * e\).
\[ e \mathrel{::=}n \mid e - e \]
\[ e \mathrel{::=}n \mid n + e \mid n * e \]
Consider the ambiguous grammar again:
\[ e \mathrel{::=}n \mid e + e \mid e * e \]
\[ \begin{array}{rrl} s & \mathrel{::=}& \mathit{ifstmt} \mid\cdots \\ \mathit{ifstmt} & \mathrel{::=}& \texttt{if} \; e \; \texttt{then} \; s \\ & \mid& \texttt{if} \; e \; \texttt{then} \; s \; \texttt{else} \; s \end{array} \]
Ambiguous?
Write a grammar for a language that has the string \(\texttt{aa}\).
Write a grammar for a language that has an even number of \(\texttt{a}\)’s.
Write a grammar for a language that has an odd number of \(\texttt{a}\)’s.
Write a grammar that generates matching open and close braces {
}
.