Sunday, April 24, 2016

Date Picker Dialog in Android


This dialog "only" gets Date as input. Not time too. Code and screen shots are in the PDF.

Code in PDF

Compiler Lab - Control and Data Flow Analysis Code and Theory PDF


Most of the variable and structure names are sensible ones. Except for a few, which are given below. And for size variable of an array, mostly I have used "size" as a suffix. And "no" means number. Just saying.
Name and meaning :
dno - definition number
bno - block number
defblockmap - mapping of definition number to their corresponding block numbers
definitionmap - mapping of variables to a number of defblockmap
globalno - total number of definitions in the program, also the size of the kill, gen, IN, OUT sets.
The theory for the exercise is also attached. I just understood it as a math problem and coded using the equations and logic. No idea about the exact theory. Alfre Aho book should help you in that.
Some confusions people face are about Kill, Gen, IN, OUT. These are sets. And the maximum number of elements they can contain is the total number of definitions in the program, which is globalno in my code. And since that is the maximum number, these sets are represented using bit vector representation. So, Kill is denoted by n bits, where n is the globalno value and each bit denotes a definition number of the program. And if a definition number is present in that set, then it's corresponding bit is set to 1 or else it is not present, it is set to 0. You should know this to do the Union and Subtract set operations properly on these bit vectors.

Code and Theory 

All the Best ! Quick Tips for Sem Lab - Android and Compiler


All the best for the Semester Lab exams! Quick points to keep in mind while coding in lab :
1. Try to be awake while coding. And don't get fatigue. Try to be calm, and not frustrated ( I m one to say. ) and avoid mind blocks. Drink water in equally spaced intervals to keep the brain cells active and not dehydrated and it helps to keep the body hydrated too. ( Actually, if you feel thirsty, you are already dehydrated. )
2. You Must be awake while debugging the errors in your code. And I will try to put up a tutorial on how to find out the part of the code causing segmentation fault in the C programs using GDB - GNU Debugger ( Google it ).
3. While debugging logical errors, most times, we just skim through the code and follow the flow of control starting from the main in the case of a C program or the start symbol in the case of a yacc specification. I suggest you do some paper work using pencil while debugging. It has helped me out in most cases. Sometimes you will feel there's nothing to write down and find problems. Well, paper work has helped me even in such cases (even recently) ! Write some crap, but write, then see it, then think, and you will find the logical errors. It actually helps you focus better on what is happening though you might not have written every detail in your paper, and it keeps up your concentration. And this way, you won't over look stuff too. Trust me. Paper work, any day. Just be quick though :P because it takes time, but it's worth it, instead of executing the whole code in your mind palace.
4. I usually don't advice about such stuff, but this time, I think I will. Code in versions. Especially all the big codes you do in compiler lab, well, you better code in versions and test each one. Let's say you have to do YACC, first complete LEX and test if it works fine for BOTH good And bad input. Yes. Then move on to code YACC and test if it works together. It takes a bit of time, but not much, and again, it's worth it.


Mail me if you have encountered errors in the past that you were not able to solve and still remain unsolved. Errors like the weird looking "unmatched rule" in LEX specification even though you think your specification looks perfectly fine, and then the YYSTYPE related errors in YACC - LEX specifications.
And you can even ask about Android stuff. In fact I heard crazy errors that were declared as "Emulator problems. It ain't working." . Actually, those errors arose because of wrong code, but the emulators and Android Studio were blamed. I don't know what to say. But yeah, if you don't know what is happening, well, just blame the computers and try to get away with it, but beware, if the teacher says try in another computer and stuff, and if your code still doesn't work, well, you are caught.
Well, that was big, did you skim read ? :P Anyways, for anything else, just mail me.

Saturday, April 23, 2016

Android Basics For Lab, Part 1


About the basics of Android, part 1.

GDB Tutorial to find Code causing Segmentation Fault in C/C++ programs


Basically, you have to compile your program like this :

gcc -g test.c -o test

where in, "test" is the output file name. And -g is used to put debugging information too in the "test" file. Read the above Tutorials Point link to know more about it.
So, you execute the program like this :

./test

Later, if you get segmentation fault, just do this :

gdb test

Then a prompt comes up, type

r

This will run the program, and it will run with debugging mode on I guess. So, when there is a segmentation fault, it will show you the line number and the line of code itself. For example, see the below screen shots :
I am writing a code to basically dereference a null pointer.



When I type "r" command [ Look for "(gdb)" - the prompt, in the screenshot ] , the program runs, and you see the "starting program : ..." message and you see the segmentation fault and where it was received.


And then I type "q" command to quit and say "y" to confirm it. Check the Tutorials Point link to find out more GDB commands. But these two commands should be enough to find out the code causing segmentation fault.



A friend asked if gdb is installed in our lab computers. Well, I think it's a very general package like gcc, and when I tried "sudo apt-get install gdb" and the same for gcc, the prompt said it is installed automatically under some package named "linux-image-extra-xyz.abc.
pqr-generic" something, as if they are all in a single basic general package. So, I think it should be installed. So, just remember these commands and try it out in your programs in case you have segmentation faults in them.

Sunday, April 17, 2016

How to create a "simple" Android Project


Creating a new project is easy. But sometimes you might use unnecessary templates and complicate it. Here's a video to explain it and to tell that only "Blank Activity" must be used to start from scratch and for simple layout and design and not "Material Design".


