Meeting 07 - Concrete Syntax

Bor-Yuh Evan Chang

Tuesday, September 17, 2024

\(\newcommand{\TirName}[1]{\text{#1}} \newcommand{\inferrule}[3][]{ \let\and\qquad \begin{array}{@{}l@{}} \TirName{#1} \\ \displaystyle \frac{#2}{#3} \end{array} } \newcommand{\infer}[3][]{\inferrule[#1]{#2}{#3}} \)

Meeting 07 - Concrete Syntax

What questions does your neighbor have?

In-Class Slides
Book Chapter

Announcements

  • HW 2 due this Friday 9/20 6pm
    • Jupyter assignment

Today

  • Preview HW 2
  • Concrete Syntax
  • Triage Your Questions
    • Lab 1?
  • Revisit and Go Deeper On:
    • Inductive Data Types (Meeting 06), if time permits

Questions?

  • Review:
    • What’s an inductive data type?

Questions?

What is the purpose of a programming language specification?

Who uses a language specification?

What is the difference between syntax and semantics?

Can a given syntax have two different semantics (in different languages)?

What is the difference between concrete and abstract syntax?

Concrete Syntax

Formal Language Terminology

In formal language theory, a language \(L\) is …

Formal Language Terminology

A sentence is …

Grammars

A (context-free) grammar describes …








BNF (Backus-Naur Form) is …

Grammars

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\).

Lexical versus Syntactic

A lexeme is …








A token is …

Generators versus Recognizers

Example Grammars

\[ e \mathrel{::=}n \mid e + e \mid e * e \]

Parsing Derivations

Give a parsing derivation for 1 + 2 * 3 with \(e \mathrel{::=}n \mid e + e \mid e * e\).

Parse Trees

Give a parse tree for 1 + 2 * 3 with \(e \mathrel{::=}n \mid e + e \mid e * e\).

Ambiguity

What should 1 + 2 * 3 evaluate to (i.e., what is the semantics of 1 + 2 * 3)?

Ambiguity

Give another parsing derivation for 1 + 2 * 3 with \(e \mathrel{::=}n \mid e + e \mid e * e\).

Ambiguity

Give the corresponding parse tree as the previous derivation for 1 + 2 * 3 with \(e \mathrel{::=}n \mid e + e \mid e * e\).

Resolving Ambiguity: Associativity

\[ e \mathrel{::=}n \mid e - e \]

Resolving Ambiguity: Precedence

\[ e \mathrel{::=}n \mid n + e \mid n * e \]

Resolving Ambiguity: Precedence

Consider the ambiguous grammar again:

\[ e \mathrel{::=}n \mid e + e \mid e * e \]

Bonus: Resolving Ambiguity

\[ \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 Grammars

Write a grammar for a language that has the string \(\texttt{aa}\).

Write Grammars

Write a grammar for a language that has an even number of \(\texttt{a}\)’s.

Write Grammars

Write a grammar for a language that has an odd number of \(\texttt{a}\)’s.

Write Grammars

Write a grammar that generates matching open and close braces { }.