\documentclass{beamer}

\usepackage{beamerthemevictor,comment,verbatim,graphicx,amssymb}
\usepackage[noeepic]{qtree}

\input{tutmacs}
\input{slidemacs}
\input idxmacs

\begin{document}

\title{Parsing}
\author{Victor Eijkhout}
\date{Notes for CS 594 -- Fall 2004}

\frame{\titlepage}

\section{Introduction}

\frame[containsverbatim]{
  \frametitle{What is parsing?}
\begin{itemize}
\item Check for correctness: is this a legal program
\item Uncover meaning: convert to internal representation
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Levels of parsing}
\begin{itemize}
\item Check for illegal characters
\item Build tokens (identifiers, numbers, operators~\&c) from
  characters (lexical analysis)
\item Statements tokens (syntactical analysis)
\item Semantical restrictions: define/use~\&c
\end{itemize}
}

\frame[containsverbatim]{
\begin{verbatim}
    my_array[ii] = 3+sin(1.0);
\end{verbatim}
\begin{itemize}
\item Lexical analysis: `\n{my_array}', `\verb+[+', `\n{ii}'~\&c.
\item Syntactical: this is an assignment; lhs is something you can
  assign to, rhs is arithmetic expression
\item Semantics: \n{my_array} is array, \n{ii}~is integer, \n{sin}~is
  defined function
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Mixing of levels}
In Fortran:
\begin{verbatim}
    X = SOMENAME( Y+3 )
\end{verbatim}
\begin{itemize}
\item Lexical analysis simple
\item Syntax unclear: rhs can be function call or array element
\item Solution: give lexer access to symbol table
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Correctness}
\begin{itemize}
\item Lexical analysis finds identifiers: \n{5ab} is illegal
\item Syntactical analysis finds expressions: \n{array[ii)} is illegal
\item In \TeX?
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Parsing by automaton}
\begin{itemize}
\item Lexical analysis by Finite State Automaton
\item Syntactical analysis by Pushdown Automaton
\item In practice some mixing of levels
\end{itemize}
}

\subsection{Automata theory}

\frame[containsverbatim]{
  \frametitle{Terminology}
\begin{itemize}
\item Language: a set of words \[\{a^n|\hbox{$n$ is prime}\}\]
\item Grammar: set of rules that produces a language
\item Automaton: abstract device that can recognize a language
\item Derivation: actual sequency of rules or transitions used to
  derive a string
\item Parse tree: 2D way of writing derivation
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{to be precise}
\begin{itemize}
\item Grammar:
\begin{itemize}
\item Start symbol $S$
\item Terminal symbols $a,b,c,...$ from the alphabet
\item Non-terminals $A,B,C,...$, ultimately to be replaced
\item Rules $\alpha\rightarrow\beta$ where $\alpha,\beta$ strings of
  terminals and non-terminals
\end{itemize}
\item Automaton:
\begin{itemize}
\item Starting state
\item Accepting state
\item Work storage
\item Transition diagram
\end{itemize}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Types of languages}
\begin{itemize}
\item Languages differ in types of grammar rules
  $\alpha\rightarrow\beta$
\item Automata differ in amount of workspace
\item Four levels Chomsky hierarchy; many other types
\end{itemize}
}

\frame{
  \frametitle{Type 0}
\begin{itemize}
\item Regular languages
\item<2-> Turing machine: infinite tape
\item<2-> No restriction on grammar rules
\end{itemize}
}

\frame{
  \frametitle{Type 1}
\begin{itemize}
\item Context-sensitive languages
\item<2-> Linear-bounded automata
\item<2-> No rules $\alpha\rightarrow\epsilon$
\item<3-> Normal form: $AB\rightarrow BA$,
  $AB\rightarrow A\beta$
\end{itemize}
}

\frame{
  \frametitle{Type 2}
\begin{itemize}
\item Context-free languages
\item<2-> Push Down Automata
\item<2-> Only rules $A\rightarrow\alpha$
\item<3-> Normal form: $A\rightarrow b\alpha$ or $A\rightarrow b$
\end{itemize}
}

\frame{
  \frametitle{Type 3}
\begin{itemize}
\item Regular languages
\item<2-> Finite State Automata
\item<2-> Only rules $A\rightarrow bC$, $A\rightarrow b$
\end{itemize}
}

\sectionframe{Lexical analysis}

\frame[containsverbatim]{
  \frametitle{Function of a lexer}
\begin{itemize}
\item Recognize identifiers, numbers
\item Also side effects: store names of functions
\end{itemize}
}

\subsection{Regular languages}

\frame[containsverbatim]{
  \frametitle{Definition}
 Inductively, through regular expressions
\begin{itemize}
\item $\epsilon$ is the empty language
\item `$a$' denotes the language~$\{a\}$ ($a$~in alphabet)
\item if $\alpha,\beta$ denote languages~$A,\nobreak B$, then
\begin{itemize}
\item $\alpha\beta$ or $\alpha\cdot\beta$ denotes $\{xy|x\in A,y\in B\}$
\item $\alpha|\beta$ denotes the language $A\cup B$.
\item $\alpha^*$ denotes the language $\cup_{n\geq 0}A^n$.
\end{itemize}
\end{itemize}
}

\frame{
  \frametitle{Finite state automata}
\begin{itemize}
\item Starting state $S_0$
\item other states $S_i$; subset: accepting states
\item input alphabet~$I$; output alphabet~$O$
\item transition diagram $I\times S\rightarrow S$
\item<2-> non-deterministic: $I\cup\{\epsilon\}\times S\rightarrow S$
\item<3-> String is accepted if (any) sequence of transitions it
  causes leads to an accepting state
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{The NFA of a regular language}
Automaton that accepts~$\epsilon$:\\
\convertMPtoPDF{eps.1}{1}{1}

Automaton that accepts~$a$:\\
\convertMPtoPDF{a.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{The NFA of a regular language}
Automaton that accepts~$A\cdot B$:\\
\convertMPtoPDF{AB.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{The NFA of a regular language}
Automaton that accepts~$A\cup B$:\\
\convertMPtoPDF{AvB.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{The NFA of a regular language}
Automaton that accepts~$A^*$:\\
\convertMPtoPDF{Astar.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{Characterization}
\begin{itemize}
\item Any sufficiently long string $\alpha=uvw$
\item then $uv^nw$ also in the language
\end{itemize}
}

\subsection{DFAs and NFAs}

\frame{
  \frametitle{Example}
Language $a^*|b^*$:

\leavevmode
\convertMPtoPDF{nfa1.1}{1}{1}
$\quad\Rightarrow\quad$
\convertMPtoPDF{dfa1.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{Example: keywords}
A bit like what happens in lexical analysis:

\convertMPtoPDF{begin.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{}
Deterministic version

\convertMPtoPDF{begind.1}{1}{1}
}

\frame{
  \frametitle{Converting NFA to DFA}
\begin{itemize}
\item Introduce new states
\item<2-> new state is set of old states
\item<3-> new states closed under $\epsilon$-transitions
\end{itemize}
}

\frame{
\convertMPtoPDF{begin.1}{1}{1}

New $S_0=\{0,1,6\}$
\begin{itemize}
\item<2->$S_0+\n{B}\Rightarrow S_1=\{2,6,7\}$,
\item<3->$S_0+\neg\n{B}\Rightarrow S_6=\{6,7\}$
\item<4->$S_1+\n{E}\Rightarrow S_2=\{3,6,7\}$, et cetera
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{NFA for lexical analysis}
\convertMPtoPDF{compound.1}{1}{1}
}

\frame[containsverbatim]{
  \frametitle{small problems}
\begin{itemize}
\item Careful with the $\epsilon$-transition back:
\begin{verbatim}
  printf("And then he said ""Boo!""");
\end{verbatim}
final state reached three times: only transition when maximum string
recognized
\item Not always:
\begin{verbatim}
X = 4.E3
IF (4.EQ.VAR) THEN
\end{verbatim}
$\Rightarrow$ look-ahead needed
\end{itemize}
}

\subsectionframe{\lex}

\frame[containsverbatim]{
  \frametitle{A tool for lexical analysis}
\begin{itemize}
\item You write regular expressions, and \lex\ reports if it finds any
\item Three sections: definitions, rules, code
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Example}
\footnotesize
\begin{verbatim}
%{
 int charcount=0,linecount=0;
%}

%%

. charcount++;
\n {linecount++; charcount++;}

%%
int main()
{
  yylex();
  printf("There were %d characters in %d lines\n",
         charcount,linecount);
  return 0;
}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Running lex}
\lex\ code gets translated to C:

\begin{verbatim}
lex -t count.l > count.c
cc -c -o count.o count.c
cc -o counter count.o -ll
\end{verbatim}

Executable uses stdin/out, can be changed
}

\frame[containsverbatim]{
  \frametitle{Definitions section}
\begin{itemize}
\item C code: between \verb+%{ ... %}+ copied to top of C file
\item Definitions: `\verb+letter [a-zA-Z]+' (like \n{#define})
\item State definitions (later)
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Example 2}
\begin{verbatim}
%{
 int charcount=0,linecount=0,wordcount=0;
%}
letter [^ \t\n]

%%

{letter}+ {wordcount++; charcount+=yyleng;}
.         charcount++;
\n        {linecount++; charcount++;}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Rules section}
\begin{itemize}
\item Input is matched by character
\item Actions of longest match are taken, earliest if equal length
\item Matched text is \verb+char *yytext+, length \verb+int yyleng+
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Example 2'}
\begin{verbatim}
{letter}+ {wordcount++; charcount+=yyleng;}
[ \t]     spacecount++;
.         charcount++;
\n        linecount++;
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Example 3}
\begin{verbatim}
[0-9]+          process_integer();
[0-9]+\.[0-9]*  |
\.[0-9]+        process_real();
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Regular expressions}
\def\titem#1{\item[{\tt #1}]}
\footnotesize
\begin{description}
\titem{.} Match any character except newlines.
\titem{\char`\\n} A newline character.
\titem{\char`\\t} A tab character.
\titem{\char`\^} The beginning of the line.
\titem{\$} The end of the line.
\titem{<expr>*} Zero or more occurrences of the expression.
\titem{<expr>+} One or more occurrences of the expression.
\titem{<expr>?} Zero or one occurrences of the expression.
\titem{(<expr1>|<expr2>)} One expression of another.
\titem{{[<set>]}} A set of characters or ranges, such as \verb+[,.:;]+
  or \verb+[a-zA-Z]+.
\titem{{[\char`\^<set>]}} The complement of the set, for instance
\verb+[^ \t]+.
\end{description}
}

\frame[containsverbatim]{
  \frametitle{Example: filtering comments}
\begin{verbatim}
%%
"/*".*"*/"  ;
.           |
\n          ECHO;
\end{verbatim}
works on
\begin{verbatim}
This text /* has a */ comment
in it
\end{verbatim}
}

\frame[containsverbatim]{
Does not work on
\begin{verbatim}
This text /* has */ a /* comment */ in it
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Context}
\begin{itemize}
\item Match in context
\item Left context implemented through states:
\begin{verbatim}
<STATE>(some pattern) {...
\end{verbatim}
State switching:
\begin{verbatim}
<STATE>(some pattern) {some action; BEGIN OTHERSTATE;}
\end{verbatim}
Initial state is \n{INITIAL}, other states defined
\begin{verbatim}
%s MYSTATE
%x MYSTATE
\end{verbatim}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Use of states}
\begin{verbatim}
%x COMM

%%

.          |
\n         ECHO;
"/*"       BEGIN COMM;
<COMM>"*/" BEGIN INITIAL;
<COMM>.    |
<COMM>\n   ;

%%
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Context'}
\begin{itemize}
\item Right context:
\begin{verbatim}
abc/de {some action}
\end{verbatim}
\item context tokens not in \n{yytext}/\n{yyleng}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Example: text cleanup}
Input:
\begin{verbatim}
This    text (all of it  )has occasional lapses , in
 punctuation(sometimes pretty bad) ,( sometimes not so).

(Ha! ) Is this : fun?Or what!
\end{verbatim}
Solution with context more compact than without.

