\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}

\usepackage{tikz}
\usepgfmodule{matrix}
\usetikzlibrary{chains}
\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}
\usepackage{multicol}

\usetikzlibrary{positioning,shapes,chains}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usepackage{dot2texi}
\input{../include/grammar.tex}
\title{Fundamentals of Computing: Lecture 5}
\date{August 5, 2009}
\begin{document}
\begin{frame}
\maketitle
\end{frame}

\begin{frame}
  \frametitle{Summary of previous lecture}
  \begin{block}{Expressions}
    \begin{itemize}
    \item Integer expressions.
    \item Floating point expressions \text{\tt float} and \text{\tt double}.
    \item Boolean expressions (or rather the lack of boolean expressions)
    \end{itemize}
  \end{block}
  \begin{block}{Assignment}
    \begin{itemize}
    \item Assignment is also an expression
    \item Special assignements liket \text{\tt i\char43{}\char43{}} and \text{\tt m~\char42{}\char61{}~4}
    \item Problem with \text{\tt \char61{}\char61{}} and \text{\tt \char61{}}. 
    \end{itemize}
  \end{block}
\end{frame}

\begin{frame}
  \frametitle{Statement}
  \begin{block}{Simple statement}
    A simple statement is just an expression followed by \text{\tt \char59{}} (semicolon).
    eg

    \text{\tt x~\char61{}~100\char59{}}
    \pause
    \text{\tt f\char40{}x\char41{}\char59{}}
  \end{block}
  \pause
  \begin{block}{Compound statement}
    A sequence of statements enclosed in a pair of braces
    eg
    \begin{tabbing}\tt
~~~~~~~\char123{}\\
\tt ~~~~~~~~~foo~\char61{}~x\char59{}\\
\tt ~~~~~~~~~bar~\char61{}~y\char59{}\\
\tt ~~~~~~~\char125{}
\end{tabbing}
  \end{block}
\end{frame}
\begin{frame}
  \frametitle{Control flow statements}
  Normally statements are executed in the sequence in which 
  they are given.

  What if we want to change ?

  \begin{itemize}
  \item Conditional statements
  \item Loop statements.
  \end{itemize}

\end{frame}
\begin{frame}
  \frametitle{Conditional statement}
  \begin{tabbing}\tt
~~~~~if~\char40{}~expression~\char41{}~statement\\
\tt ~~~~~if~\char40{}~expression~\char41{}~statement1~else~statement2
\end{tabbing}
  \pause
  eg
  \begin{tabbing}\tt
~~~~~if~\char40{}~x~\char62{}~42~\char41{}~\char123{}\\
\tt ~~~~~~~printf\char40{}\char34{}Value~too~large\char34{}\char41{}\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~else~\char123{}\\
\tt ~~~~~~~printf\char40{}\char34{}just~right\char34{}\char41{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
  \frametitle{While loop}
  \begin{tabbing}\tt
~~~~~while\char40{}~expression~\char41{}~statement
\end{tabbing}
  \pause
  Example summing numbers till \text{\tt n}
  \begin{tabbing}\tt
~~~~~sum~\char61{}~0\char59{}\\
\tt ~~~~~i~\char61{}~0\char59{}\\
\tt ~~~~~while\char40{}~i~\char60{}~n~\char41{}\\
\tt ~~~~~\char123{}~\\
\tt ~~~~~~~sum~\char43{}\char61{}~i\char59{}\\
\tt ~~~~~~~i\char43{}\char43{}\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~printf\char40{}\char34{}sum~till~\char37{}d~is~\char37{}d\char34{}\char44{}n\char44{}sum\char41{}\char59{}
\end{tabbing}
\end{frame}
\begin{frame}
  \frametitle{For loop}
  \begin{tabbing}\tt
~~~~~~~for\char40{}~initialisation~\char59{}~condition\char59{}~update\char41{}~statement
\end{tabbing}
  eg
  \begin{tabbing}\tt
~~~~~for\char40{}int~i~\char61{}~0\char59{}~i~\char60{}\char61{}~n\char59{}~i\char43{}\char43{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~sum~\char43{}\char61{}i\char59{}\\
\tt ~~~~~\char125{}\\
\tt ~~~~~printf\char40{}\char34{}Sum~till~\char37{}d~is~\char37{}d\char34{}\char44{}n\char44{}sum\char41{}\char59{}
\end{tabbing}
  \pause
  \newpage
  \begin{tabbing}\tt
~~~~~for\char40{}int~i~\char61{}~0\char59{}~i~\char60{}\char61{}~n\char59{}~i\char43{}\char43{}\char41{}~sum~\char43{}\char61{}i\char59{}\\
\tt ~~~~~printf\char40{}\char34{}Sum~till~\char37{}d~is~\char37{}d\char34{}\char44{}n\char44{}sum\char41{}\char59{}
\end{tabbing}
\end{frame}
\end{document}

%[start chain, every node/.style={on chain, join},every join/.style={->}]
      % %   \&              \& |[symbol]|{expr} \& |[symbol]{binary op} \& \node[symbol]{expr}\\
      % %   \&              \& \node[symbol]{unary op} \& \node[symbol]{expr} \\



\begin{frame}
  \frametitle{Grammar of expressions}
  \begin{tikzpicture}[tip/.style={->},join/.style={rounded corners},
      thick,draw=black!50,
      
      hv path/.style={to path={-\text{\tt \char40{}\char92{}tikztotarget\char41{}\char125{}\char125{}\char44{}~~~~~~~vh~path\char47{}\char46{}style\char61{}\char123{}to~path\char61{}\char123{}}-(\tikztotarget)}}]
    \matrix[ampersand replacement =\&, row sep=0.25cm, column sep=0.5cm]
    {
      %%% Constant line
       \& \& \Point{c1}\& \Token{constant} \& \Point{c2}\\
       \& \& \Point{v1}\& \Symbol{variable} \& \Point{v2}\\
       \Point{p0}\& \Point{p1} \& \& \& \&
       \Point{p2} \& \Point{p3} \\
       \& \& \Symbol[1]{expr} \& \Symbol{bin-op} \& \Symbol[2]{expr}\\
       \& \& \Symbol{un-op} \& \Symbol[3]{expr}\\
    };
    \begin{scope}
      [start chain];
      \chainin(p0);
      \chainin(p1);
      \begin{scope}[start branch=const]
        \chainin(c1)[join =by {vh path,tip}];
        \chainin(constant);
        \chainin(c2);
        \chainin(p2)[join =by {hv path, tip}];
      \end{scope};
    \end{scope}
  \end{tikzpicture}
\end{frame}
