How to convert this grammar to LL(1)? - parsing

S → Y | 1X
X → 1X | 0
Y → Y 0 | 1X1 | 2X2
I do not understand how to do factoring or substitution when there are more than one of the same symbol. Thanking you.

X → 1 X | 0
refers to a 0-or-more 1 followed by 0, so it's equivalent to
X → 1* 0
The same approach can be used to remove your left-recursion. You can rewrite
S → Y | 1 X
X → 1 X | 0
Y → Y 0 | 1 X 1 | 2 X 2
as
S → Y | 1 X
X → 1* 0
Y → ( 1 X 1 | 2 X 2 )* 0
In EBNF:
S → Y | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
In BNF:
S → Y | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
1* → 1 1* | Ɛ
A* → A A* | Ɛ
If all you wanted to do was to eliminated left-recursion, you're done.
If you want to eliminate common prefixes too, you're not done because both sub-rules of S can start with 1 X. To fix this, inline and distribute to obtain the following:
S → 0
| 1 X 1 Y
| 2 X 2 Y
| 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
Now, we're finally in a position to factor out the common 1 X.
S → 0
| 1 X ( 1 Y )?
| 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
In EBNF:
S → 0 | 1 X B? | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
B → 1 Y
In BNF:
S → 0 | 1 X B? | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
B → 1 Y
B? → B | Ɛ
1* → 1 1* | Ɛ
A* → A A* | Ɛ

Related

Is the image of a `hcomp` the `hcomp` of images?