Define:
\begin{verbatim}
punct [,.;:!?]
text [a-zA-Z]
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{need for context}
\begin{itemize}
\item Consider `\n{),}' `\n{) ,}' `\n{)a}' `\n{) a}'
\item Rules \verb!")" " "+ {printf(")");}! depend on context
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{right context solution}
\footnotesize
\begin{verbatim}
")"" "+/{punct}       {printf(")");}
")"/{text}            {printf(") ");}
{text}+" "+/")"       {while (yytext[yyleng-1]==' ') yyleng--; ECHO;}

({punct}|{text}+)/"(" {ECHO; printf(" ");}
"("" "+/{text}        {while (yytext[yyleng-1]==' ') yyleng--; ECHO;}

{text}+" "+/{punct}   {while (yytext[yyleng-1]==' ') yyleng--; ECHO;}

^" "+                 ;
" "+                  {printf(" ");}
.                     {ECHO;}
\n/\n\n               ;
\n                    {ECHO;}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{left context solution}
Use defined states:
\begin{verbatim}
punct [,.;:!?]
text [a-zA-Z]

%s OPEN
%s CLOSE
%s TEXT
%s PUNCT
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{left context solution, cont'd}
\footnotesize
\begin{verbatim}
" "+ ;

<INITIAL>"(" {ECHO; BEGIN OPEN;}
<TEXT>"("    |
<PUNCT>"("   {printf(" "); ECHO; BEGIN OPEN;}

")" {ECHO ; BEGIN CLOSE;}

<INITIAL>{text}+ |
<OPEN>{text}+    {ECHO; BEGIN TEXT;}
<CLOSE>{text}+   |
<TEXT>{text}+    |
<PUNCT>{text}+   {printf(" "); ECHO; BEGIN TEXT;}

{punct}+ {ECHO; BEGIN PUNCT;}

\n {ECHO; BEGIN INITIAL;}
\end{verbatim}
}

\sectionframe{Syntactical analysis}

\frame[containsverbatim]{
  \frametitle{Function of syntactical analysis}
\begin{itemize}
\item Recognize statements: loops, assignments~\&c
\item Convert to internal representation: parse trees
\begin{quote}
\hbox{%
\Tree [.* [.+ $2\quad$ $5\quad$ ] $3\quad$ ]
\Tree [.+ $2\quad$ [.* $5\quad$ $3\quad$ ] ]
}
\end{quote}
\item Semantics: define/use sequence~\&c
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Grammars}
\begin{itemize}
\item Backus Naur, or other formalism
\item In \LaTeX: \n{bnf.sty}
\begin{examplewithcode}
\begin{bnf}
Expr: number Tail.
Tail: $\epsilon$ ; + number Tail; * number Tail
\end{bnf}
\end{examplewithcode}
(use my \n{bnf.env})
\item most language constructs are context-free
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Concepts}
\begin{itemize}
\item Grammar rules $A\rightarrow x\alpha$
\item Derivations $abcA\gamma\Rightarrow abc x\alpha\gamma$
\item Parse tree
\begin{quote}
\Tree [.. abc [.A $x\quad$ $\alpha\quad$ ] $\gamma$ ]
\end{quote}
\end{itemize}
}

\subsectionframe{Context-free languages}

\frame[containsverbatim]{
  \frametitle{Definition}
\begin{itemize}
\item Grammatical: only rules $A\rightarrow \alpha$
\item From automata: pushdown automata
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Pumping lemma}
\begin{itemize}
\item For every language there is an $n$ such that
\begin{itemize}
\item strings longer than~$n$ can be written $uvwxy$
\item and for all~$k$: $uv^kwx^ky$ also in the language
\end{itemize}
\item Proof: \begin{quote}
\Tree [.S $u$ [.A $v$ [.A $w$ ] $x$ ] $y$ ]
\end{quote}
\item Non-\{context-free\} language: $\{a^nb^nc^n\}$
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Deterministic and non-deterministic}
\begin{itemize}
\item No equivalence
\item Deterministic: $L_c=\{\alpha c\alpha^R|c\not\in\alpha\}$
\item Non-deterministic: $L=\nobreak\{\alpha\alpha^R\}$
\end{itemize}
}

\def\mbx{\mathbf{x}}
\def\mba{\mathbf{a}}
\def\mbb{\mathbf{b}}
\def\mbA{\mathbf{A}}
\def\mbB{\mathbf{B}}
\def\mby{\mathbf{y}}
\def\mbf{\mathbf{f}}
\frame[containsverbatim]{
  \frametitle{Algebra of languages}
\begin{itemize}
\item Expressions $\mbx$ and $\mby$ denote languages, then
\begin{itemize}
\item union: $\mbx+\mby=\mbx\cup\mby$
\item concatenation: $\mbx\mby=\{w=xy|x\in\mbx,y\in\mby\}$
\item repetition: $\mbx^*=\{w=x^n|x\in\mbx,n\geq0\}$
\end{itemize}
\end{itemize}
}

\frame{
  \frametitle{Algebra: solving equations}
\begin{itemize}
\item Equation: $\mbx=\mba+\mbx\mbb$
\item Interpretation: $\mbx=\mba\cup\{w=xb|x\in\mbx,b\in\mbb\}$
\item Solving:
\begin{itemize}
\item first of all $\mbx\supset\mba$
\item then also $\mbx\supset\mba\cdot\mbb$
\item continuing: $\mbx\supset\mba\mbb\mbb$,\dots
\end{itemize}
\item verify: $\mbx=\mba\mbb^*$
\item<2> Numerically: $x=a/(1-b)$
\end{itemize}
}

\frame{
  \frametitle{Derive normal form}
\begin{itemize}
\item Normal form: $A\rightarrow a\alpha$
\item Write grammar of context-free language as $\mbx^t=\mbx^t\mbA+\mbf^t$,
where $\mbx$~non-terminals, $\mbf$~rhs that are of normal form,
$\mbx^t\mbA$~describes normal form rhs
\item<2> Example:\\
\hbox{%
$\displaystyle\begin{array}{l}
S\rightarrow aSb|XY|c\\
X\rightarrow YXc|b\\
Y\rightarrow XS
  \end{array}$\ \ %
  $\displaystyle [S,X,Y]=[S,X,Y]\left[
                \begin{matrix}\phi&\phi&\phi\\
                  Y&\phi&S\\ \phi&Xc&\phi \end{matrix}\right]
                +[aSb+c,b,\phi]$
}
\item<3->  Solution:
\[ \mbx^t = \mbf^t\mbA^* \]
\item<4-> Needed: more explicit expression for~$\mbA^*$.
\end{itemize}
}

\frame{
\begin{itemize}
\item Note $\mbA^*=\lambda+\mbA\mbA^*$
\item then normal form: \[
 \mbx^t = \mbf^t+\mbf^t\mbA\mbA^*
  =  \mbf^t+\mbf^t\mathbf{B} \]
where $\mathbf{B}=\mbA\mbA^*$.
\item<2-> $\mbB$:
\[ \mbB=\mbA\mbA^*=
  \mbA+\mbA\mbA\mbA^* =
  \mbA+\mbA\mbB \]
not necessarily normal form
\item<3-> Elements of~$\mbA$ that start with a
nonterminal can only start with nonterminals in~$\mbx$. Hence
substitute a rule from equation above.
\end{itemize}
}

\subsectionframe{Parsing strategies}

\frame[containsverbatim]{
  \frametitle{Top-down parsing}
\begin{itemize}
\item Start with $S$ on the stack, replace by appropriate rule, guided
  by input
\item Example: expression \n{2*5+3},
which is produced by the grammar
\begin{bnf}
Expr: number Tail.
Tail: $\epsilon$ ; + number Tail; * number Tail
\end{bnf}
\end{itemize}
}

\frame[containsverbatim]{
\begin{tabbing}
start symbol on stack:$\quad$\=${}2*5+3{}\quad$\=\kill
initial queue:\>$2*5+3$\\
start symbol on stack:\>\>Expr\\
replace\>\>number Tail\\
match\>${}*5+3$\>Tail\\
replace\>\>* number Tail\\
match\>$5+3$\>number Tail\\
match\>${}+3$\>Tail\\
replace\>\>+ number Tail\\
match\>$3$\>number Tail\\
match\>$\epsilon$\>Tail\\
match
\end{tabbing}
\[ E\Rightarrow n\, T\Rightarrow n*n\,T\Rightarrow
    n*n+n\,T\Rightarrow n*n+n \]
$LL(1)$
}

\frame[containsverbatim]{
  \frametitle{}
Equivalent grammar:
\begin{bnf}
Expr: number; number + Expr; number * Expr
\end{bnf}
assuming one more token look-ahead:
\begin{tabbing}
start symbol on stack:$\quad$\=${}2*5+3{}\quad$\=\kill
initial queue:\>$2*5+3$\\
start symbol on stack:\>\>Expr\\
replace\>\>number * Expr\\
match\>${}5+3$\>Tail\\
replace\>\>number + Expr\\
match\>$3$\>Expr\\
replace\>$3$\>number\\
match\>$\epsilon$
\end{tabbing}
$LL(2)$
}

\frame[containsverbatim]{
  \frametitle{$LL$ is recursive descent}
Finding of proper rule:
\begin{verbatim}
define FindIn(Sym,NonTerm)
  for all expansions of NonTerm:
    if leftmost symbol == Sym
      then found
    else if leftmost symbol is nonterminal
      then FindIn(Sym,that leftmost symbol)
FindIn(symbol,S);
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Problems with $LL(k)$}
\begin{itemize}
\item Some grammars are not $LL(k)$ for any~$k$:\\
if \n{A<B} and \n{A<B>} both legal
\item Infinite loop:
\begin{bnf}
Expr: number; Expr + number; Expr * number
\end{bnf}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Bottom-up: Shift-reduce}
\begin{itemize}
\item Recognize productions from terminals
\item Example: expression $2*5+3$ produced by
\begin{bnf}
E: number; E + E; E * E
\end{bnf}
\end{itemize}
}

\frame[containsverbatim]{
\begin{tabbing}
shift, shift, reduce: abcd\=initial state:initial\=\kill
\>stack\>queue\\
initial state:\>\>$2*5+3$\\
shift\>2\>*5+3\\
reduce\>E\>*5+3\\
shift\>E*\>5+3\\
shift\>E*5\>+3\\
reduce\>E*E\>+3\\
reduce\>E\>+3\\
shift, shift, reduce\>E+E\\
reduce\>E\\
\end{tabbing}
\[ E\Rightarrow E+E\Rightarrow E+3\Rightarrow E*E+3
    \Rightarrow E*5+3\Rightarrow 2*5+3 \]
$LR(0)$
}

\frame[containsverbatim]{
  \frametitle{Where to start reducing?}
\begin{itemize}
\item `Greedy' reducing is not always best
\item Grammar:
\begin{bnf}
S:aAcBe.
A:bA;b.
B:d.
\end{bnf}
and string \n{abbcde}.
\item Derivation 1:
\[ \n{abbcde}\Leftarrow\n{abAcde}\Leftarrow\n{aAcde}\Leftarrow
    \n{aAcBe}\Leftarrow\n{S}. \]
\item Derivation 2:
\[ \n{abbcde}\Leftarrow\n{aAbcde}\Leftarrow aAAcde \Leftarrow ? \]
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Handle}
\begin{quote} If $S\Rightarrow^*\alpha Aw\Rightarrow\alpha\beta w$ is a
  right-most derivation, then $A\rightarrow\beta$ at the position
  after~$\alpha$ is a handle of~$\alpha Aw$.
\end{quote}
Question: how to find handles
}

\frame[containsverbatim]{
  \frametitle{Operator-precedence grammars}
\begin{itemize}
\item Operator grammar: `expr-op-expr'
\item Formally: never two consecutive non-terminals, and no
  rules~$A\rightarrow\nobreak\epsilon$.
\item Declare precedences (and associativity)
\begin{tabular}{r|ccc}
&\hbox{number}&$+$&$\times$\\\hline
\hbox{number}&&$\gtrdot$&$\gtrdot$\\
$+$&$\lessdot$&$\gtrdot$&$\lessdot$\\
$\times$&$\lessdot$&$\gtrdot$&$\gtrdot$
\end{tabular}\\
\end{itemize}
}

\frame[containsverbatim]{
\begin{itemize}
\item Annotate expression: $5+2*3$ becomes
$\lessdot5\gtrdot+\lessdot2\gtrdot*\lessdot3\gtrdot$
\item Reducing: $E+E*E$
\item Insert precedences: ${\lessdot}+{\lessdot}*{\gtrdot}$
\item Scan forward to closing, back to open:
  ${\lessdot}E*E{\gtrdot}$~is handle
\item Reduce: $E+E$
\item $\Rightarrow$ precendences correctly observed
\item (note: no global scanning; still shift-reduce like)
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Definition of $LR$ parser}
An LR parser has the following components
\begin{itemize}
\item Stack and input queue; stack will also contain states
\item Actions `shift', `reduce', `accept', `error'
\item Functions \n{Action} and \n{Goto}
\begin{itemize}
\item With input symbol~$a$ and state on top of the stack~$s$:
\item If \n{Action}$(a,s)$ is `shift', then $a$ and a new state
  $s'=\n{Goto}(a,s)$ are pushed on the stack.
\item If \n{Action}$(a,s)$ is `reduce $A\rightarrow\beta$' where
  $|\beta|=r$, then $2r$ symbols are popped from the stack, a new
  state $s'=\n{Goto}(a,s'')$ is computed based on the newly exposed
  state on the top of the stack, and $A$ and~$s'$ are pushed. The
  input symbol~$a$ stays in the queue.
\end{itemize}
\end{itemize}
More powerful than simple shift/reduce; states much more complicated
}

\frame[containsverbatim]{
  \frametitle{motivating example}
\begin{itemize}
\item Grammar
\begin{bnf}
E: E + E; E * E
\end{bnf}
input string $1+2*3+4$.
\item Define precedences:
\def\op{\mathop{\mathbf{op}}}
$\op(+)=1, \op(\times)=2$
\item Define states as; initially state~0
\item Transitions: push operator precedence, do not change state for numbers
\item Shift/reduce strategy: reduce if precedence of input lower than
  of stack top
\end{itemize}
}

\frame[containsverbatim]{
\def\op{\mathop{\mathbf{op}}}
\footnotesize
\begin{tabbing}
1 $S_0$ + $S_1$ 2 $S_1$ * $S_2$ 3 $S_2$ \= $1+2*3+4$ \=\kill
\>$1+2*3+4$\> push symbol; highest precedence is 0\\
1 $S_0$\>$+2*3+4$\>highest precedence now becomes 1\\
1 $S_0$ + $S_1$\>$2*3+4$\\
1 $S_0$ + $S_1$ 2 $S_1$\>$*3+4$\>highest precedence becoming 2\\
1 $S_0$ + $S_1$ 2 $S_1$ * $S_2$\>$3+4$\\
1 $S_0$ + $S_1$ 2 $S_1$ * $S_2$ 3 $S_2$\>$+4$\>reduce because $\op(+){}<2$\\
1 $S_0$ + $S_1$ 6 $S_1$\>$+4$\>the highest exposed precedence is 1\\
1 $S_0$ + $S_1$ 6 $S_1$ + $S_1$\>$4$\\
1 $S_0$ + $S_1$ 6 $S_1$ + $S_1$ 4 $S_1$ \>\>at the end of the queue we reduce\\
1 $S_0$ + $S_1$ 10 $S_1$ \\
11
\end{tabbing}
}

