Tuesday, March 29, 2011

Study Habits - Avoiding the Last Minutes Blues

Virtually every topic is interesting to someone, somewhere. I'm not particularly interested in the sex life of the South American tree fog. However, a biologist might be fascinated. (Another tree frog might be, too.) If you wait for teachers to "make" their courses interesting, you are missing the point. Interest is a matter of your attitude. It's mistake to blame poor performance on events "beyond your control". Students who believe that success is based on effort tend to do better in the long run (Noel et al, 1987).

Cool, Dennis. Essential of Psychology Exploration and Application - 8th Edition. Wadsworth, Thomson Learning. 2000. Print.


Saturday, March 19, 2011

Quotes


Three extension are common to most EBNFs

The first extension denotes an optional part of a RHS, which is delimited by brackets. For example:

<if_stmt> -> if ( <expr> ) stmt [ else <stmt> ]

The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether. For example:



<ident_list> -> ident {, <ident> } 


The third extension deals with multiple choice options. For example:



<term> -> <term> ( *|/|% ) <factor>


Three extension are common to most EBNFs

  • The first extension denotes an optional part of a RHS, which is delimited by brackets. For example:   -> if ( ) statement [ else ]
  • The second extension is the use of braces in an RHS to indicate that the enclosed part can be repeated indefinitely or left out altogether. For example:  -> identifier {, }
  • The third extension deals with multiple choice options. For example:  -> term ( *| / | % )

Distinguish between static and dynamic semantics

  • Static semantic of a language is only indirectly related to the meaning of program during execution; rather it has to do with the legal forms of programs (syntax rather than semantics). Many static semantic rules of a language state its type constraints. Static semantics is so named because the analysis is required to check these specification can be done at compile time.
  • Dynamic semantics is the meaning of the expression, statements, and program units of a programming language.
Generally speaking, dynamic semantics are defined during execution time and can be change while static semantics are known at compile time and do not change.

What are the advantages and disadvantages of dynamic scoping?

Disadvantages

  • Inability to statically check for references to nonlocal variables.
  • Dynamics scoping also makes program difficult to read because the calling sequence of subprograms must be known to determine the meaning of references to nonlocal variables. 
  • There's no way to protect local variables from being accessed to by subprograms because local variables of subprogram are all visible to all other executing subprograms, regardless of textual proximity.
  • Accessing to nonlocal variables in dynamic scoping takes far longer than accesses to nonlocal variables when static scoping is used.
Advantages
  • On the other hand, the only advantage of dynamic scoping is writability. It is more convenient and it provides more flexibility than static scoping. For example, in some cases, some parameters passed from one subprogram to another are variables that are defined in the caller. None of these need to be passed in a dynamic scoped language, because they are implicitly visible in the called subprogram.

Converse Fermat's Little Theorem Proof

Prove that if $n > 1$ is a prime if for some a,

$$a^{n - 1} \equiv 1 \pmod{n}$$
while $a^q \not\equiv 1 \pmod{n}$ for each divisor of $n - 1$.
Proof
Before attempt the proof we will prove these two claims.
Claim 1
If $a \equiv b \pmod{n}, then (a, n) = (b, n)$ 
Proof
Since $a \equiv b \pmod{n}$, we have $a - b = n.k \Leftrightarrow b = a - n.q = a + n.k$ for some integers q, k. 
So, proving $(a, n) = (b, n) \Leftrightarrow (a, n) = (a + n.k, n)$
Let $e = (a, n)$, then $e|a, e|n \implies e|(a + n.k) \implies e|(a + k.n, n) *$
Next, let $ f = (a + n.k, n)$, similarly we have $f|a, f|(a + n.k) \implies f|(a + n.k - n.k ) = a \implies f|(a, n) **$
From * and **,
$\therefore e = f \Leftrightarrow (a, n) = (b, n)$, as was to be shown.
Claim 2
If $a^{n - 1} \equiv 1 \pmod{n}$, then $(a, n) = 1$
Proof
Since $a|a^{n - 1} \implies (a, n)|(a^{n - 1}, n)$
where $(a^{n - 1}, n) = (n, 1) = 1$ by Claim 1.
Hence, $(a, n)|1 \implies (a, n) = 1$, as was to be shown.
Now we're ready!
We have $a^{n - 1} \equiv 1 \pmod{n}$, so by Claim 2, $(a, n) = 1$.
Using Euler's theorem, we have $a^{\phi(n)} \equiv 1 \pmod{n}$.
Since $\phi(n) > 0$, by Well-Ordering principle, there exists the least positive
integer $x$ such that $a^{x} \equiv 1 \pmod{n}$.
Next, we will show that if there exists an integer $m, m > x$, and $a^{m} \equiv 1 \pmod{n}$, then $x|m$.
If $x|m$, we're done. Otherwise if $x\not|m$, then
By division algorithm, we have:
$$m = x.q + r \text{ for some integers } q, \text{ where } 0 \leq r < x$$
$\longrightarrow a^{x.q + r} \equiv {a^{x}}^q \cdot a^{r} \equiv a^r \pmod{n}$
Hence, $a^m \equiv a^r \equiv 1 \pmod{n}$
But $x$ is the smallest integer in the set, and $r < x$, therefore $r = 0$.
$\therefore x|m$, as was to be shown.
Now, consider $a^{n - 1} \equiv 1 \pmod{n}$, we see that there exists an $x$ such that $x|(n - 1)$,
And this implies $x$ must be a divisor of $n - 1$, i.e $x = q$, 
Nonetheless, $x^q \not\equiv 1 \pmod{n} \forall$ divisors of $n - 1$.
Hence, $x = n - 1$, and this implies that $n - 1$ is the smallest positive integer such that
$$a^{n - 1} \equiv 1 \pmod{n}$$
By Euler's theorem, we have $a^{\phi(n)} \equiv 1 \pmod{n} \implies (n - 1) \leq \phi(n)$
But from the definition of $\phi(n)$, we know that $\phi(n) \leq (n - 1)$.
So,
$$(n - 1) \leq \phi(n) $$
$$\phi(n) \leq (n - 1) $$

which implies $n - 1 = \phi(n) \implies$ n is prime, as was to be shown.

Three main approaches to build a lexical analyzer

  1. Write a formal description of the token patterns of the language using descriptive languages related to regular expression. These descriptions can be used as inputs to a software tool such as Yac, Lex, that automatically generate a lexical analyzer.
  2. Design a state transition diagram that describes the token patterns of the language and write a program that implements the diagram.
  3. Hand construct a table driven implementation of the state diagram.