I am trying to fill the remaining one hole in the following program:
{-# OPTIONS --cubical #-}
module _ where
open import Cubical.Core.Everything
open import Cubical.Foundations.Everything
data S1 : Type where
base : S1
loop : base ≡ base
data NS : Type where
N : NS
S : NS
W : S ≡ N
E : N ≡ S
module _ where
open Iso
NS-Iso : Iso NS S1
NS-Iso .fun N = base
NS-Iso .fun S = base
NS-Iso .fun (W i) = base
NS-Iso .fun (E i) = loop i
NS-Iso .inv base = N
NS-Iso .inv (loop i) = (E ∙ W) i
NS-Iso .leftInv N = refl
NS-Iso .leftInv S = sym W
NS-Iso .leftInv (W i) = λ j → W (i ∨ ~ j)
NS-Iso .leftInv (E i) = λ j → compPath-filler E W (~ j) i
NS-Iso .rightInv base = refl
NS-Iso .rightInv (loop i) = ?
The type of the hole is:
fun NS-Iso (inv NS-Iso (loop i)) ≡ loop i
inv NS-Iso (loop i) of course definitionally equal to (E ∙ W) i, but what is then fun NS-Iso ((E ∙ W) i)? Is there some kind of homomorphism / continuity / similar property with which I can use the definitions of fun NS-Iso (E i) and fun NS-Iso (W i) to figure out what it is?
Since fun NS-Iso (E i) = loop i and fun NS-Iso (W i) = base, I thought this might be a valid filling (pun intended) of the hole:
NS-Iso .rightInv (loop i) = λ j → compPath-filler loop (refl {x = base}) (~ j) i
But that gives a type error:
hcomp (doubleComp-faces (λ _ → base) (λ _ → base) i) (loop i)
!=
fun NS-Iso (hcomp (doubleComp-faces (λ _ → N) W i) (E i))
I have found that the answer is, basically, yes!
Let's add a local binding of our goal in NS-Iso .rightInv (loop i) just to keep an eye on the type:
NS-Iso .rightInv (loop i) = goal
where
goal : (fun NS-Iso ∘ inv NS-Iso) (loop i) ≡ loop i
goal = ?
Since we have
NS-Iso .inv (loop i) = (E ∙ W) i
the type of goal reduces to:
step1 : fun NS-Iso ((E ∙ W) i) ≡ loop i
And now comes the crucial step, the actual answer to my question: can we push in fun NS-Iso into E ∙ W?
Let's draw wavy lines between the points and paths where fun NS-Iso is directly defined, or where we know its value by cong _ refl = refl:
base base
^ ~ ~ ^
| ~ ~ |
| ~ ~ |
| N N |
| ^ ^ |
refl | ~~~ refl | | W ~~~~~ | refl
| | | |
| N -------------> S |
| ~ E ~ |
| ~ ~ ~ |
| ~ ~ ~ |
base --------------------------------> base
loop
E ∙ W is the lid of the inner box, and it turns out yes, its image by fun NS-Iso is indeed the lid of the outer box:
step2 : (cong (fun NS-Iso) E ∙ (cong (fun NS-Iso) W)) i ≡ loop i
In graphical form:
?
base - - - - - - - - - - - - - - - - - > base
^ ~ ~ ^
| ~ ~ |
| ~ E ∙ W ~ |
| N - - - - - - -> N |
| ^ ^ |
refl | ~~~ refl | | W ~~~~~ | refl
| | | |
| N -------------> S |
| ~ E ~ |
| ~ ~ ~ |
| ~ ~ ~ |
base -------------------------------> base
loop
So we can reduce the fun NS-Iso applications now:
step3 : (loop ∙ (λ _ → base)) i ≡ loop i
which we can finally solve with a library function doubleCompPath-filler.
The complete code:
NS-Iso .rightInv (loop i) = goal
where
step3 : (loop ∙ (λ _ → base)) i ≡ loop i
step3 = cong (λ p → p i) (symP (ompPath-filler loop (λ _ → base)))
step2 : (cong (fun NS-Iso) E ∙ (cong (fun NS-Iso) W)) i ≡ loop i
step2 = step3
step1 : fun NS-Iso ((E ∙ W) i) ≡ loop i
step1 j = step2 j
goal : (fun NS-Iso ∘ inv NS-Iso) (loop i) ≡ loop i
goal j = step1 j
I don't know why I needed to eta-expand goal and step1, but otherwise Agda doesn't recognize that they meet the boundary conditions.

Making Agda reduce an expression in a goal type

Using ℕ and _≟_ from the standard library, I have
open import Data.Nat
open import Relation.Binary.PropositionalEquality
open import Relation.Nullary
foo : ℕ -> ℕ -> ℕ
foo x y with x ≟ y
foo x .x | yes refl = x
foo x y | no contra = y
data Bar : ℕ -> Set where
bar : (x : ℕ) -> Bar (foo x x)
I want to implement
mkBar : (x : ℕ) -> Bar x
mkBar x = bar x
Agda complains,
Type mismatch:
expected: x
actual: foo x x | x ≟ x
when checking that the expression bar x
has type Bar x
This makes sense to me: Agda doesn't know a priori that x ≟ x always evaluates to yes refl, so it's not going to evaluate foo x x until it knows a bit more about x.
So I tried rewriting the goal to force x ≟ x to resolve to yes refl,
eq-refl : forall x -> (x ≟ x) ≡ yes refl
eq-refl x with x ≟ x
eq-refl x | yes refl = refl
eq-refl x | no contra = ⊥-elim (contra refl)
mkBar : (x : ℕ) -> Bar x
mkBar x rewrite eq-refl x = bar x
but to no avail. Same error message. I also tried rewriting by foo x x ≡ x:
foo-eq : forall x -> foo x x ≡ x
foo-eq x rewrite eq-refl x = refl
mkBar : (x : ℕ) -> Bar x
mkBar x rewrite foo-eq x = bar x
This answer suggests pattern matching on x ≟ x on the left hand side of mkBar, but it also seems to have no effect:
mkBar : (x : ℕ) -> Bar x
mkBar x with x ≟ x
mkBar x | yes refl = bar x
mkBar x | no contra = ⊥-elim (contra refl)
I must be missing a trick here. How do I get rid of the | in the goal type and make foo x x reduce to x? (I'd prefer not to examine x directly in the LHS of mkBar.)
You were almost there: the important thing to notice is that rewrite takes an x ≡ y and replaces x by y in the goal. foo-eq x has type foo x x ≡ x but there is no foo x x to replace in the goal!
What you need to do is rewrite by sym (foo-eq x) like so:
mkBar : (x : ℕ) → Bar x
mkBar x rewrite sym (foo-eq x) = bar x
Bar x then becomes Bar (foo x x) meaning you can apply your constructor.

Do I have to use an explicit match statement to identify its wildcard value?

Do I have to use an explicit match statement to identify its wildcard value?
For example, take the following function:
let (|Positive|Neutral|Negative|) = function
| x when x > 0 -> Positive
| x when x = 0 -> Neutral
| x when x < 0 -> Negative
| _ -> failwith (sprintf "unknown: %d" _)
Error:
Unexpected symbol '_' in expression
I learned that I can do this without any errors:
let (|Positive|Neutral|Negative|) v =
match v with
| x when x > 0 -> Positive
| x when x = 0 -> Neutral
| x when x < 0 -> Negative
| _ -> failwith (sprintf "unknown: %d" v)
UPDATE
Here's the result from a posted answer:
let (|Positive|Neutral|Negative|) = function
| x when x > 0 -> Positive
| x when x = 0 -> Neutral
| x when x < 0 -> Negative
| x -> failwith (sprintf "unknown: %d" x)
You can change it to this and it will work:
let (|Positive|Neutral|Negative|) = function
| x when x > 0 -> Positive
| x when x = 0 -> Neutral
| x when x < 0 -> Negative
| f -> failwith (sprintf "unknown: %d" f)

Need documentation for GPUImageColorMatrixFilter. Where can I find documentation for what the matrix means?

I would like to implement a bandpass filter using GPUImageColorMatrixFilter. Basically, blue would equal floor(blue - (k*red)) and both red and green would just end up being zero. Where can I find documentation indicating what the columns and rows of the matrix mean?
My intuition would suggest that the 4x4 matrix is following the standard RGBA order and judging by the examples (see for instance GPUImageSepiaFilter) it looks like I'm right.
For instance, this is the identity GPUMatrix4x4
R G B A
| 1 0 0 0 | red
| 0 1 0 0 | green
| 0 0 1 0 | blue
| 0 0 0 1 | alpha
Let's name each coefficient
R G B A
| a b c d | red
| e f g h | green
| i j k l | blue
| m n o p | alpha
Applying the matrix to a RGBA color will result in the following R'G'B'A' color where the components are computed as
R' = a*R + b*G + c*B + d*A
G' = e*R + f*G + g*B + h*A
B' = i*R + j*G + k*B + l*A
A' = m*R + n*G + o*B + p*A
which is nothing but the following matrix multiplication
| a b c d | |R| |R'|
| e f g h | x |G| = |G'|
| i j k l | |B| |B'|
| m n o p | |A| |A'|

Eliminate this indirect left recursion

The following grammar has left recursion.
X is the start simbol.
X -> Xa | Zb
Z -> Zc | d | Xa
How to remove it? Please explain it step by step.
-- remove Z left recursion--
X -> Xa | Zb
Z -> dZ' | XaZ'
Z' -> cZ' | empty
--next remove Z --
X -> Xa | dZ'b | XaZ'b
Z' -> cZ' | empty
--next factorize X--
X -> XaX' | dZ'b
X'-> Z'b | empty
Z'-> cZ' | empty
--next remove X recursion--
X -> dZ'bX''
X'' -> a X'X'' | empty
X' -> Z'b | empty
Z' -> cZ' | empty

Resources