Flex lexical analyzer: remember starting position - c

I'm working on a flex program and i want to do the following :
-read a line , split it in tokens, remember the tokens(let's say in array1)
if number of tokens equal the number of tokens on the next line(which were remembered in array2) , print array1[i] : array2[i];
First i thought to remember each of the tokens in a matrix , but it's too muahc to do , allocating dynamic memory and so on . I'm sure there is a simple way , i just don't have enough experience with flex.
Thank you.

As far as I know, there is no built-in functionality in Flex to store a sequence of tokens, so that you can print them later. So just do it in normal code. Just use two (possibly malloced) arrays. – Thomas Padron-McCarthy
You could try implementing a simple vector structure yourself and use this. Then let flex return a special value on newline. Just check for this value and you know you're done. – Shadowwolf

Related

What does &array[element] means and why?

I was coding in MPI using C. I don't understand how the MPI_Send() works or if maybe &array[element] works.
MPI_Send(&array[element],element_left,MPI_INT,i,0,MPI_COMM_WORLD);
here array[]={1,2,3,4,5,6,7,8,9,10} and element = 6 and element_left = 4. I understand array[element]=array[6]=7 but why this function picks 7,8,9,10? I know it will pick 4 elements from the array but why do we need & here and by only giving starting entry array[6] how is this function able to pick the next 3 as well?
I thought I have to add one after another using a for loop or something, but when I searched something on Google I got this code and after going through so much I still didn't understand. Please help me understand the backwardness of this code.
&array[element] is the same expression as array + element and means the address of the elementth element of the array array.
The function you call wants this address as the first argument, and takes the number of elements to process as the second argument.
Most MPI routines take a trio of arguments:
address of buffer
count of elements
datatype of elements
So by &array[element],element_left,MPI_INT you specify the elements element as the start of the buffer, and then you take element_left many integers to send. Kinda strange that you name the count element_left which is more like a name for an index, but that's what happens.

Is there a way to sort a no. of character arrays in alphabetical order without using the #include<string.h> or #include<stdlib.h>?

So, I have tried to do the same in a case of array of structures where 'char name[100]' is the only data member.
1st part of the code
2nd part of the code
The problem that I have encountered here is that once I provide a no. of names during program runtime, the output screen either does not print anything afterwards, or, prints the data without sorting it.
output screen
I did not get any compile time errors so I believe that there is a flaw in the logic.
There's this another method I tried hoping to get positive results. I type-casted characters to integers hoping that ASCII values could be used to compare. But, the results are exactly the same (undesired results).
updated logic of the 2nd part of the code
I hope somebody helps me find a way to correct this logic or provide another logic that is efficient.
the sorting logic you used is good , but from what is see the use of function's in C need's to be provided by pointers. other wise all the data inside the function will born and die inside the function , and the all the variables in the Main will stay the same as given, that explains why the output is the same as the input
try to print inside the sorting function's to see if this is the problem.

how to construct an array with specific element size in python3?

you might ask "why don't you just use C?",
but I would like to try constructing an array
with specific element size using python 3, is that possible?
I know bytearray() but it's limited to one byte per element, is there a more flexible function?
also, is there any equivalent statement in python 3 to "sizeof(int)" in C?
I am not talking about sys.getsizeof(int) since it gives me the bytesize of the whole int class
x = [None]*10
This will initialize a list with size 10, but you can also take a look at arrays from the numpy module. This will also have lots of other optimization in it too.
I don't have much experience with your memory problem, but maybe this code will help? http://code.activestate.com/recipes/546530/

Perl: Reading from file and saving into an array

I had got a script in Perl and my task is to do some changes in it. This of course means to understand which part does exactly what. I am not familiar with Perl language but I tried to read through some tutorials, but still some things are confusing me. And I got stuck in following part:
while (<KOEFICIENTYfile>) {
#_=(split(/\s+/, $_));
push(#ZAID,shift(#_));
$KOEFICIENTY{$ZAID[-1]}=[#_];
}
As I understands this part then it:
Reads line from filehandle KOEFICIENTYfile
Separates them by spaces (one or more)
Loads first item from this separated array into array ZAID (and in the process, removes it from #_)
??? Adds a rest of a separated array into array KOEFICIENTY? I am confused by curly brackets part and by square brackets after equals sign.
I think that I understood the meaning of #, $, #_ or negative indexing but this is beyond me. Can you please advice me on meaning of this?
[-1] indexing is just a shortcut way to say "last element of the array".
KOEFICIENTY is actually a hash (you can tell this because it is using curly braces, instead of square ones, around the index), so you're putting the rest of the array #_ into a hash called KOEFICIENTY with a key of the last element of the array.
If you include:
use Data::Dumper
at the top of the script and do
print Dumper(%KOEFICIENTY)
it will nicely format the output for you, which may help
The original coder was trying to be too clever using the negative offset. It would have been much more obvious if the code had been written with a simple temporary variable thus:
while (<KOEFICIENTYfile>) {
#_ = (split(/\s+/, $_));
my $key = shift(#_);
push(#ZAID, $key);
$KOEFICIENTY{$key} = [#_];
}
The braces on $KOEFICIENTY show that this is a "hash" of key/value pairs named %KOEFICIENTY, and not a normal array.
If you don't actually need to preserve the sort order of the keys you could just use keys %KOEFICIENTY to retrieve them instead of storing them in #ZAID.
#zaid is a list, into which the first part of the split is added.
%KOEFICIENTY is a hash, in which a reference to the rest of the split is stored as a list reference under the key of the first part.
So if the line is a b c, #zaid will get a and %KOEFICIENTY{'a'} will hold a reference to a list containing b and c.

Regular expression to extract string in C Code (not inside comment)

I have this code in C but I only know how to extract string with regular expression that not inside comment code:
1. /* * "path_build()" function in "home.c" for more information.
2. * this is an example basic"
3. */
4.
5. /*** Free ***/
6. VALOR = string_make(format("%sxtra", libpath));
7. event_signal_string(EVENT_INITSTATUS, "Inicializando...");
should only return:
"%sxtra"
"Inicializando..."
I try:
".*"
but its don't work, it show me all text inside "", including the strings that inside /*...*/
I use EditPag Pro, RegExp panel.
It's a game translation project, I take the string of every C file and I translate to Spanish. I can't remove the comments of the original file.
The only thing I have clear is that this is the regex to find comments in C, maybe that will help the solution:
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)
Any help?
Edit: I put number of lines.
Hernaldo, this is an interesting question.
Here are two versions because I am not sure if you want to capture the "inside of the string" or "the whole string"
The regexps below capture the strings to capture Group 1. You completely ignore the overall match (Group 0) and just focus on Group 1. To retrieve the strings, just iterate over Group 1 matches in your language (discarding empty strings if any).
Version 1: "The inside of the string"
(?s)/\*.*?\*/|"([^"]+)"
This will capture %sxtra and Inicializando... to Group 1.
Version 2: "The whole string"
(?s)/\*.*?\*/|("[^"]+")
This will capture "%sxtra" and "Inicializando..." to Group 1.
Please let me know if you have any questions!
Note: I did not handle /* nested /* comments */ */ as that was not specified in the question. That would require a bit of tweaking and probably a regex engine supporting recursion.
The final solution for EditPad 6/7 is:
(?<!^[ \t]*/?[*#][^"\n]*")(?<=^[^"\n]*")[^"]+
Link:
Regular expression for a string that does not start with a /*

Resources