\frame[containsverbatim]{
  \frametitle{Parser states}
\begin{description}
\item[item] Grammar rule with location indicated.\\
 From \parserule{A}{B C} items:
 \parserule{A}{.B C}, \parserule{A}{B .C}, \parserule{A}{B C.}\\
(stack is left of dot, queue right)
\item[closure of an item]   The smallest set that
\begin{itemize}
\item Contains that item;
\item If $I$ in closure and $I={}$\parserule{A}{$\alpha$ .B $\beta$}
 with~\n{B} nonterminal, then $I$~contains all items \parserule{B}{.$\gamma$}.
\end{itemize}
\item[state] Set of items.
\item[follow] of~\n{A}: set of all terminals that can follow $A$'s~expansions
\end{description}
}

\frame[containsverbatim]{
  \frametitle{Motivation: valid items}
\begin{itemize}
\item  Recognized so far: $\alpha\beta_1$
\item Consider  item \parserule{A}{$\beta_1$.$\beta_2$}
\item Item is called \emph{valid}, if rightmost derivation
\[ S\Rightarrow^*\alpha Aw\Rightarrow \alpha\beta_1\beta_2w \]
\item Case: $\beta_2=\epsilon$, then $A\rightarrow\beta_1$
  handle: reduce
\item Case: $\beta_2\not=\epsilon$, so shift~$\beta_2$.
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{example of valid items}
\begin{itemize}
\item String~\n{E+T*} in grammar:
\begin{bnf}
E:E+T; T.
T:T*F; F.
F:(E); id
\end{bnf}
\item Derivations
\[ E\Rightarrow E+T\Rightarrow E+T*F \]
\[ E\Rightarrow E+T\Rightarrow E+T*F\Rightarrow E+T*(E) \]
\[ E\Rightarrow E+T\Rightarrow E+T*F\Rightarrow E+T*\mathrm{id} \]
give items \parserule{T}{T*.F} \parserule{F}{.(E)} \parserule{F}{.id}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{States and transitions}
\begin{itemize}
\item New start symbol~\n{S'}, added production \parserule{S'}{S}.
\item  Starting state is closure of \parserule{S'}{.S}.
\item Transition $d(s,\n{X})$: the closure of
\[ \{ \parserule{A}{$\alpha$ X. $\beta$}
          | \hbox{\parserule{A}{$\alpha$ .X $\beta$} is in $s$} \} \]
\item The initial state is the closure of \parserule{S'}{.S}.
\end{itemize}
}

\frame{
\footnotesize
Grammar: $S\rightarrow(S)S\mid|\epsilon$
%\begin{bnf}S:(S)S;$\epsilon$\end{bnf}

States (after adding \parserule{S'}{.S}):
\begin{enumerate}\setcounter{enumi}{-1}
\item<2-> $\{ \parserule{S'}{.S}, \parserule{S}{.(S)S}, \parserule{S}{.} \}$
\item<3-> $\{ \parserule{S'}{S.} \}$
\item<4-> $\{ \parserule{S}{(.S)S}, \parserule{S}{.(S)S}, \parserule{S}{.} \}$
\item<5-> $\{ \parserule{S}{(S.)S} \}$
\item<6-> $\{ \parserule{S}{(S).S}, \parserule{S}{.(S)S}, \parserule{S}{.} \}$
\item<7-> $\{ \parserule{S}{(S)S.} \}$
\end{enumerate}
with transitions ($\{A\rightarrow\alpha_\bullet X\beta\in s\Rightarrow
\mathop(cl)(A\rightarrow\alpha X_\bullet\beta)$):
\begin{itemize}
\item<8->$d(0,S) = 1 $
\item<9->$d(0,'(') = 2 $
\item<10->$d(2,S) = 3 $
\item<11->$d(2,'(') = 2 $
\item<12->$d(3,')') = 4 $
\item<13->$d(4,S) = 5 $
\item<14->$d(4,'(') = 2$
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Stack handling}
\begin{tabbing}
\textbf{Loop}:\\
(1) \textbf{else} \=\kill
(1) \textbf{if} \>th\=e current state contains \parserule{S'}{S.}\\
\>\>accept the string\\
(2) \textbf{else} \>\textbf{if} the current state %
    contains any other final item \parserule{A}{$\alpha$.}\\
\>\>pop all the tokens in $\alpha$ from the stack,\\
\>\>\hspace{20pt}along with the corresponding states; \\
\>\>let $s$ be the state left on top of the stack:\\
\>\>\hspace{20pt}    push \n{A}, push \n{d($s$,A)}\\
(3) \textbf{else} \>\textbf{if} the current state contains any item %
    \parserule{A}{$\alpha$ .x $\beta$},\\
\>\>$\qquad$ where x is the next input token\\
\>\>let $s$ be the state on top of the stack: %
    push \n{x}, push \n{d($s$,x)}\\
(1) \=\kill
\>\textbf{else} report failure
\end{tabbing}
}

\subsectionframe{Ambiguity and conflicts}

\frame[containsverbatim]{
  \frametitle{Shift/reduce conflict}
\begin{itemize}
\item Grammar for $2+5*3$:
\begin{bnf}
<expr>: <number>; <expr> + <expr>; <expr> $\times$ <expr>.
\end{bnf}
\item interpretations:
\hbox{%
\Tree [.* [.+ $2\quad$ $5\quad$ ] $3\quad$ ]
\Tree [.+ $2\quad$ [.* $5\quad$ $3\quad$ ] ]
}
\item Parse: reduce $2+5$ to \n{<expr> + <expr>},\\
then reduce to~\n{<expr>}, or~shift the minus?
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{solutions}
\begin{itemize}
\item Reformulate the grammar as
\begin{bnf}
<expr>: <mulex>; <mulex> + <mulex>.
<mulex>: <term>; <term> $\times$ <term>.
<term>: number.
\end{bnf}
\item new parse:
\begin{quote}
\Tree [.expr [.mulex [.term 2 ] ] + [.mulex [.term 5 ] * [.term 3 ] ] ]
\end{quote}
\item Introduce precedence of operators.\\ Possibly more efficient if
  large number of operators.
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{`Dangling else'}
\begin{itemize}
\item Consider the grammar
\begin{bnf}
<statement>: if <clause> then <statement>; if <clause> then <statement> else <statement>
\end{bnf}
\item  string
\begin{quote}
\tt if c$_1$ then if c$_2$ then s$_1$ else s$_2$
\end{quote}
\item Interpretations:
\hbox{\tiny
\Tree [.S If Then [.S If Then ] Else ]
\hskip-1cm
\Tree [.S If Then [.S If Then Else ] ]
}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Reduce/reduce conflict}
\begin{itemize}
\item Grammar for \n{x y c}
\begin{bnf}
A : B c d ; E c f.
  B : x y.
  E : x y.
\end{bnf}
\item $LR(1)$ parser: shift \n{x y},\\ then reduceto \n{B} or~\n{E}?
\item $LR(2)$ parser: sees \n{d} or~\n{f}
\item An $LL$ parser: ambiguity in the first 3 tokens\\
  $LL(4)$ parser can sees \n{d} or~\n{f}.
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item Grammar for $x\,y\,c^n\,\{d|f\}$:
\begin{bnf}
  A : B C d ; E C f.
  B : x y .
  E : x y .
  C : c ; C c.
\end{bnf}
\item confusing for any $LR(n)$ or $LL(n)$ parser with a
fixed amount of look-ahead
\item rewrite:
\begin{bnf}
  A    : BorE c d ; BorE c f.
  BorE : x y.
\end{bnf}
or (for an $LL(n)$ parser):
\begin{bnf}
  A    : BorE c tail.
  tail : d ; f.
  BorE : x y.
\end{bnf}
\end{itemize}
}

\subsectionframe{\yacc}

\frame[containsverbatim]{
  \frametitle{\yacc\ and \lex}
\begin{itemize}
\item \lex\ produces tokens
\item \yacc\ analyzes sequences of tokens
\item lexer returns on recognizing a token
\item main program in \yacc\ code
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{File structure}
\begin{verbatim}
  ...definitions...
%%
  ...rules...
%%
  ...code...
\end{verbatim}
\begin{itemize}
\item Default main calls \n{yyparse}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Example: \yacc\ code header}
\footnotesize
File name \n{words.y}
\begin{verbatim}
%{

#include <stdlib.h>
#include <string.h>
  int yylex(void);
#include "words.h"
  int nwords=0;
#define MAXWORDS 100
  char *words[MAXWORDS];
%}

%token WORD

%%
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{include file}
Generated by running \yacc:
\begin{verbatim}
%% cat words.h
#define WORD 257
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Example: \lex\ code}
\footnotesize
\begin{verbatim}
%{

#include "words.h"
int find_word(char*);
extern int yylval;
%}

%%

[a-zA-Z]+ {yylval = find_word(yytext);
	   return WORD;}
.         ;
\n        ;

%%
\end{verbatim}
}

\frame[containsverbatim]{
\footnotesize
\begin{verbatim}
text : ;
       | text WORD  ; {
            if ($2<0) printf("new word\n");
            else printf("matched word %d\n",$2);
                      }
%%

int find_word(char *w)
{ int i;
  for (i=0; i<nwords; i++)
    if (strcmp(w,words[i])==0) return i;
  words[nwords++] = strdup(w); return -1;
}

int main(void)
{
  yyparse();
  printf("there were %d unique words\n",nwords);
}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Running \lex\ and \yacc}
\begin{verbatim}
/* create and compile yacc C file */
yacc -d -t -o YACCFILE.c YACCFILE.y
cc -c -o YACCFILE.o YACCFILE.c

/* create and compile lex C file */
lex -t LEXFILE.l > LEXFILE.c
cc -c -o LEXFILE.o LEXFILE.c

/* link together */
cc YACCFILE.o LEXFILE.o -o YACCPROGRAM -ly -ll
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Make with suffix rules}
\footnotesize
\begin{verbatim}
# disable normal rules
.SUFFIXES:
.SUFFIXES: .l .y .o
# lex rules
.l.o :
        lex -t $*.l > $*.c
        cc -c $*.c -o $*.o
# yacc rules
.y.o :
        if [ ! -f $*.h ] ; then touch $*.h ; fi
        yacc -d -t -o $*.c $*.y
        cc -c -o $*.o $*.c ;
        rm $*.c
# link lines
lexprogram : $(LEXFILE).o
        cc $(LEXFILE).o -o $(LEXFILE) -ll
yaccprogram : $(YACCFILE).o $(LEXFILE).o
        cc $(YACCFILE).o $(LEXFILE).o -o $(YACCFILE) -ly -ll
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{\yacc\ definitions section}
\begin{itemize}
\item C code in between \verb+%{ ... %}+
\item Token definitions: the \lex\ return tokens
\item Associativity rules (later)
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Tokens}
\begin{itemize}
\item Definition: \verb+%token FOO+
\item In \n{.h} file: \verb+#define FOO 257+ (or so)
\item \lex\ code: \verb+return FOO+
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Returning values over the stack}
\begin{itemize}
\item \lex\ assigns to \n{yylval}
\item value is put on top of stack
\item if a \yacc\ rule is matched: \verb+$1, $2, $3+ are assigned\\
(as many as elements in rhs)
\item replace stack top: assign to \verb+$$+
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Calculator example: \lex\ code}
\footnotesize
\begin{verbatim}
%{
#include "calc1.h"
void yyerror(char*);
extern int yylval;
%}

%%
[ \t]+ ;
[0-9]+     {yylval = atoi(yytext);
            return INTEGER;}
[-+*/]     {return *yytext;}
"("        {return *yytext;}
")"        {return *yytext;}
\n         {return *yytext;}
.          {char msg[25];
            sprintf(msg,"%s <%s>","invalid character",yytext);
            yyerror(msg);}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Calculator example: \yacc\ code}
\footnotesize
\begin{verbatim}
%{
int yylex(void);
#include "calc1.h"
%}

%token INTEGER

%%

program:
        line program
        | line
line:
        expr '\n'           { printf("%d\n",$1); }
        | 'n'
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Calculator example: \yacc\ code, cont'd}
\footnotesize
\begin{verbatim}
expr:
        expr '+' mulex      { $$ = $1 + $3; }
        | expr '-' mulex    { $$ = $1 - $3; }
        | mulex             { $$ = $1; }
mulex:
        mulex '*' term      { $$ = $1 * $3; }
        | mulex '/' term    { $$ = $1 / $3; }
        | term              { $$ = $1; }
term:
        '(' expr ')'        { $$ = $2; }
        | INTEGER           { $$ = $1; }

\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Calculator with variables}
\begin{itemize}
\item Simple case: single letter variables
\item more complicated: names
\item Extra rule: assignments
\item \lex\ returns
\begin{itemize}
\item \n{double} values \item \n{int} index of variable
\end{itemize}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Multiple return types}
\footnotesize
\begin{itemize}
\item Declare possible return types:
\begin{verbatim}
%union {int ival; double dval;}
\end{verbatim}
\item Connect types to return tokens:
\begin{verbatim}
%token <ival> NAME
%token <dval> NUMBER
\end{verbatim}
\item The types of non-terminals need to be given:
\begin{verbatim}
%type <dval> expr
%type <dval> mulex
%type <dval> term
\end{verbatim}
\item In \n{.h}~file will now have
\begin{verbatim}
#define name 258
#define NUMBER 259
typedef union {int ival; double dval;} YYSTYPE;
extern YYSTYPE yylval;
\end{verbatim}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Multiple return types: \lex\ code}
\begin{verbatim}
[ \t]+ ;
(([0-9]+(\.[0-9]*)?)|([0-9]*\.[0-9]+)) {
            yylval.dval = atof(yytext);
            return DOUBLE;}
[-+*/=]    {return *yytext;}
"("        {return *yytext;}
")"        {return *yytext;}
[a-z]      {yylval.ivar = *yytext - 'a';
            return NAME;} /* more later */
\n         {return *yytext;}
.          {char msg[25];
            sprintf(msg,"%s <%s>","invalid character",yytext);
            yyerror(msg);}
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Example: calculator with variables}
Tokens are \n{double} numbers, or variables (\n{int} index in table)
\begin{verbatim}
%{
#define NVARS 100
char *vars[NVARS]; double vals[NVARS]; int nvars=0;
%}
%union { double dval; int ivar; }
%token <dval> DOUBLE
%token <ivar> NAME
%type <dval> expr
%type <dval> mulex
%type <dval> term
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Symbol table handling}
\begin{itemize}
\item \lex\ parses variable names:
\begin{verbatim}
[a-z][a-z0-9]* {
            yylval.ivar = varindex(yytext);
            return NAME;}
\end{verbatim}
\item names are dynamically stored:
\begin{verbatim}
int varindex(char *var)
{
  int i;
  for (i=0; i<nvars; i++)
    if (strcmp(var,vars[i])==0) return i;
  vars[nvars] = strdup(var);
  return nvars++;
}
\end{verbatim}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Arithmetic}
Largely as before:
\begin{verbatim}
expr:
        expr '+' mulex      { $$ = $1 + $3; }
        | expr '-' mulex    { $$ = $1 - $3; }
        | mulex             { $$ = $1; }
mulex:
        mulex '*' term      { $$ = $1 * $3; }
        | mulex '/' term    { $$ = $1 / $3; }
        | term              { $$ = $1; }
term:
        '(' expr ')'        { $$ = $2; }
        | NAME              { $$ = vals[$1]; }
        | DOUBLE            { $$ = $1; }
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Assignments}
\begin{verbatim}
line:
        expr '\n'             { printf("%g\n",$1); }
        | NAME '=' expr '\n'  { vals[$1] = $3; }
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Operator precedence and associativity}
Increasing precedence order:
\begin{verbatim}
%left '+' '-'
%left '*' '/'
%right '^'
%%
expr:
        expr '+' expr ;
        expr '-' expr ;
        expr '*' expr ;
        expr '/' expr ;
        expr '^' expr ;
        number ;
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Unary operators}
Declare non-associative;\\
indicate presence in rule
\begin{verbatim}
%left '-' '+'
%nonassoc UMINUS
%
expression : expression '+' expression
    | expression '-' expression
    | '-' expression %prec UMINUS
\end{verbatim}
}

\frame[containsverbatim]{
  \frametitle{Error handling}
\footnotesize
\begin{itemize}
\item Default: \n{yyerror} prints \n{syntax error}
\item Better:\\
\lex\ code:
\begin{verbatim}
\n    lineno++;
\end{verbatim}
\yacc\ code:
\begin{verbatim}
void yyerror(char *s)
{
  printf("Parsing failed in line %d because of %s\n",lineno,s);
  return;
}
\end{verbatim}
\item Your own error messages:
\begin{verbatim}
expr : name '[' name ']'
        {if (!is_array($1) yyerror("array name expected");
\end{verbatim}
\end{itemize}
}

\frame[containsverbatim]{
  \frametitle{Error recovery}
\begin{itemize}
\item Use of \n{error} token:
\begin{verbatim}
foo : bar baz ;
    | error baz printf("Hope for the best\n");
\end{verbatim}
\end{itemize}
}

\end{document}

\frame[containsverbatim]{
  \frametitle{}
\begin{itemize}
\item
\end{itemize}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% University/School Laboratory Report
% LaTeX Template
% Version 3.1 (25/3/14)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% Linux and Unix Users Group at Virginia Tech Wiki
% (https://vtluug.org/wiki/Example_LaTeX_chem_lab_report)
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%	PACKAGES AND DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass{article}
\usepackage{fullpage,listings}
\usepackage[version=3]{mhchem} % Package for chemical equation typesetting
\usepackage{siunitx} % Provides the \SI{}{} and \si{} command for typesetting SI units
\usepackage{graphicx} % Required for the inclusion of images
\usepackage{natbib} % Required to change bibliography style to APA
\usepackage{amsmath} % Required for some math elements

\setlength\parindent{0pt} % Removes all indentation from paragraphs

\renewcommand{\labelenumi}{\alph{enumi}.} % Make numbering in the enumerate environment by letter rather than number (e.g. section 6)

%\usepackage{times} % Uncomment to use the Times New Roman font

\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\lstset{style=mystyle}

%----------------------------------------------------------------------------------------
%	DOCUMENT INFORMATION
%----------------------------------------------------------------------------------------

\title{Team Assignment 8 \\ Linear Regression \\ STAT 6021} % Title

\date{\today} % Date for the report

\begin{document}

\maketitle % Insert the title, author and date

\begin{center}
\begin{tabular}{l r}
Team members: & Don Chesworth \\ % Partner names
& Marcus Rosti \\
& Katherine Schinkel \\
& Mike Voltmer \\
Instructor: & Professor Holt % Instructor/supervisor
\end{tabular}
\end{center}

% If you wish to include an abstract, uncomment the lines below
% \begin{abstract}
% Abstract text
% \end{abstract}

%----------------------------------------------------------------------------------------
%	SECTION 1
%----------------------------------------------------------------------------------------

\section{Objective}
To analyze the effectiveness of merely subsetting parameters versus subsetting and transforming variables in a least squares regression setting.

% If you have more than one objective, uncomment the below:
%\begin{description}
%\item[First Objective] \hfill \\
%Objective 1 text
%\item[Second Objective] \hfill \\
%Objective 2 text
%\end{description}

\section{Question}
\subsection{Only Subsets}
To subset the data meaningfully, we used a mix of anova and the t test for significance.  We started by using regsubsets from the package leaps to identify the most important variables. This yielded age, PPE and DFA as the most important variables based on the exhaustive algorithm. Using those as a starting point, we next used anova to start removing variables. We dropped variables based on their significance and then verified there predictive power using 10 fold cross validation.  After getting to the point where we could no longer remove significant variables via ANOVA we were left with,
\begin{lstlisting}[language=R]
lm2 <- lm(motor_UPDRS ~ age + sex + test_time + Jitter.Abs. + NHR +
            Jitter.PPQ5 + Jitter.DDP + Shimmer.APQ3 + Shimmer.APQ5
            + Shimmer.APQ11 + HNR + DFA + PPE, data=p)
\end{lstlisting}

\subsection{Subsets and transformations}
This was a more difficult task.  The variables had little meaningful transformations. We used a mix of visual inspection and cross validation to confirm the predictive and inferential power of the model.  Going off of only our subsets, we started adding transformations on the data and did cross validation to measure the fit.  This ultimately yielded an interesting model.  Based on cross validation, I raised age to the 13th and PPE to the 7th while removing NHR, PPE and sex.
\begin{lstlisting}[language=R]
lm3 <- lm(motor_UPDRS ~ poly(age,13) + test_time + Jitter.Abs. +
            Jitter.PPQ5 + Jitter.DDP + Shimmer.APQ5 +
            Shimmer.APQ11 + HNR + DFA + poly(PPE,7), data=p)
\end{lstlisting}

\end{document}


\documentclass[a4paper,12pt]{article}
\usepackage{amsmath, amssymb}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{hyperref}

% \usepackage[UTF8]{ctex} % use ctex enable Chinese

% -------------------------------------------
\begin{document}

% title -------------------------------------
\title{My First Document}
\author{Yichen Mo}
\date{\today}
\maketitle

% toc ---------------------------------------
\pagenumbering{roman} % page number for toc
\tableofcontents
\newpage
\pagenumbering{arabic} % page number for rest article

% content -----------------------------------
\section{Introduction}
This is the introduction.
\href{http://www.texample.net/tikz/resources/}{Tex Example Site}

\section{Related Works}
Former works.\cite{Birdetal2001}

\section{Methods}

\subsection{Stage 1}
The first part of the methods.

% equation ----------------------------------
These are labeled equations
\begin{equation}
    e = mc^2
\end{equation}

\begin{equation}
    \pi = \frac{c}{d}
\end{equation}

This is no label equation
\begin{equation*}
    \pi = \frac{c}{d}
\end{equation*}

\subsection{Stage 2}
The second part of the methods. See the table \ref{table:1}

% table -------------------------------------
\begin{table}[h!]
    \centering
    \caption{Table to test captions and labels} % table name usually at the top
    \label{table:1}
    \begin{tabular}{ l | c c c }

        \multirow{2}{4em}{City} & \multicolumn{3}{l}{Year} \\
        \cline{2-4}
                           & 2006 & 2007 & 2008 \\
        \hline
        London & 45789 & 45628 & 48922 \\
        Berlin & 29392 & 23912 & 49293 \\
        Paris & 38281 & 29312 & 29341 \\
    \end{tabular}
\end{table}

\section{Results}
Here are my results. Show in the figure \ref{resulttest}

% figures -----------------------------------
\begin{figure}[ht!] % h-here t-top b-bottom p-new page !-force
    \centering
    \includegraphics[width=1\textwidth]{images/result.jpg}
    \caption{My test image}
    \label{resulttest} % reference
\end{figure}

% reference ---------------------------------
\newpage
% \bibliographystyle{abbrv} % use abbrv. author name
\bibliographystyle{plain} % use numbered list [1]
% \bibliographystyle{unsrt} % unsort reference (in paper)
% \bibliographystyle{alpha} % use author name list [BSB01]
\bibliography{template}

% list of figures ---------------------------
\newpage
\listoffigures

\end{document}


\documentclass[11pt]{article}

\usepackage[margin=1in]{geometry}
\usepackage{setspace}
\onehalfspacing
\usepackage{graphicx}
\graphicspath{report_images/}
\usepackage{appendix}
\usepackage{listings}
\usepackage{float}
\usepackage{amsthm}

% DOCUMENT INFORMATION =================================================
\title {ECEN 429: Introduction to Digital Systems Design Laboratory \\ North Carolina Agricultural and Technical State University \\ Department of Electrical and Computer Engineering} % Declare Title
\author{Reporter: Nikiyah Beaulah\\ \and Partner: Chris Cannon} % Declare authors
\date{February 8, 2018}
% ======================================================================

\begin{document}

\maketitle % Render Title, Author, and Date

\begin{center}
Lab	2
\end{center}

\pagebreak

\section{Introduction}
For lab 2 our task was to utilize the seven-segment displays and practice implementing decoders and full adders.  During these steps we have to create truth tables to gather the right information. We use these truth tables to code each program in VHDL also so that we have the right implemented display on our Basys 3 board. We have three programming assignments for this lab. The first assignment was to write VHDL code to map out a seven segment display where it consist numbers 0-9 to display on the board. The second assignment was to write a VHDL code and create a truth table for a 1:2 decoder using two inputs and two outputs also using a AND and OR gate.  The third assignment was to write a VHDL code for the expression output SUM, considering we have a full adder with three inputs A,B, and Carryin and two outputs SUM and Carry out.

\section{Background, Design Solution, and Results}

\subsection{Problem 1 Seven Segment Display}

\subsubsection{Background}
This problem prompted us to utilize the seven-segment display on the Basys3 board. Our input will be a 3-bit number so that we can provide input from 0-9, which is the range that can be displayed by a single seven-segment display. Seven-segment displays include, fittingly, seven segments. Those segments are conventionally referred to as a-g in accordance with Figure ~\ref{fig:sevenSegMap}.

\begin{figure}[h]
\begin{center}
\includegraphics[width=0.2\textwidth]{report-images/img1.png}
\caption{Seven-Segment Display Map}
\label{fig:sevenSegMap}
\end{center}
\end{figure}

\subsubsection{Design Solution}
As discussed in the background for this problem, a seven-segment display can only display a single-digit 0-9. We have to use a 4-bit number in order to represent 9 (1001b) however, 10-15 (1010b-1111b) cannot be displayed using a single seven-segment display. In our code, this will be treated as an error condition where no data will be displayed on the seven-segment display. The 4-bit input will be x3 down to x0, with x3 as the most significant bit, and the output bits have been defined in the assignment. The input pin assignments are defined in Table ~\ref{tab:sevenSegInput} and the output pin assignments as well as their segment are defined in Table ~\ref{tab:sevenSegOutput}. The truth table for our seven segment decoder is shown in Table ~\ref{tab:sevenSegTruthTable}. During our design process, we remembered that the seven-segment display is "active-low", meaning that a '0' is treated as "ON" and a '1' is treated as "OFF". This was important when designing our truth table.

\begin{table}[h]
\begin{center}
	\begin{tabular}{| l | l | l |}
		\hline
		Bit & Label & Address \\ \hline
		x3 & Switch 3 & V17 \\ \hline
		x2 & Switch 2 & V16 \\ \hline
		x1 & Switch 1 & W16 \\ \hline
		x0 & Switch 0 & W15 \\ \hline
	\end{tabular}
	\caption{\label{tab:table-name}Input pin assignments for seven-segment decoder.}
	\label{tab:sevenSegInput}
\end{center}
\end{table}

\begin{table}[h]
\begin{center}
	\begin{tabular}{| l | l | l |}
		\hline
		Bit & Seven-Segment & Address \\ \hline
		out1 & a & W7 \\ \hline
		out2 & b & W6 \\ \hline
		out3 & c & U8 \\ \hline
		out4 & d & V8 \\ \hline
		out5 & e & U5 \\ \hline
		out6 & f & V5 \\ \hline
		out7 & g & U7 \\ \hline
	\end{tabular}
	\caption{\label{tab:table-name}Output pin assignments for seven-segment decoder.}
	\label{tab:sevenSegOutput}
\end{center}
\end{table}

\begin{table}[h]
\begin{center}
	\begin{tabular}{| l | l | l | l | l | l | l | l | l | l | l |}
		\hline
		x3 & x2 & x1 & x0  & out1 & out2 & out3 & out4 & out5 & out6 & out7\\ \hline
		0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \hline
		0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline
		0 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 \\ \hline
		0 & 0 & 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 & 0 \\ \hline
		0 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 1 & 0 & 0 \\ \hline
		0 & 1 & 0 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 \\ \hline
		0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ \hline
		0 & 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ \hline
		1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \hline
		1 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ \hline
		1 & 0 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
		1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
		1 & 1 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
		1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
		1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
		1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
	\end{tabular}
	\caption{\label{tab:table-name}Truth table for seven-segment decoder.}
	\label{tab:sevenSegTruthTable}
\end{center}
\end{table}

The circuit design representing a driver for a seven-segment display is displayed in Figure ~\ref{fig: sevenSegSchematic}.

\begin{figure}[H]
\begin{center}
	\includegraphics[width=0.75\textwidth]{report-images/img2.png}
	\caption{\label{fig: sevenSegSchematic} Circuit schematic for binary-coded decimal to seven-segment display driver.}
	\label{fig:sevenSegSchematic}
\end{center}
\end{figure}

\subsubsection{Results}

The resulting circuit successfully displayed the digit corresponding to each 4-bit number from 0-9. We were also able to correctly turn off all displays when an input greater than 9 was provided. Initially, the number 6 displayed incorrectly, we activated segment 'f' by mistake, therefore displaying '8'. Once this problem was identified, we were able to quickly address it and provide the correct output.

\begin{center}
	For the figures ~\ref{fig:sevenSegZero} through ~\ref{fig:sevenSegNine}, switches boxed in red are turned off and switches boxed in green are turned on.
\end{center}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/0.png}
\caption{Seven-Segment Display for "0000"}
\label{fig:sevenSegZero}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/1.png}
\caption{Seven-Segment Display for "0001"}
\label{fig:sevenSegOne}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/2.png}
\caption{Seven-Segment Display for "0010"}
\label{fig:sevenSegTwo}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/3.png}
\caption{Seven-Segment Display for "0011"}
\label{fig:sevenSegThree}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/4.png}
\caption{Seven-Segment Display for "0100"}
\label{fig:sevenSegFour}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/5.png}
\caption{Seven-Segment Display for "0101"}
\label{fig:sevenSegFive}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/6.png}
\caption{Seven-Segment Display for "0110"}
\label{fig:sevenSegSix}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/7.png}
\caption{Seven-Segment Display for "0111"}
\label{fig:sevenSegSeven}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/8.png}
\caption{Seven-Segment Display for "0100"}
\label{fig:sevenSegEight}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part1/9.png}
\caption{\label{fig: fig-name} Seven-Segment Display for "1001"}
\label{fig:sevenSegNine}
\end{center}
\end{figure}

\pagebreak

\subsection{Problem 2 1:2 Decoder}

\subsubsection{Background}
The task posed for this problem was to develop a 1:2 decoder and implement it on the Basys3. The design will include 2 inputs, a 1-bit number and an enable bit.

\subsubsection{Design Solution}
The 1:2 decoder will accept a 1-bit input 'a', and is controlled by the bit 'sel'. The output is made up of bits 'out1' and 'out2'. The ports are described in Table ~\ref{tab:decoderPort}.

\begin{table}[h]
\begin{center}
		\begin{tabular}{| l | l | l |}
			\hline
			Bit & Label & Address \\ \hline
			a & Switch 0 & V17 \\ \hline
			sel & Switch 1 & V16 \\ \hline
			out1 & LED 0 & U16 \\ \hline
			out2 & LED 1 & E19 \\ \hline
		\end{tabular}
		\caption{\label{tab: tab-name} Input and output port assignments for 1:2 decoder.}
		\label{tab:decoderPort}
\end{center}
\end{table}

The decoder will only produce input when 'sel', the enable bit, is HIGH. When 'sel' is HIGH, the decoder will activate the 'out1' bit, representing the decimal value "0". When 'a' is HIGH when 'sel' is HIGH, the decoder will activate the 'out2' bit, representing the decimal value "1". This is summarized in Table ~\ref{tab:decoderTruthTable}.

\begin{table}[h]
\begin{center}
	\begin{tabular}{| l | l | l | l |}
		\hline
		a & sel & out1 & out2 \\ \hline
		0 & 0 & 0 & 0 \\ \hline
		0 & 1 & 1 & 0 \\ \hline
		1 & 0 & 0 & 0 \\ \hline
		1 & 1 & 0 & 1 \\ \hline
	\end{tabular}
	\caption{\label{tab:decoderTruthTable} Truth table for 1:2 decoder.}
	\label{tab:decoderTruthTable}
\end{center}
\end{table}

The circuit design representing a 1:2 decoder is shown in Figure ~\ref{fig: decoderSchematic}.

\begin{figure}[H]
\begin{center}
	\includegraphics[width=0.75\textwidth]{report-images/img3.png}
	\caption{\label{fig: decoderSchematic} Circuit schematic for 1:2 decoder.}
	\label{fig:decoderSchematic}
\end{center}
\end{figure}

\subsubsection{Results}

Our first test of the 1:2 decoder revealed a flaw, where one of the if statements in our code had its boolean logic flipped. However, after briefly troubleshooting the issue, we were able to adjust the code accordingly and retrieve appropriate output. Figures ~\ref{fig:decoderImgOne} through ~\ref{fig:decoderImgFour} show the output displayed under all possible inputs. Switches outlined in red are giving HIGH or '1' input, and switches outlined in green are giving LOW or '0' input. Switches not outlined are not utilized by our design. Outlines are for the reader's information only.

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part2/IMG_0445.jpg}
\caption{Input is '0', decoder is not active.}
\label{fig:decoderImgOne}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part2/IMG_0446.jpg}
\caption{Input is '0', decoder is active.}
\label{fig:decoderImgTwo}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part2/IMG_0447.jpg}
\caption{Input is '1', decoder is active.}
\label{fig:decoderImgThree}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part2/IMG_0448.jpg}
\caption{Input is '1', decoder is not active.}
\label{fig:decoderImgFour}
\end{center}
\end{figure}

\pagebreak

\subsection{Problem 3 SUM of a Full Adder}

\subsubsection{Background}

A full adder is designed to incorporate both a carry-in and carry-out bit, which makes it more modular and allows us to build multi-bit adders. For this problem, we are to design a full adder that will accept a carry-in, a carry-out, and perform addition of two bits.

\theoremstyle{definition}
\newtheorem{definition}{Definition}
\begin{definition}
Carry-in Bit: the carry-in bit represents a 1-bit overflow from a previous addition operation. Because a full adder is intended to be modular, and build adders of multiple bits, it is important that the previous operation be available. For purposes of this lab, the carry-in for a 1 bit adder will be tied to ground.
\end{definition}

\begin{definition}
	Carry-out Bit: the carry-out bit represents a 1-bit overflow from the current addition operation. This can either be used to signal an error condition or to tie this adder in with multiple full adders. The result of this addition operation will either between 0 and 3. If the output is 2 or 3, the carry-out bit will be HIGH to signal that there is an additional bit needed to represent the results of this operation.
\end{definition}

\subsubsection{Design Solution}

For our solution, the two 1-bit inputs are designated as 'A' and 'B'. Carry-in is denoted as 'CI' and carry-out is denoted as 'CO'. The output sum of the adder will be represented by 'SUM'. The truth table for a full adder is represented in Table ~\ref{tab:fullAddTruthTable}.

\begin{table}[h]
\begin{center}
	\begin{tabular}{| l | l | l | l | l |}
		\hline
		A & B & CI & SUM & CO \\ \hline
		0 & 0 & 0 & 0 & 0 \\ \hline
		0 & 0 & 1 & 1 & 0 \\ \hline
		0 & 1 & 0 & 1 & 0 \\ \hline
		0 & 1 & 1 & 0 & 1 \\ \hline
		1 & 0 & 0 & 1 & 0 \\ \hline
		1 & 0 & 1 & 0 & 1 \\ \hline
		1 & 1 & 0 & 0 & 1 \\ \hline
		1 & 1 & 1 & 1 & 1 \\ \hline
	\end{tabular}
	\caption{\label{tab:fullAddTruthTable}Truth table for a full adder.}
	\label{tab:fullAddTruthTable}
\end{center}
\end{table}

Our full bit adder uses switches as all inputs and LEDs as outputs. The port assignment for the inputs and outputs is specified in Table ~\ref{tab:fullAddPorts}.

\begin{table}[h]
\begin{center}
	\begin{tabular}{| l | l | l |}
		\hline
		Bit & Label & Port \\ \hline
		A & Switch 0 & V17 \\ \hline
		B & Switch 1 & V16 \\ \hline
		CI & Switch 2 & W16 \\ \hline
		CO & LED 1 & E19 \\ \hline
		SUM & LED 0 & U16 \\ \hline
	\end{tabular}
	\caption{\label{tab:fullAddPorts}Port assignment for full adder.}
	\label{tab:fullAddPorts}
\end{center}
\end{table}

The circuit for our full adder is shown in Figure ~\ref{fig: adderSchematic}.

\begin{figure}[H]
\begin{center}
	\includegraphics[width=0.75\textwidth]{report-images/img4.png}
	\caption{\label{fig: adderSchematic} Circuit schematic for a single bit full adder.}
	\label{fig:adderSchematic}
\end{center}
\end{figure}

\subsubsection{Results}

The full adder behaved as expected, displaying a carry-out bit if the input exceeded 1. This is because a 1-bit number can only represent 0 or 1. Since the possible outcomes for this operation were 0 through 3, overflow was possible. In the Figures for this result Figures ~\ref{fig:fullAdderImgOne} through ~\ref{fig:fullAdderImgEight}, switches are highlighted in red to show LOW, or '0' input, and green to show HIGH, or '1' input. This highlighting is purely for the convenience of the reader.

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0449.jpg}
\caption{Addition of 0 + 0 with no carry-in.}
\label{fig:fullAdderImgOne}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0450.jpg}
\caption{Addition of 0 + 1 with no carry-in.}
\label{fig:fullAdderImgTwo}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0451.jpg}
\caption{Addition of 1 + 0 with no carry-in.}
\label{fig:fullAdderImgThree}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0452.jpg}
\caption{Addition of 1 + 1  with no carry-in.}
\label{fig:fullAdderImgFour}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0453.jpg}
\caption{Addition of 0 + 0  with '1' carried in.}
\label{fig:fullAdderImgFive}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0454.jpg}
\caption{Addition of 0 + 1 w with '1' carried in.}
\label{fig:fullAdderImgSix}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0455.jpg}
\caption{Addition of 1 + 0  with '1' carried in.}
\label{fig:fullAdderImgSeven}
\end{center}
\end{figure}

\begin{figure}[H]
\begin{center}
\includegraphics[width=0.5\textwidth]{report-images/Part3/IMG_0456.jpg}
\caption{Addition of 1 + 1  with '1' carried in.}
\label{fig:fullAdderImgEight}
\end{center}
\end{figure}

\pagebreak

\section{Conclusion}

During this lab, we were able to successfully complete and document all three design problems. As mentioned in our design processes above, we had several instances where a boolean expression was reserved or some small coding error caused our design to fail. However, this served as a great learning experience because we learned how to debug with the hardware in our hands. For example, when writing the code for the 1:2 decoder, the case where 'A' was active and 'sel' was off was reversed, so we wound up activating the zero output bit when the decoder was not supposed to be one. After observing this phenomenon and comparing it to our code, we identified the line with the error and made the appropriate adjustments. One technical challenge during this lab was the "active-low" output required to work with the seven-segment display. In much of our early electrical and computer engineering classes, "on" is almost always synonymous with digital HIGH. Converting our thought process to make a truth table where '0' represented an output that was on proved difficult, but we were successful. Now that this lab is completed, one interesting tool we now have is the ability to scale our full adder to do multiple bit arithmetic. Once we have a full adder component, it is trivial to scale it to as many bits as we'd like. The building blocks we have developed in this lab will surely be instrumental in future labs and our design processes in our careers.

\pagebreak

\begin{appendices}

	\section{Problem 1 VHDL Code}

	\begin{lstlisting}[language=VHDL]
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity Lab2Part1 is
    Port ( x : in STD_LOGIC_VECTOR(3 downto 0);
           da : out STD_LOGIC;
           db : out STD_LOGIC;
           dc : out STD_LOGIC;
           dd : out STD_LOGIC;
           de : out STD_LOGIC;
           df : out STD_LOGIC;
           dg : out STD_LOGIC);
end Lab2Part1;

architecture Behavioral of Lab2Part1 is

begin

    process(x)
        begin
        case x is
        when "0000" =>
            da <= '0';
            db <= '0';
            dc <= '0';
            dd <= '0';
            de <= '0';
            df <= '0';
            dg <= '1';
        when "0001" =>
            da <= '1';
            db <= '0';
            dc <= '0';
            dd <= '1';
            de <= '1';
            df <= '1';
            dg <= '1';
        when "0010" =>
            da <= '0';
            db <= '0';
            dc <= '1';
            dd <= '0';
            de <= '0';
            df <= '1';
            dg <= '0';
        when "0011" =>
            da <= '0';
            db <= '0';
            dc <= '0';
            dd <= '0';
            de <= '1';
            df <= '1';
            dg <= '0';
        when "0100" =>
            da <= '1';
            db <= '0';
            dc <= '0';
            dd <= '1';
            de <= '1';
            df <= '0';
            dg <= '0';
        when "0101" =>
            da <= '0';
            db <= '1';
            dc <= '0';
            dd <= '0';
            de <= '1';
            df <= '0';
            dg <= '0';
        when "0110" =>
            da <= '0';
            db <= '1';
            dc <= '0';
            dd <= '0';
            de <= '0';
            df <= '0';
            dg <= '0';
        when "0111" =>
            da <= '0';
            db <= '0';
            dc <= '0';
            dd <= '1';
            de <= '1';
            df <= '1';
            dg <= '1';
        when "1000" =>
            da <= '0';
            db <= '0';
            dc <= '0';
            dd <= '0';
            de <= '0';
            df <= '0';
            dg <= '0';
        when "1001" =>
            da <= '0';
            db <= '0';
            dc <= '0';
            dd <= '0';
            de <= '1';
            df <= '0';
            dg <= '0';
        when others =>
            da <= '1';
            db <= '1';
            dc <= '1';
            dd <= '1';
            de <= '1';
            df <= '1';
            dg <= '1';
        end case;
    end process;
end Behavioral;
	\end{lstlisting}

	\section{Problem 1 Constraints File}

	\begin{figure}[h]
	\includegraphics[width=\textwidth]{report-images/img5}
	\end{figure}

	\section{Problem 2 VHDL Code}

	\begin{lstlisting}[language=VHDL]
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity Lab2Part1 is
    Port ( a : in STD_LOGIC;
           sel : in STD_LOGIC;
           out1 : out STD_LOGIC;
           out2 : out STD_LOGIC);
end Lab2Part1;

architecture Behavioral of Lab2Part1 is

begin

out1 <= (not a) and sel;
out2 <= a and sel;

end Behavioral;
	\end{lstlisting}

	\section{Problem 2 Constraints File}

	\begin{figure}[h]
	\includegraphics[width=\textwidth]{report-images/img6}
	\end{figure}

	\section{Problem 3 VHDL Code}

	\begin{lstlisting}[language=VHDL]
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity Lab2Part3 is
    Port ( cin : in STD_LOGIC;
           a : in STD_LOGIC;
           b : in STD_LOGIC;
           cout : out STD_LOGIC;
           sum : out STD_LOGIC);
end Lab2Part3;

architecture Behavioral of Lab2Part3 is

begin

sum <= (a and b and cin) or (a and (not b) and (not cin)) or (b and
	(not a) and (not cin)) or (cin and (not a) and (not b));
cout <= (cin and (a or b)) or (a and b);

end Behavioral;
	\end{lstlisting}

	\section{Problem 3 Constraints File}

	\begin{figure}[h]
	\includegraphics[width=\textwidth]{report-images/img7}
	\end{figure}

\end{appendices}

\end{document}


\chapter[]{Time-domain analysis of SDOF systems}

\section{Mathematic modelling of lumped-parameter systems}

\section{Free vibration of SDOF systems}

\section{Dynamic behaviour of undamped SDOF systems under harmonic excitation}

\section{Viscous-damped SDOF systems under Harmonic excitation}

\section{Expansion to periodic excitation via Fourier series}


\documentclass[a4paper,12pt]{report}
\usepackage{hyperref}

\begin{document}

\title{MSE160 Notes: Materials NTK}
\author{Eli Scott and Aman Bhargava}
\date{\today}
\maketitle

\tableofcontents

\section{Basic Properties}
\begin{itemize}
\item \textbf{Resilience}: Ability to absorb energy and recover it upon unloading (elastic energy)
\item \textbf{Tensile Strength}: Absolute peak stress on stress-strain curve.
\item \textbf{Fracture Toughness}: $K_c$, Fracture toughness (amount of energy absorbed before rupture)
\item \textbf{Polymorphism}: Ability to have more than one molecular structure.
\item \textbf{Amorphous}: Non-crystalline solid w/o long-range order.
\item \textbf{Yield Stress}: Stress that causes 0.2\% elastic deformation.
\item \textbf{Cathodic Protection}: When you use an anodic material to protect a cathodic material from corrosion.

\end{itemize}

\section{Material Design}
\begin{enumerate}
\item Choose what to min/max
\item Decide on functionality (supporting, min deflection)
\item Decide objectives
\item Decide constraints
\item Figure out free var
\item Derive function for objective and constraints
\item Eliminate free variable using constraints
\item Solve for geometric/functional/material parameters
\item Always maximize
\item Cost parameter = material index/cost per kg
\end{enumerate}

\section{Crystal Structures}
\paragraph{Theoretical Density: } $$\rho = \frac{n*A/N_a}{V_c}$$ Where $n$ is the number of atoms per unit cell, $A$ is the atomic mass, $N_a$ is avogadro's number, and $V_c$ is the volume of the unit cell.
\subsection{Simple Cubic}
\begin{itemize}
\item Packing efficiency: 52.4\%
\item Coordination number: 6
\item One atom at each corner.
\end{itemize}

\subsection{Body Centered Cubic}
\begin{itemize}
\item Packing efficiency: 68\%
\item Coordination number: 8
\item One atom in the middle and one atom at each corner.
\item Slip system: plane = $(0, 1, 1)$, direction = $<1, 1, 1>$
\end{itemize}

\subsection{Face Centered Cubic}
\begin{itemize}
\item Packing efficiency: 74\%
\item Coordination number: 12
\item Eigth of atom at corners. Half atoms on each face.
\item Slip system: plane = $(1, 1, 1)$, direction = $<1, 1, 1>$
\end{itemize}

\paragraph{Trend for coordination number in ionic crystal} $~\frac{r_{cation}}{r_{anion}}$

\subsection{Ductile-Brittle Transition}
BCC and polymers experience transition from brittle to ductile from low to high temperatures.

\section{Strength of Crystalline Materials (Metals)}
\subsection{4 Strengthening Mechanisms: }
\begin{enumerate}
\item Alloying (larger and smaller atoms)
\item Precipitation/"particle" strengthening (relies on the fact that distortions can't easily pass through graini boundaries)
\item Cold work/work hardening
\item Grain size reduction
\end{enumerate}

\paragraph{Equation to fit work hardening stress-strain response}
$$\sigma_t = K(\epsilon_t)^n$$
Where $t$ indicates 'true' as opposed to engineering values, $K$ is a constant and $n$ is the hardening exponent.

\subsection{Conditions for Substitutional Solid Solutions}
\begin{itemize}
\item $\Delta r < 15\%$
\item Similar electronegativities
\item Same crystal structures
\item Similar valences
\end{itemize}

\paragraph{Empiric relationship between $\sigma_y$ and concentration of alloy $C$: } $sigma_y ~ \sqrt{C}$

\subsection{Resolved Shear Stress: }
$$\tau_r = \sigma_o*cos(\lambda)*cos(\phi)$$
$\lambda$ is the angle between the $\sigma_o$ and the plane, $\phi$ is the angle between the normal of the plane and ${\sigma}_o$

$\tau_{crss} = \frac{\sigma_{y}}{2}$ is the critical resolved shear stress.

\subsection{Defects}
Types of Defects:
\begin{enumerate}
\item Point defects (vacancy, self-interstitial, substitutions)
\item Linear defects (dislocations)
\item Area defects (grain bounds)
\end{enumerate}

\paragraph{Conditions for dislocation motion: } $\tau_r > \tau_{crss}$

\subsection{Equilibrium Concentration of Point Defects}
$$\frac{N_v}{N} = exp(\frac{-Q_v}{kT}$$ Where $\frac{N_v}{N}$ is the ratio of vacancies to potential vacancies (i.e. number of atoms), $Q_v$ is activiatioin energy, $k$ is Boltzmann's constant, and $T$ is temperature.

\subsection{Fatigue}
Fatigue is cyclic stressing. Here are the main parameters:
\begin{enumerate}
\item $S$: amplitude of cyclic stress
\item $\sigma_0$: mean stress
\item Frequency of stressing
\end{enumerate}

$$\frac{da}{dN} = \Delta K^{m}$$
Where a = half crack length, N = number of cycles, $\Delta K ~ \Delta \sigma * \sqrt(a)$, m is a constant

\textbf{$S_{fat}$ is the minimum $S$ for fatigue behavior.}

\subsection{Hall-Petch Equation}
Gives you $\sigma_y$ from grain size.
$$\sigma_y = sigma_0 + k_y * d^{-1/2}$$
Where $\sigma_0$ and $k_y$ are constants and $d$ is the diameter of the grain size.

\section{C R A C K}
\subsection{Griffith's Formula: $\sigma_m$}
Crack tip stress. $$\sigma_m = 2*\sigma_o*\sqrt{a/p_t}$$
Where $p_t$ is the radius of curvature, a is half crack length, and $\sigma_o$ is the applied stress.

\paragraph{Propagation criterion: } When $\sigma_m > \sigma_c$

\subsection{$\sigma_c$}
Critical stress, $$\sigma_c = \sqrt{2E\gamma_s}{\pi*a}$$ Where $E$ is modulus of elasticity, $\gamma_s$ is the specific surface energy (add $\gamma_p$ = plastic deformation energy if ductile), $a$ is half crack length.

Basically just the stress that cracks can withstand before propagating.

\subsection{$K$}
At crack failure, $$K_c = Y*\sigma*\sqrt{\pi*a}$$ Where $K_c$ is fracture toughness of material, $Y$ is a constant, and $a$ is the half crack width.

Two failure cases here: either cracks are too large or $\sigma$ is too large.

\section{Electrical Properties}

\subsection{4 Ways to Increase Resistivity?}
\begin{enumerate}
\item Increase temperaturre
\item Increase grain boundaries, dislocations
\item Add impurities
\item Add vacancies
\end{enumerate}

\paragraph{Calculating resistance of a sample: } $R = \frac{L}{A*\sigma}$

\subsection{P vs. N-Type Doping}
P-type brings the acceptor band down significantly. N-type brings the donor band up. P-type results in more free holes, N-type results in more free electrons.

\paragraph{Current Density} = $J = I/A$ = current/cross sectional area.
\paragraph{Electric Field Potential} = $E = V/L$

\subsection{New Ohm's Law}
$$J = \sigma*E$$

\subsection{Calculating $\sigma$}
$$\sigma = n|e|\mu_e + p|e|\mu_h$$
Where $n$ is the number of mobile electrons, $p$ is the number of holes, $\mu_e$ is electron mobility and $\mu_h$ is the hole mobility.

\paragraph{Voltage threshold for semiconductors: $2eV$}

\end{document}


\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{microtype}      % microtypography
\usepackage{array}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{amsthm}

%% Header
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[C]{CS 136 - 2022s - HW0 Submission}
\fancyfoot[C]{\thepage} % page number
\renewcommand\headrulewidth{0pt}
\pagestyle{fancy}

%% Hyperlinks always blue, no weird boxes
\usepackage[hyphens]{url}
\usepackage[colorlinks=true,allcolors=black,pdfborder={0 0 0}]{hyperref}

%%% Doc layout
\usepackage{parskip}
\usepackage{times}

%%% Write out problem statements in blue, solutions in black
\usepackage{color}
\newcommand{\officialdirections}[1]{{\color{blue} #1}}

%%% Avoid automatic section numbers (we'll provide our own)
\setcounter{secnumdepth}{0}

\begin{document}
~~\\ %% add vert space

{\Large{\bf Student Name: TODO}}

{\Large{\bf Collaboration Statement:}}

Turning in this assignment indicates you have abided by the course Collaboration Policy:

\url{www.cs.tufts.edu/comp/136/2022s/index.html#collaboration-policy}

Total hours spent: TODO

I consulted the following resources:
\begin{itemize}
\item TODO
\item TODO
\item $\ldots$
\end{itemize}

FYI Official instructions for all problems can be found at:

\url{www.cs.tufts.edu/comp/136/2022s/hw0.html}

\tableofcontents

\newpage
\officialdirections{
\subsection*{1a: Problem Statement}

Across the whole population, the chance that one individual has the disease is 4\%.

Formalize this statement by defining a random variable "D". What is the sample space of D? What is the PMF?
}

\subsection{1a: Solution}
TODO YOUR SOLUTION HERE

\newpage

\officialdirections{
\subsection*{1b: Problem Statement}

If the person has the disease, the light becomes red is 100\%.
If the person does not have the disease, the probability the light turns green is 95\%.

Write the above statements using conditional probabilities with random variable "D" for disease and "L" for indicator light result.
}

\subsection{1b: Solution}
TODO YOUR SOLUTION HERE

\newpage

\officialdirections{
\subsection*{1c: Prompt}

Show the joint probability table for all possible configurations of D and L.
}

\subsection{1c: Solution}
TODO YOUR SOLUTION HERE

\newpage

\officialdirections{
\subsection*{1d: Prompt}

For a random individual whose disease status is unknown, you run the test and observe a red light. What is the probability they have the disease?
}

\subsection{1d: Solution}
TODO YOUR SOLUTION HERE

\end{document}


\documentclass[twoside]{article}
\setlength{\oddsidemargin}{0.25 in}
\setlength{\evensidemargin}{-0.25 in}
\setlength{\topmargin}{-0.6 in}
\setlength{\textwidth}{6.5 in}
\setlength{\textheight}{8.5 in}
\setlength{\headsep}{0.75 in}
\setlength{\parindent}{0 in}
\setlength{\parskip}{0.1 in}

%
% ADD PACKAGES here:
%

\usepackage{amsmath,amsfonts,graphicx,mathdots}
\usepackage{tcolorbox}
\usepackage{enumitem}

\newcounter{lecnum}
\renewcommand{\thepage}{\thelecnum-\arabic{page}}
\renewcommand{\thesection}{\thelecnum.\arabic{section}}
\renewcommand{\theequation}{\thelecnum.\arabic{equation}}
\renewcommand{\thefigure}{\thelecnum.\arabic{figure}}
\renewcommand{\thetable}{\thelecnum.\arabic{table}}
\newcommand{\N}{\operatorname{\mathcal{N}}}
\renewcommand{\t}{^\mathrm{T}{}}
\newcommand{\E}{\mathrm{E}{}}
\newcommand{\tr}{\mathrm{tr}{}}
\renewcommand{\k}{_k{}}
\newcommand{\kp}{_{k+1}{}}
\newcommand{\kpk}{_{k+1\mid k}{}}
\newcommand{\kpkp}{_{k+1\mid k+1}{}}
\newcommand{\kk}{_{k\mid k}{}}
\DeclareMathOperator*{\argmin}{argmin}

%
% The following macro is used to generate the header.
%
\newcommand{\lecture}[4]{
   \pagestyle{myheadings}
   \thispagestyle{plain}
   \newpage
   \setcounter{lecnum}{#1}
   \setcounter{page}{1}
   \noindent
   \begin{center}
   \framebox{
      \vbox{\vspace{2mm}
    \hbox to 6.28in { {\bf EE402 - Discrete Time Systems
	\hfill Fall 2021} }
       \vspace{4mm}
       \hbox to 6.28in { {\Large \hfill Lecture #1 \hfill} }
       \vspace{2mm}
       \hbox to 6.28in { {\it Lecturer: #2 \hfill } }
      \vspace{2mm}}
   }
   \end{center}
   \markboth{Lecture #1}{Lecture #1}

   \vspace*{4mm}
}

\renewcommand{\cite}[1]{[#1]}
\def\beginrefs{\begin{list}%
        {[\arabic{equation}]}{\usecounter{equation}
         \setlength{\leftmargin}{2.0truecm}\setlength{\labelsep}{0.4truecm}%
         \setlength{\labelwidth}{1.6truecm}}}
\def\endrefs{\end{list}}
\def\bibentry#1{\item[\hbox{[#1]}]}

\newcommand{\fig}[3]{
			\vspace{#2}
			\begin{center}
			Figure \thelecnum.#1:~#3
			\end{center}
	}

% Use these for theorems, lemmas, proofs, etc.
\newtheorem{theorem}{Theorem}[lecnum]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}
\newenvironment{proof}{{\bf Proof:}}{\hfill\rule{2mm}{2mm}}

% **** IF YOU WANT TO DEFINE ADDITIONAL MACROS FOR YOURSELF, PUT THEM HERE:

\begin{document}

% Lecture Details
\lecture{18}{Elif Sarıtaş}

\par

\section*{Preliminaries}

\subsection*{Multivariate Normal Distribution}
A multivariate normal distribution for $x\in\mathbb{R}^{n_x}$
\begin{align*}
	p(x) =& \N(x;\mu,\Sigma)\\
	=&\frac{1}{\sqrt{\textrm{det}(2\pi\Sigma)}} \exp\left\lbrace-\frac{1}{2}(x-\mu)\t\Sigma^{-1}(x-\mu) \right\rbrace
\end{align*}
where
\begin{align*}
	\mu =& \E\left[x \right] \\
	\Sigma =& \E\left[(x-\mu)(x-\mu)\t \right] \\
\end{align*}
Remember that any covariance matrix is
\begin{enumerate}[label=\roman*.]
	\item a square matrix (in this case $\Sigma \in \mathbb{R}^{n_x\times n_x}$)
	\item symmetric, i.e., $\Sigma = \Sigma\t$
	\item positive semi-definite, i.e., $a\t\Sigma a \geq 0,\hspace{0.2cm}\forall a$
\end{enumerate}
\paragraph{Marginal Distribution:}
Given that $x\sim\N(x;\mu,\Sigma)$ where
$x = \left[\begin{array}{cc}
		x_a & x_b
	\end{array} \right]\t$, $
	\mu = \left[\begin{array}{cc}
		\mu_a & \mu_b
	\end{array} \right]\t $, and $
	\Sigma = \left[\begin{array}{cc}
		\Sigma_a & \Sigma_c\\
		\Sigma_c & \Sigma_b
	\end{array} \right] $, then
\begin{align*}
	p(x_a) =& \N(x_a;\mu_a,\Sigma_a)\\
	p(x_b) =& \N(x_b;\mu_b,\Sigma_b)
\end{align*}

\paragraph{Conditional Distribution:}
Given that $x\sim\N(x;\mu,\Sigma)$ where
$x = \left[\begin{array}{cc}
	x_a & x_b
\end{array} \right]\t$, $
\mu = \left[\begin{array}{cc}
	\mu_a & \mu_b
\end{array} \right]\t $, and $
\Sigma = \left[\begin{array}{cc}
	\Sigma_a & \Sigma_c\\
	\Sigma_c & \Sigma_b
\end{array} \right] $, then
\begin{align*}
	p(x_a\mid x_b) =& \N(x_a;\mu_{a\mid b},\Sigma_{a\mid b})\\
\end{align*}
where
\begin{align*}
	\mu_{a \mid b} =& \mu_a + \Sigma_c \Sigma_b^{-1}(x_b-\mu_b)\\
	\Sigma_{a \mid b} =& \Sigma_a - \Sigma_c\Sigma_b^{-1}\Sigma_c\t
\end{align*}
\paragraph{Linear Combinations:}
Linear combinations of jointly Gaussian densities are also Gaussian, which is one of the main reasons for Gaussian densities being this popular. Given two jointly normal random variables, $(x,y)\sim\N\left( \left[ \begin{array}{c}
	x\\y
\end{array}\right] ;\left[ \begin{array}{c}
\mu_x\\\mu_y
\end{array},\right],\left[ \begin{array}{cc}
\Sigma_x & \Sigma_{xy}\\ \Sigma_{y} & \Sigma_{yx}
\end{array}\right]\right)   $, then
\begin{align*}
	z =& Ax+By+c \\
	z \sim& \N(z;A\mu_x+B\mu_y+c,A\Sigma_xA\t+B\Sigma_yB\t+A\Sigma_{xy}B\t+B\Sigma_{yx}A\t)
\end{align*}
When $x$ and $y$ are independent, $\Sigma_{xy}=0$, then covariance matrix of the $z$ becomes
\begin{align*}
	z \sim& \N(z;A\mu_x+B\mu_y+c,A\Sigma_xA\t+B\Sigma_yB\t)
\end{align*}
\subsection*{Markov Property}
A process, $x$, is called is Markov, if
\begin{align*}
	p(x\k \mid x_{k-1},x_{k-2},\cdots,x_0) = p(x\k \mid x_{k-1})\hspace{0.2cm}\forall k.
\end{align*}
That is the transition between the states depend only on the previous state, which is a summary of the past.

\subsection*{Estimators}
An estimator is an algorithm to find one or more unknown values using observations. These unknowns may be
\begin{itemize}
	\item stochastic or deterministic,
	\item static or dynamic,
	\item finite dimensional or infinite dimensional.
\end{itemize}
There are three inference problems in the Bayesian framework.

	\paragraph{\textbf{Prediction}:} In this problem, the estimate of an $n$-step ahead future state is computed by considering the measurements up to time $k$. That is we deal with the following distribution
	\begin{align*}
		p(x_{k+n}\mid Y_k),\hspace{0.2cm} \textrm{where} \hspace{0.2cm}n=1,2,\cdots
	\end{align*}
	\paragraph{\textbf{Filtering}:} In this case, we estimate the state at time $k$ using the measurements up to and including the time $k$. The distribution we consider is
	\begin{align*}
		p(x_{k}\mid Y_k),\hspace{0.2cm}
	\end{align*}
	\paragraph{\textbf{Smoothing}:}This time all of the measurements in an interval is used to estimate the state at a time instant in that interval. Hence, the distribution of interest is now
	\begin{align*}
		p(x_{k}\mid Y_T),\hspace{0.2cm}\hspace{0.2cm} \textrm{where} \hspace{0.2cm}k<T,\cdots
	\end{align*}

\begin{figure}[!h]
	\centering
	\includegraphics[trim={0cm 0cm 0cm 0cm},clip,width=0.5\textwidth]{figures/inference}
	\caption{Inference problems (Courtesy of Murat Kumru)}
	\label{fig:Inference}
\end{figure}

\subsubsection*{Minimum Mean Square Error}
Mean square error (MSE) is a measure that demonstrates how much estimates deviate from their true values. It is used as an performance indicator for estimators, and it is defined as
\begin{align*}
	\textrm{MSE}_\theta = \E\left[(\theta-\hat \theta)\t(\theta-\hat \theta) \right]
\end{align*}
where $\theta \sim p(\theta)$,  and  $\hat\theta$ is the estimate.Minimum mean square error is the estimator that minimizes MSE.

\newpage

\section*{Discrete-Time Kalman Filter}
\subsection*{Problem Definition}
We consider a linear-Gaussian system, in which the models are linear and the noise characteristics are Gaussian.
\begin{align*}
	x\kp &= G x\k + H u\k + w\k
	\\
	y\k &= C x\k + v\k
\end{align*}
where
\begin{itemize}
	\item $x\k \in \mathbb{R}^{n_x}$ is the state,
	\item $y\k \in \mathbb{R}^{n_x}$ is the measurement,
	\item $w\k \in \mathbb{R}^{n_x}$ is the process noise with $w\k \sim \mathcal{N}(w\k;0,Q)$,
	\item $v\k \in \mathbb{R}^{n_x}$ is the measurement noise with $v\k \sim \mathcal{N}(v\k;0,R)$,
	\item $u\k$ is either deterministic or known, it may a function of known variables.

	\item We denote all of the measurements collected from the start up to  and including the time k as $Y\k = \left\lbrace y_0, y_1, \cdots,y_k \right\rbrace$.
\end{itemize}
The process noise captures the uncertainties regarding the system dynamics, whereas the measurements noise depends on the noise characteristics of the sensors. The probabilistic model for this system is written as
\begin{align*}
	p(x\kp\mid x\k) =& \N(x\kp;Gx\k+Hu\k,Q),\\
	p(y\k\mid x\k) =& \N(y\k;Cx\k,R).
\end{align*}
This system can be modelled using block diagrams as follows.
\begin{figure}[!h]
	\centering
	\includegraphics[trim={1cm 10cm 7cm 4cm},clip,width=0.75\textwidth]{figures/system}
	\caption{Block diagram of the system}
	\label{fig:system}
\end{figure}

The initial state is distributed according to
\begin{align*}
	x_0 \sim \N(x_0;\hat x_{0\mid0},P_{0\mid0}).
\end{align*}

We assume that $x_0$, $w_0$,  $w_1$,  $\cdots$ and $v_0$, $v_1$, $\cdots$ are  independent.

\textbf{Goal:} Given the measurements $y_0$, $y_1$, $\cdots$, $y_k$, find an estimate of $x\k$ such that the loss function, $\E\left[(x\k-\hat x\k)\t(x\k-\hat x\k) \right] $, is minimized.

\subsubsection*{Notation}
We will use the following notations throughout the derivation, the first one of the densities below is called the predicted density, whereas the latter is the posterior density.

\begin{align*}
	p(x\kp\mid Y\k) =& \N(x\kp; \hat x\kpk,P\kpk)\\
	p(x\k\mid Y\k) =& \N(x\k; \hat x\kk,P\kk)\\
\end{align*}
\begin{tcolorbox} [colback=blue!5!white,colframe=blue!75!black,title=\textbf{Remark}:,subtitle style={boxrule=0.4pt,
		colback=yellow!50!red!25!white}]
	If we can find a linear estimator, the above equations will hold; as the linear combinations of independent Gaussians are also Gaussian.
\end{tcolorbox}

\subsection*{Derivation}

Let us first consider the prediction problem, as it is quite simple given the motion model. The prediction problem aims to find $\E\left[x\kp \mid Y\k \right] $.
\begin{align*}
	\hat{x}\kpk =& \E\left[x\kp \mid Y\k \right]\\
	 =& \E\left[G x\k + H u\k + w\k \mid Y\k \right]\\
	=& \E\left[G x\k \mid Y\k \right] + \E\left[H u\k \mid Y\k \right] + \E\left[w\k \mid Y\k \right]\\
	=& G\E\left[x\k \mid Y\k \right] + H u\k \\
	=& G \hat x\kk + H u\k
\end{align*}
Note that the input, $u\k$, is deterministic and $\E\left[w\k \mid Y\k \right]=\E\left[w\k \right]=0 $.

The predicted error covariance is defined and computed as
\begin{align*}
	P\kpk =& \E\left[(x\kp-\hat x\kpk)(x\kp-\hat x\kpk)\t \mid Y\k \right]\\
	=& \E\left[(G x\k + H u\k + w\k- G \hat x\kk - H u\k)(G x\k + H u\k + w\k- G \hat x\kk - H u\k)\t \mid Y\k \right]\\
	=& \E\left[\big(G (x\k-\hat x\kk) + w\k\big)\big(G (x\k-\hat x\kk) + w\k\big)\t \mid Y\k \right]\\
	=& G\E\left[(x\k-\hat x\kk)(x\k-\hat x\kk)\t \mid Y\k \right]G\t +  \E\left[ w\k w\k\t \mid Y\k \right] \\
	=& G P \kk G\t + Q
\end{align*}
where we use the fact that $\E\left[ w\k w\k\t \mid Y\k \right] = Q$.

\begin{tcolorbox} [colback=blue!5!white,colframe=blue!75!black,title=\textbf{Remark}:,subtitle style={boxrule=0.4pt,
		colback=yellow!50!red!25!white}]
Both the predicted state and the predicted error covariance estimates depend on the filtered estimates on the previous time step.
\end{tcolorbox}

Now in order to find the filter estimates at time $k+1$, the idea is to use the predicted estimates and incorporate the new measurement arriving at time $k+1$ into the procedure. Thus, we start this part of the derivation by making the guess that the filtered state estimate is a linear combination of the predicted state and the additional information obtained from the new measurement. That is
\begin{align*}
	\hat x\kpkp = \hat x\kpk + K\kp (y\kp - \hat y\kpk)
\end{align*}
where $\hat y \kpk$ is the predicted measurement,
\begin{align*}
	\hat y \kpk = C \hat x \kpk,
\end{align*}
the predicted measurement error is named as innovation, and $K$ is an unknown matrix named as Kalman gain. We wish to determine the Kalman gain such that the loss function is minimized. Then,
\begin{align*}
	\hat x\kpkp =& \hat x\kpk + K\kp (Cx\kp + v\kp - C \hat x \kpk)\\
	P\kpkp =& \E\left[(x\kp-\hat x\kpkp)(x\kp-\hat x\kpkp)\t\right] \\
	=& \E\left[\big(x\kp-\hat x\kpk - K\kp (Cx\kp + v\kp - C \hat x \kpk)\big)\right. \\
	&\left. \big(x\kp-\hat x\kpk - K\kp (Cx\kp + v\kp - C \hat x \kpk)\big)\t  \right]\\
	=& \E\left[\big((I-K\kp C)(x\kp-\hat x\kpk) - K\kp v\kp\big) \big((I-K\kp C)(x\kp-\hat x\kpk) - K\kp v\kp\big)\t \right]\\
	=& (I-K\kp C)\E\left[(x\kp-\hat x\kpk) (x\kp-\hat x\kpk)\t \right](I-K\kp C)\t + K\kp\left[v\kp v\kp\t \right] K\kp\t\\
	& -2\E\left[K\kp v\kp(x\kp-\hat x\kpk)\t(I-K\kp C)\t \right] \\
	=& (I-K\kp C)P\kpk(I-K\kp C)\t + K\kp RK\kp \t\\
	=& P\kpk + K\kp C P\kpk C\t K\kp\t - K\kp C P\kpk - P\kpk C\t K\kp\t + K\kp RK\kp \t
\end{align*}

Remember that our aim is to find the Kalman gain that minimizes the loss function, i.e.,
\begin{align*}
	K\kp = \underset{K}{\argmin} \E\left[(x\kp-\hat x\kpkp)\t(x\kp-\hat x\kpkp) \right]
\end{align*}
We observe that loss function is equivalent to the trace of the filtered error covariance.
\begin{align*}
	\E\left[(x\kp-\hat x\kpkp)\t(x\kp-\hat x\kpkp) \right]  = \tr\left[ P\kpkp\right]
\end{align*}
Therefore, instead of minimizing the loss function we can minimize the trace of the filtered error covariance. For that purpose, we take the derivative of it with respect to the Kalman gain and set it to zero.
\begin{align*}
	\frac{\partial \tr\left[ P\kpkp\right] }{\partial K\kp } =& 2K\kp (C P\kpk C\t + R) - 2P\t\kpk C\t\\
	=& 0\\
	K\kp =& P\kpk C\t (C P\kpk C\t + R)^{-1}\\
	=& P\kpk C\t S^{-1}\kpk
\end{align*}
We substitute the gain into the update equations
\begin{align*}
	\hat x\kpkp =& \hat x\kpk + P\kpk C\t S^{-1}\kpk\kp (y\kp - \hat y\kpk)\\
	P\kpkp =& P\kpk - P\kpk C\t (CP\kpk C\t + R)^{-1} C P\kpk\\
	=& (I - K\kp C)P\kpk
\end{align*}
\begin{tcolorbox} [colback=blue!5!white,colframe=blue!75!black,title=\textbf{Remark}:,subtitle style={boxrule=0.4pt,
		colback=yellow!50!red!25!white}]
	Both the filtered state and the filtered error covariance estimates depend on the predicted estimates on the previous time step.
\end{tcolorbox}
\newpage
\subsection*{Properties of the Kalman Filter}
\begin{enumerate}
	\item The Kalman filter is a recursive algorithm, which is an implication of the model obeying the Markov property. The recursion visits the prediction and measurement update stages one after another till the end.
\begin{tcolorbox} [colback=red!5!white,colframe=red!75!black,title=\textbf{Kalman Filter Recursion}:,subtitle style={boxrule=0.4pt,
		colback=yellow!50!red!25!white}]
Given the initial distribution $p(x_0) = \N(x_0;\hat x_{0\mid0},P_{0\mid0})$\\

Repeat for each k:
	\begin{itemize}
		\item Prediction Update
		\begin{align}
			\hat{x}\kpk =& G \hat x\kk + H u\k \\
			P\kpk =& G P \kk G\t + Q
		\end{align}
		\item Measurement Update
		\begin{align}
			\hat x\kpkp =& \hat x\kpk + K\kp (y\kp - \hat y\kpk)\label{eq:stateMeasurementUpdate}\\
			P\kpkp =& P\kpk - K\kp S\kpk K\t\kp
		\end{align}
	where
	\begin{align}
		\hat y\kpk =& C x\kpk \\
		S\kpk =& CP\kpk C\t + R\\
		K\kp =& P\kpk C\t S^{-1}\kpk
	\end{align}
	\end{itemize}

\end{tcolorbox}

	\item The Kalman gain and the covariance update equations do not depend on the measurements, hence they can be calculated offline once the model is given.
	\item The sufficient statistics to describe a Gaussian distribution are the mean and the covariance. Using Kalman filter, we compute this statistics, thus we make not only point estimations but also produce probability desity function estimates.
	\item The Kalman filter is an optimal observer, you can see that \eqref{eq:stateMeasurementUpdate} has the same form of a Luenberger's observer.
	\item When we believe that we know the system dynamics quite surely, we keep the process noise covariance matrix, $Q$, small. Then, the resulting Kalman gain becomes also small that the effect of measurements on the estimates are kept small. Similarly, we can infer that when $Q$ is high, the measurements are incorporated into the estimates to a bigger extent, as we have more trust on the measurements than the system model.
	\item When we think that our sensor is highly noisy, we set the measurement noise covariance, $R$, to a high value, as a result the Kalman gain gets smaller. Therefore, estimates mostly depend on the predictions. In the case of a smaller $R$, the measurements affect the estimates more due to having a larger Kalman gain.
\end{enumerate}

\textbf{Example:} 2-D random walk with scalar measurement.
Consider the following system
\begin{align*}
	x\kp =& \left[ \begin{array}{cc}
		1 & 0 \\ 0 & 1
	\end{array}\right] x\k + w\k\\
	y\k =& \left[\begin{array}{cc}
		1 & 1
	\end{array} \right]  + v\k
\end{align*}
where $w\k \sim \N(w\k;0,Q) $, $v\k \sim \N(v\k;0,0.4) $, and $x_{0\mid 0}\sim\N(x_0;0,\mathrm{I}_{2\times2 })$ with $Q = 0.1\cdot \mathrm{I}_{2\times2}$. Given the measurements $y_1 = 1$  and $y_2=-1.5$, compute the filtered mean and covariance estimates for the first two time instants.

\textbf{Solution:}
\begin{itemize}
	\item When $k = 1$:

	Prediction Update:
	\begin{align*}
		x_{1\mid 0} =& G x_{0\mid 0}\\
		=& \left[ \begin{array}{cc}
			1 & 0 \\ 0 & 1
		\end{array}\right] \left[ \begin{array}{c}
		0 \\ 0
	\end{array}\right] \\
		=&\left[ \begin{array}{c}
			0 \\ 0
		\end{array}\right] \\
		P_{1\mid 0} =& G P_{0\mid 0} G\t + Q\\
		=& \left[ \begin{array}{cc}
			1 & 0 \\ 0 & 1
		\end{array}\right]\left[ \begin{array}{cc}
		1 & 0 \\ 0 & 1
	\end{array}\right]\left[ \begin{array}{cc}
	1 & 0 \\ 0 & 1
	\end{array}\right]+\left[ \begin{array}{cc}
	0.1 & 0 \\ 0 & 0.1
	\end{array}\right]\\
	=& \left[ \begin{array}{cc}
		1.1 & 0 \\ 0 & 1.1
	\end{array}\right]
	\end{align*}

	Measurement Update:
	\begin{align*}
		S_{1\mid 0} =& C P_{1\mid0}C\t+R\\
		=& \left[\begin{array}{cc}
			1 & 1
		\end{array} \right] \left[ \begin{array}{cc}
		1.1 & 0 \\ 0 & 1.1
	\end{array}\right]\left[ \begin{array}{c}
	1 \\ 1
	\end{array}\right] + 0.4\\
	 =& 2.5\\
	 K_1 =& P_{1\mid0}C\t S^{-1}_{1\mid0}\\
	 =&\frac{1}{2.5}\left[ \begin{array}{cc}
	 	1.1 & 0 \\ 0 & 1.1
	 \end{array}\right]\left[ \begin{array}{c}
	 1 \\ 1
	 \end{array}\right]\\
	=& \left[ \begin{array}{c}
		0.44 \\ 0.44
	\end{array}\right]\\
	\hat y_1 =& C x_{1\mid0}\\
	=& \left[\begin{array}{cc}
		1 & 1
	\end{array} \right] \left[ \begin{array}{c}
	0 \\ 0
	\end{array}\right] \\
	=& 0
\end{align*}
\begin{align*}
	x_{1\mid1} =& x_{1\mid0}+K_1(y_1-\hat y_1)\\
	=& \left[ \begin{array}{c}
		0 \\ 0
	\end{array}\right] + \left[ \begin{array}{c}
	0.44 \\ 0.44
	\end{array}\right] (1-0)\\
	 =&\left[ \begin{array}{c}
		0.44 \\ 0.44
	\end{array}\right]\\
	P_{1\mid 1} =& P_{1\mid 0} - K_1 S_{1\mid 0}K_1\t\\
	 =& \left[ \begin{array}{cc}
	 	1.1 & 0 \\ 0 & 1.1
	 \end{array}\right]-2.5\left[ \begin{array}{c}
	 0.44 \\ 0.44
	 \end{array}\right]\left[ \begin{array}{cc}
	 0.44 & 0.44
	\end{array}\right]\\
	=&	\left[ \begin{array}{cc}
		0.62 & -0.48 \\ -0.48 & 0.62
	\end{array}\right]
	\end{align*}
\item When $k = 2$:

Prediction Update:
\begin{align*}
	x_{2\mid 1} =& G x_{1\mid 1}\\
	=& \left[ \begin{array}{cc}
		1 & 0 \\ 0 & 1
	\end{array}\right] \left[ \begin{array}{c}
		0.44 \\ 0.44
	\end{array}\right] \\
	=&\left[ \begin{array}{c}
		0.44 \\ 0.44
	\end{array}\right] \\
	P_{2\mid 1} =& G P_{1\mid 1} G\t + Q\\
	=& \left[ \begin{array}{cc}
		1 & 0 \\ 0 & 1
	\end{array}\right]\left[ \begin{array}{cc}
		0.62 & -0.48 \\ -0.48 & 0.62
	\end{array}\right]\left[ \begin{array}{cc}
		1 & 0 \\ 0 & 1
	\end{array}\right]+\left[ \begin{array}{cc}
		0.1 & 0 \\ 0 & 0.1
	\end{array}\right]\\
	=& \left[ \begin{array}{cc}
		0.72 & -0.48 \\ -0.48 & 0.72
	\end{array}\right]
\end{align*}

	Measurement Update:
\begin{align*}
	S_{2\mid 1} =& C P_{2\mid1}C\t+R\\
	=& \left[\begin{array}{cc}
		1 & 1
	\end{array} \right] \left[ \begin{array}{cc}
		0.72 & -0.48 \\ -0.48 & 0.72
	\end{array}\right]\left[ \begin{array}{c}
		1 \\ 1
	\end{array}\right] + 0.4\\
	=& 0.88\\
	K_2 =& P_{2\mid1}C\t S^{-1}_{2\mid1}\\
	=&\frac{1}{0.88}\left[ \begin{array}{cc}
		0.72 & -0.48 \\ -0.48 & 0.72
	\end{array}\right]\left[ \begin{array}{c}
		1 \\ 1
	\end{array}\right]\\
	=& \left[ \begin{array}{c}
		0.27 \\ 0.27
	\end{array}\right]\\
	\hat y_2 =& C x_{2\mid1}\\
	=& \left[\begin{array}{cc}
		1 & 1
	\end{array} \right] \left[ \begin{array}{c}
		0.44 \\ 0.44
	\end{array}\right] \\
	=& 0.88
\end{align*}
\begin{align*}
	x_{2\mid2} =& x_{2\mid1}+K_2(y_2-\hat y_2)\\
	=& \left[ \begin{array}{c}
		0.44 \\ 0.44
	\end{array}\right] + \left[ \begin{array}{c}
		0.27 \\ 0.27
	\end{array}\right] (-1.5-0.88)\\
	=&\left[ \begin{array}{c}
		0.2 \\ 0.2
	\end{array}\right]\\
	P_{2\mid 2} =& P_{2\mid 1} - K_2 S_{2\mid 1}K_2\t\\
	=& \left[ \begin{array}{cc}
		0.72 & -0.48 \\ -0.48 & 0.72
	\end{array}\right]-0.88\left[ \begin{array}{c}
		0.27 \\ 0.27
	\end{array}\right]\left[ \begin{array}{cc}
		0.27 & 0.27
	\end{array}\right]\\
	=&	\left[ \begin{array}{cc}
		0.66 & -0.54 \\ -0.54 & 0.66
	\end{array}\right]
\end{align*}
\end{itemize}

% **** This ENDS THE EXAMPLES. DON'T DELETE THE FOLLOWING LINE:
\end{document}