Skip to content

Commit e5e90b0

Browse files
committed
[CMPT 379] Add notes
1 parent c7a1b14 commit e5e90b0

19 files changed

+1298
-0
lines changed
Binary file not shown.
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
\documentclass[10pt, oneside, letterpaper, titlepage]{article}
2+
3+
\usepackage{amsmath}
4+
\usepackage[ampersand]{easylist}
5+
\ListProperties(
6+
Progressive*=5ex,
7+
Space=5pt,
8+
Space*=5pt,
9+
Style1*=\textbullet\ \ ,
10+
Style2*=\begin{normalfont}\begin{bfseries}\textendash\end{bfseries}\end{normalfont} \ \ ,
11+
Style3*=\textasteriskcentered\ \ ,
12+
Style4*=\begin{normalfont}\begin{bfseries}\textperiodcentered\end{bfseries}\end{normalfont}\ \ ,
13+
Style5*=\textbullet\ \ ,
14+
Style6*=\begin{normalfont}\begin{bfseries}\textendash\end{bfseries}\end{normalfont}\ \ ,
15+
Style7*=\textasteriskcentered\ \ ,
16+
Style8*=\begin{normalfont}\begin{bfseries}\textperiodcentered\end{bfseries}\end{normalfont}\ \ ,
17+
Hide1=1,
18+
Hide2=2,
19+
Hide3=3,
20+
Hide4=4,
21+
Hide5=5,
22+
Hide6=6,
23+
Hide7=7,
24+
Hide8=8 )
25+
\usepackage[T1]{fontenc}
26+
\usepackage{forest}
27+
\usepackage{geometry}
28+
\geometry{margin=1.2in}
29+
\usepackage{graphicx}
30+
\graphicspath{ {img/} }
31+
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}
32+
\usepackage{listings}
33+
\usepackage{lmodern} % Allows the use of symbols in font size 10; http://ctan.org/pkg/lm
34+
\usepackage{multirow}
35+
\usepackage{textcomp} % Allows the use of \textbullet with the font
36+
\usepackage{verbatim}
37+
38+
\renewcommand{\arraystretch}{1.2}
39+
\renewcommand{\familydefault}{\sfdefault}
40+
41+
\begin{document}
42+
43+
\section{LL(1) Parsing}
44+
\subsection{Introduction}
45+
\begin{easylist}
46+
47+
& \textbf{LL(1) parsing:} Top-down parsing algorithm which creates a left-to-right leftmost derivation requiring only 1 symbol of lookahead
48+
&& Requires a grammar which is:
49+
&&& Unambiguous
50+
&&& Left-factored
51+
&&& Free of left-recursion
52+
&& Only one possible production can exist given the next token
53+
54+
& Grammar validity:
55+
&& A grammar is LL(1) if and only if, for each pairing of a non-terminal symbol with a terminal symbol, only one possible production rule can be applied
56+
&& A grammar is not LL(1) if and only if there exists at least one pairing of a non-terminal symbol and a terminal symbol which can expand to multiple production rules (e.g. $A \rightarrow bc; A \rightarrow bd$)
57+
58+
\end{easylist}
59+
\subsection{Predictive Parsing Tables}
60+
\begin{easylist}
61+
62+
& \textbf{Predictive parsing table:} Table which correlates the leftmost non-terminal symbol and the next terminal symbol with the only possible production
63+
&& No cell can have more than one string as the expansion to the rule
64+
&& Empty cells mean parsing errors
65+
&& Uses lookahead to resolve conflicts between multiple possible production rule applications for the same symbol
66+
&& Layout: See figure~\ref{tab:predictive-parsing-table-layout}
67+
68+
\end{easylist}
69+
\begin{figure}[!htb]
70+
\caption{Predictive Parsing Table Layout}
71+
\label{tab:predictive-parsing-table-layout}
72+
\begin{center}
73+
\begin{tabular}{ r r | l l }
74+
& & \textbf{Terminal symbols} & \$ \\
75+
\hline
76+
\textbf{Non-terminal Symbols} & \textit{Symbol} & \textit{Production result}
77+
\end{tabular}
78+
\end{center}
79+
\end{figure}
80+
\begin{easylist}
81+
82+
& To create a predictive parsing table:
83+
&& For each non-terminal symbol $X$ on the left column:
84+
&&& If there is a production rule where $X$ expands to epsilon ($\epsilon$), then:
85+
&&&& For each terminal symbol which is in the $FOLLOW$ of symbol $X$, write $\epsilon$
86+
&&& If there is a production rule where $X$ expands to a string which is not epsilon ($\epsilon$), then:
87+
&&&& For each terminal symbol which is in the $FIRST$ of symbol $X$, write the expansion of the valid production rule
88+
89+
& E.g. In figure~\ref{tab:ll1-conflict-resolution}, $Y \rightarrow \epsilon$ is the valid production rule to be chosen when $+, ),$ or $\$$ are the lookahead symbol
90+
91+
\end{easylist}
92+
\begin{figure}[!htb]
93+
\caption{LL(1) Conflict Resolution with $FOLLOW$}
94+
\label{tab:ll1-conflict-resolution}
95+
\begin{center}
96+
\begin{tabular}{ r l }
97+
Production Rules
98+
& $E \rightarrow TX$ \\
99+
& $X \rightarrow \epsilon$ \\
100+
& $X \rightarrow +E$ \\
101+
& $T \rightarrow (E)$ \\
102+
& $T \rightarrow id Y$ \\
103+
& $Y \rightarrow *T$ \\
104+
& $Y \rightarrow \epsilon $ \\
105+
\hline
106+
$FOLLOW(Y)$
107+
& $= FOLLOW(T)$ \\
108+
& $= ( FIRST(X) - \{\epsilon\} ) + FOLLOW(E)$ \\
109+
& $= \{ +, ), \$ \}$
110+
\end{tabular}
111+
\end{center}
112+
\end{figure}
113+
\begin{easylist}
114+
115+
\clearpage
116+
117+
\end{easylist}
118+
\subsection{LL(1) Parsing Trace}
119+
\begin{easylist}
120+
121+
& LL(1) parsing trace:
122+
&& Stack is used to keep track of non-terminal symbols
123+
&& Stack and input strings are terminated with the end of input symbol (\$)
124+
&& Layout: See figure~\ref{tab:ll1-parsing-trace-example}
125+
126+
\end{easylist}
127+
\begin{figure}[!htb]
128+
\caption{LL(1) Parsing Trace Example}
129+
\label{tab:ll1-parsing-trace-example}
130+
\begin{center}
131+
\begin{tabular}{ l | l | l }
132+
\textbf{Stack} & \textbf{Input} & \textbf{Action} \\
133+
\hline
134+
\textit{string \$} & \textit{string \$} & \textit{Production rule to expand OR} \\
135+
&& \textit{$\epsilon$ OR} \\
136+
&& \textit{Terminal $\alpha$ OR} \\
137+
&& \textit{acc}
138+
\end{tabular}
139+
\end{center}
140+
\end{figure}
141+
\begin{easylist}
142+
143+
& To create an LL(1) parsing trace:
144+
&& For the leftmost input symbol and the leftmost stack symbol:
145+
&&& If the leftmost stack symbol is a non-terminal symbol, then use the leftmost input and stack symbols to find the corresponding production rule expansion from the predictive parsing table, and:
146+
&&&& If the expansion is not an epsilon, then replace the leftmost stack symbol with the expansion
147+
&&&& If the expansion is an epsilon ($\epsilon$), then remove the leftmost stack symbol
148+
&&& If the leftmost stack symbol is a terminal symbol and the leftmost stack and input symbols match, then consume both of them with the action \textit{Terminal}
149+
&&& If the leftmost stack and input symbols are the end of input (\$), then accept the parse
150+
151+
\end{easylist}
152+
153+
\end{document}
902 KB
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
%
2+
% CMPT 379: Principles of Compiler Design - A Course Overview
3+
%
4+
% Author: Jeffrey Leung
5+
%
6+
7+
\documentclass[10pt, oneside, letterpaper, titlepage]{article}
8+
9+
\usepackage{amsmath}
10+
\usepackage[ampersand]{easylist}
11+
\ListProperties(
12+
Progressive*=5ex,
13+
Space=5pt,
14+
Space*=5pt,
15+
Style1*=\textbullet\ \ ,
16+
Style2*=\begin{normalfont}\begin{bfseries}\textendash\end{bfseries}\end{normalfont} \ \ ,
17+
Style3*=\textasteriskcentered\ \ ,
18+
Style4*=\begin{normalfont}\begin{bfseries}\textperiodcentered\end{bfseries}\end{normalfont}\ \ ,
19+
Style5*=\textbullet\ \ ,
20+
Style6*=\begin{normalfont}\begin{bfseries}\textendash\end{bfseries}\end{normalfont}\ \ ,
21+
Style7*=\textasteriskcentered\ \ ,
22+
Style8*=\begin{normalfont}\begin{bfseries}\textperiodcentered\end{bfseries}\end{normalfont}\ \ ,
23+
Hide1=1,
24+
Hide2=2,
25+
Hide3=3,
26+
Hide4=4,
27+
Hide5=5,
28+
Hide6=6,
29+
Hide7=7,
30+
Hide8=8 )
31+
\usepackage[T1]{fontenc}
32+
\usepackage{forest}
33+
\usepackage{geometry}
34+
\geometry{margin=1.2in}
35+
\usepackage{graphicx}
36+
\graphicspath{ {img/} }
37+
\usepackage[colorlinks=true, linkcolor=blue]{hyperref}
38+
\usepackage{listings}
39+
\usepackage{lmodern} % Allows the use of symbols in font size 10; http://ctan.org/pkg/lm
40+
\usepackage{multirow}
41+
\usepackage{textcomp} % Allows the use of \textbullet with the font
42+
\usepackage{verbatim}
43+
44+
\renewcommand{\arraystretch}{1.2}
45+
\renewcommand{\familydefault}{\sfdefault}
46+
47+
\title{CMPT 379: Principles of Compiler Design \\\medskip \Large A Course Overview}
48+
\author{Jeffrey Leung \\ Simon Fraser University}
49+
\date{Summer 2019}
50+
51+
\begin{document}
52+
53+
\maketitle
54+
\tableofcontents
55+
\clearpage
56+
57+
\input{./tex/introduction.tex}
58+
\input{./tex/lexical-analysis.tex}
59+
\input{./tex/syntax-analysis.tex}
60+
\input{./tex/parsing-algorithms-bottom-up.tex}
61+
\input{./tex/parsing-algorithms-top-down.tex}
62+
\input{./tex/syntax-directed-translation.tex}
63+
\input{./tex/semantic-analysis-ssa.tex}
64+
\input{./tex/programming-language-structure.tex}
65+
\input{./tex/optimization.tex}
66+
\input{./tex/register-allocation.tex}
67+
68+
\end{document}
67.7 KB
Loading
235 KB
Loading
109 KB
Loading
27.4 KB
Loading
214 KB
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
%
2+
% CMPT 379: Principles of Compiler Design - A Course Overview
3+
% Section: Introduction
4+
%
5+
% Author: Jeffrey Leung
6+
%
7+
8+
\section{Introduction}
9+
\label{sec:introduction}
10+
\begin{easylist}
11+
12+
& \textbf{Compiler:} Program which translates a source program from user intention in text form into target in machine code
13+
&& \textbf{Interpreter:} Program which dynamically executes code
14+
15+
& \textbf{Bootstrapping:} Creating a compiler for a language by building a compiler for a simple subset of the language, then using the subset for the rest of the language definition
16+
&& Can be done with a different language
17+
18+
& \textbf{Intermediate representation:} Machine code representation of the program which may be modified and optimized before outputting
19+
20+
& Challenges:
21+
&& Differing architectures, memory hierarchies, memory sizes
22+
&& Instruction and algorithm parallelism
23+
&& Branch prediction
24+
25+
& Stages:
26+
&& \textbf{Analysis (front-end):} Stage of compiling code which reviews and understands the code in lexical, syntax/parsing, and semantic/type-checking contexts
27+
&& \textbf{Synthesis (back-end):} Stage of compiling code which generates and optimizes code
28+
29+
\end{easylist}
30+
\clearpage

0 commit comments

Comments
 (0)