Friday, April 15, 2016

Buttons and Button Clicks


Running some code on a button click is actually an easy thing to do, and there are many ways to do it. If you are not comfortable with the way you are currently used to, you may try this method.

When you define Buttons in the XML Layout, using the Button tag, just include an extra attribute, android:onClick to it, for example, like this :

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/bStart"
android:onClick="MyClick"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />


If you see, I have written the value for the onClick attribute as "MyClick". This basically means, execute the function named MyClick which is present in the Java Code for the corresponding activity (screen) for which the layout has the button tag.

So, in the Java code for the corresponding activity, put this function :


public void MyClick(View v)
{
               int id = v.getId();
         
               switch(id)
              {

                           case R.id.bStart :         
                                                          tvResult.setText("Start button clicked!");

                                                          break;

               }

}

As you can see, the function takes in an argument, which is a View class object. So, this function can be called by any of the Views in the XML layouts, even by a TextView. But we are calling this only from a button, using the android:onClick attribute. Now, when the function gets called, the value of v is assigned accordingly, based on which view (that had this function name in it's onClick attribute) was clicked. And then, you can get the view's id using the getId() method, which, if you don't remember, when you type "v." in your Android Studio, it will give a lot of functions as suggestions. And to get data, most times, Java methods have the get word at the start, so type "v.get" and you should see a list of get based methods, to get data. Now, press Ctrl + Q to see info about the functions, when they are highlighted in the suggestions. Finally, click on "getId()", and you will have "v.getId()" finally. Actually, you can use this method of determining methods, for all cases, and this was just an easy example, where getId() is an easy method name to remember, but still, I wanted to explain how to do it, when you don't know what methods exist and what is possible and what ain't. And if Android Studio doesn't show any suggestions on it's own, while typing ( probably due to some problem, or due to low RAM, consequently, very slow response of the editor ), the shortcut to get suggestions is Ctrl + Space.

Finally, you can use a switch statement to check the id of the view. So, if you have many buttons, all can have MyClick as the function name in the android:onClick attribute and when the function is called, you can identify which button called the function, using the id of the button. So, use R.id.xyz to refer to the id. And then in the switch case statements, execute what your logic is, for the appropriate button.

Tips : I recommend you to write the function first, then put it's name in the android:onClick attribute value in the XML layout. This way, when you type in the function's name in the value, it will show suggestions, about your MyClick function. This suggestion is show only when MyClick method is public. So, don;t forget, it MUST be a public method, else the App will crash, ie it will crash, if the method is private and if it's being be called from the layout.

Comparing with old code and feeling better ( Please don't look at this section if you get confused with too much code ) :

Finally, the best part is, you don't have to use code like this :

buttonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent i = new Intent(MainActivity.this,SecondActivity.class);
          startActivity(i);
       }
});

where buttonStart is a Button object. And the above code is messy and tough to maintain and debug and change.

And there's another way, which is slightly better, but you have some extra code. Which is where, you implement View.OnClickListener interface and have onClick() method implemented and also make the buttons listen in the onCreate method, using this code (after referencing the button to it's id) :

buttonStart.setOnClickListener(this);

So, if you have many buttons, you have to put the above code many times. Then, finally, you will have the onClick method, in which you will implement stuff that you implemented in MyClick function. Same old thing. Just the "this" thingy code for making the buttons listen, will be extra code over here. And in this case, if you miss the code to make the buttons listen, the onClick method is of no use.


Verdict : So, finally, use the Custom Click function, which can be triggered using android:onClick attribute concept in XML layouts.

Tuesday, April 5, 2016

Compiler Lab stuff. Material for Lex and Yacc

Why does your lexical analyzer recognize "int" as an identifier ? What is wrong with your Lex code or your computer ? How do you pass a file as an input to your lexical analyzer ?

Why do you get "shift/reduce conflicts" warning ? What is yylval ? What is YYSTYPE ? What is the weird error related to YYSTYPE you got in your program ? Have you included the necessary files ? Have you included them just once or twice by mistake ? Is there a problem in your compilation method instead of the main logic code ? What are the different ways to compile ? Some ways ( options in commands ) work only in Ubuntu and other Linux systems and not in Mac ?
Ever had any of these doubts or thought about these ? If yes, mail me! Teach me! If no, it simply means we gotta learn stuff to know more.

Now, are you worried about Lab models and Sem Lab practicals? Let's be frank, if you are worried, you are worried either because you coded only a few programs, or never coded at all. Or have tons of doubts. Which sounds fine.
My smart advice would be, learn stuff at least at an abstract level, and have enough knowledge in theory to code the main logic. For this you need preparation ahead of models exam. Or if you are the super smart person, you could learn all the code the day before models and Sem Lab practicals.
Back to Lex and Yacc stuff. Below are some articles I found useful. I only read a few of them. Yet to read others.

http://www.tldp.org/HOWTO/Lex-YACC-HOWTO.html

Particularly this :
http://www.tldp.org/HOWTO/Lex-YACC-HOWTO-6.html#ss6.3
Then this ( a famous one in Google results) :
epaperpress.com/lexandyacc/download/LexAndYaccTutorial.pdf

http://dinosaur.compilertools.net/
Two good videos that I saw :
https://www.youtube.com/watch?v=54bo1qaHAfk
https://www.youtube.com/watch?v=__-wUHG2rfM
I will update more, in the future, if I find anything.
Share the material with interested people! :)