1. Search Given File
2. Loop looking for /* to */
2.1 check if /* is inside of "" or part of code if it is leave it
2.3 if not delete it
2.4 look for next /* */ combination
3. Print file with changes made.
You are thinking about this "from the wrong side". I suggest you read again what i said about parsers reading one character at a time and applying rules to it. I'll take our phantasy-shell example and show you a solution to this, you will still have to come up with a solution for your original problem:
At first, we need some "memory" to remember what we have read so far. Memory comes in the form of status flags (TRUE/FALSE) , which we maintain while reading the code. We start with one for remembering the escape-sequence. We need to do this first, because escaping the strongest "coupling" construct of a language. It is like operator precedence: this is the "operator" with the highest precedence so we have to take care of this first *):
You see, the loop does nothing more than to read one character at a time and maintain a status flag for escaped characters. This relates to the "rule 2" of my last posting.
I suggest you take a piece of paper and try to go through a real script with this pseudo-program "by hand" to see how the status flags are maintained and the logic works (the same for the other pieces of code to follow - this is a invaluable exercise in grasping programming constructs, believe me! *) ). You see that what the escaped-status-flag does is to exclude the next character from being interpreted by the logic (the "further work") we are going to apply now. Let's start with quoting (i ignore single quotes here to keep the example short - this doesn't mean we could forget about them in the real world):
The added logic just flips the inside-quote-status flag when we encounter double quotes (actually: double quotes which aren't escaped). Now, that we have covered what was "rule 1" in my last posting we are ready to tackle the comments themselves:
Notice, that applying a rule to the character read is indeed changing the way we are applying the following rules: if rule 1 says "don't look at the character any further" this means that rule 3 won't be applied to it, because there is "nothing any more that it could be applied to", so to say.
I have added some logic at the end to let you see if the program correctly catches all instances of comments. Again, i suggest you "execute" it by hand with a piece of paper to see its operation.
Some notes reagrding your own program: you will notice that (unlike in my example) there is not a "comment-start character" but a "comment-start SEQUENCE". You will have to implement additional status flags to catch these sequences.
Put your efforts here, in pseudo-code like i did, and we will discuss your solution and finally implement the program itself.
I hope this helps.
bakunin
__________
*) This is easily shown by looking at this: "\"" . If the quoting would be primary and the escaping secondary the escaping would not take place because of being inside a quote. And the output would be 2 characters (backslash-quote sign). There would be no way to have the double quote character inside a quoted string. As it is escaping takes place before quoting and therefore the string means "an escaped double-quote sign inside a quoted string" and this accomplishes the "double-quote inside a quoted string" just fine.
*) Alan Turing did exactly this with his first try at a chess program.
Last edited by bakunin; 05-23-2011 at 02:11 PM..
Reason: changed a logical mistake
Hello Unix Gurus,
We are doing large system upgrade. We expect upgrade to last 180-200 hours. The servers are located remotely.
I am looking for solution which allows me to reconnect to the same session active on unix server where I launched the process. This would protect from local client... (0 Replies)
I have a file with a single line in it as below.
field1 field2 field3
Different fields separated by spaces. I need the output as below.
field1
field2
field3
Any sed/awk solution you can suggest? (6 Replies)
$db2 connection ...........Q
a=`$db2 -x "select A from tablename where z in (select z from tablename Q where condition fetch first 1 rows only ) with ur"`
b=`$db2 -x "select B from tablename where z in (select z from tablename Q where condition fetch first 1 rows only) with ur"`
$db2... (2 Replies)
hi
i have one file where i want to substitute only first instance of
swap
with swap1
i want to replcae only first instance of swap in my script
i know we can do this with awk. but i need to do this with sed only
i tried follwoing code
sed 's/swap/swap1' filename
but here all... (15 Replies)
Hi,
can anyone help me with my scrip please. I wanted do following tasks:
1. List all the directory
2. A STDIN to ask user to enter a directory name from listed directories
3. command to check if the directory exists( or a command to validate if the user entered a valid directory name)
... (2 Replies)
Hi All,
I am using LATEX and need to delete all the lines in a file matching:
\begin{work}
I know there are several ways to do this, but I am trying to do it with the substitute command in VI. The problem is I can't get substitute to recognize the character '\'!
How do I do it?
... (7 Replies)
have this code but when i run it i get this error
./pulse: line 2: and here is the code
#!/bin/bash
if ;
then
pulseaudio -k;
fi
what am i doing wrong
thanks
Adam (5 Replies)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
Try running 'phone4 xyz' and see what happens.
Modify your program so that if no matching name is found, an... (1 Reply)
Hello people,
I am having problem to sort, sed and zero padding of column in csv file.
7th column only.
Input of csv file:
1,2,3,4,5,6,4/1/2010 12:00 AM,8
1,2,3,4,5,6,3/11/2010 9:39 AM,8
1,2,3,4,5,6,5/12/2011 3:43 PM,8
1,2,3,4,5,6,12/20/2009 7:23 PM,8
Output:... (5 Replies)
I'm trying to remove '--X' from the whole file and using variables replace $oldvar with $newvar.
I have tried with double quotes but it doesn't seem to work. $newvar is set to /usr/bin/bash. Would appreciate some guidance.
newvar=$(which bash)
oldvar=/bin/bash
sed... (1 Reply)