Write a lex program that detects and counts the pattern that starts with an uppercase letter, ends with a lowercase letter - lex

I understood the problem and written the below code. My code works and it prints the number of detected valid and invalid patterns when I quit the program with ctrl+z.
Here is my code:
%{
int valid = 0;
int invalid = 0;
%}
%%
([A-Z][a-zA-Z0-9]*[a-z])* {valid++;}
[a-zA-Z0-9]* {invalid++;}
%%
int yywrap(){}
int main(int argc, char **argv[])
{
printf("\n Enter inputs: \n\n");
yylex();
printf("\n\n\tNumber of VALID patterns = %d\n", valid);
printf("\tNumber of invalid patterns = %d\n\n", invalid);
return 0;
}
But I want something like this:
It should print the detected patterns, number of valid patterns and the number of invalid patterns whenever I input a new line.
There should be an EXIT command.

To achieve your goal, you should modify your code like this:
/*** Definition Section ***/
%{
int valid = 0;
int invalid = 0;
%}
/*** Rules Section ***/
%%
([A-Z][a-zA-Z0-9]*[a-z])* {printf("\n\tPattern Detected: %s ", yytext); valid++;}
[a-zA-Z0-9]* {invalid++;}
"\n" {
printf("\n\n\tNumber of VALID patterns = %d\n", valid);
printf("\tNumber of invalid patterns = %d\n\n", invalid);
valid = 0;
invalid = 0;
}
EXIT__ return 0;
%%
/*** User code section***/
int yywrap(){}
int main(int argc, char **argv[])
{
printf("\n Enter inputs: \n\n");
yylex();
return 0;
}
Here main change comes in the rule section.
Rule-1: ([A-Z][a-zA-Z0-9]*[a-z])* It detect and count valid patterns that starts with an uppercase letter, ends with a lowercase letter. In action, it prints the detected patterns and does the counting job too. Here yytext contains the text in the buffer, for this rule, it's the detected pattern.
Rule-2: [a-zA-Z0-9]* Keep a track of invalid patterns. It will help to prevent returning unmatched patterns.
Rule-3: "\n" It detects when you input a new line. In action, it prints the detected patterns, the number of valid patterns, and the number of invalid patterns whenever I input a new line. Also, reset the variables for counting to zero for the next line of input.
Rule-4: EXIT__ whenever you will input this exact command, the program will exit.
You can avoid printing the numbers of valid and invalid patterns inside the main function in the user code section.
But if you want to print the numbers of detected valid and invalid patterns at the end too, then this program will require a few modifications.

Related

How to get the length of a formatted string read in using fscanf in SystemVerilog?

I am reading a text file which has string test cases and decode them to process as Verilog test constructs to simulate. The code that I use to read a file is as follows:
integer pntr,file;
string a,b,c,d;
initial
begin
pntr = $fopen(FOO, "r");
end
always
begin
if(!$feof(pntr))
begin
file = $fscanf(pntr, "%s %s %s %s \n", a,b,c,d);
end
else
$fclose(pntr);
I have tried using
integer k;
k = strlen($fscanf(pntr, "%s %s %s %s \n", a,b,c,d));
$display(k);
and the display statement outputs an "x"
I also tried using
$display(file)
but this also gives me x as the display output. The above code is just a representation of my problem, I am using a larger formatted string to read in larger data. Each line of my testcase may have different size. I have initialized the format to the maximum number of string literals that my testcase can have. I wanted to ask if there is a way to get the length of each line that I read or number of string literals that fscanf read ?
Note: I am using Cadence tools for this task.
Input file looks like
read reg_loc1
write regloc2 2.5V regloc3 20mA
read regloc3 regloc5 regloc7
It's hard to debug your code when you have lots of typos and incomplete code. And you also have a race condition in that pntr may not have been assigned from $fopen if the always block executes before the initial block.
But in any case, the problem with using $fscanf and the %s format is that a newline gets treated as whitespace. It's better to use $fgets to read a line at a time, and the use $sscanf to parse the line:
module top;
integer pntr,file;
string a,b,c,d, line;
initial begin
pntr = $fopen("FOO", "r");
while(!$feof(pntr))
if ((file = $fgets(line,pntr)!=0)) begin
$write("%d line: ", file, line);
file = $sscanf(line, "%s %s %s %s \n", a,b,c,d);
$display(file,,a,b,,c,,d);
end
$fclose(pntr);
end
endmodule

USART format data type

i would like to ask, how i can send data via usart as integer, i mean variable which stores number. I am able to send char variable, but terminal shows me ascii presentation of this number and i need to see number.
I edited code like shown below but it gives me error: "conflicting types for 'USART_Transmit'"
#include <avr/io.h>
#include <util/delay.h>
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr );
void USART_Transmit( unsigned char data );
unsigned char USART_Receive( void );
int main( void )
{
unsigned char str[5] = "serus";
unsigned char strLenght = 5;
unsigned int i = 47;
USART_Init ( MYUBRR );
//USART_Transmit('S' );
while(1)
{
/*USART_Transmit( str[i++] );
if(i >= strLenght)
i = 0;*/
USART_Transmit(i);
_delay_ms(250);
}
return(0);
}
void USART_Init( unsigned int ubrr )
{
/* Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS)|(3<<UCSZ0);
}
void USART_Transmit( unsigned int data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC)) )
;
/* Get and return received data from buffer */
return UDR0;
}
Do you have any ideas what is wrong?
PS: I hope you understand what im trying to explain.
I like to use sprintf to format numbers for serial.
At the top of your file, put:
#include <stdio.h>
Then write some code in a function like this:
char buffer[16];
sprintf(buffer, "%d\n", number);
char * p = buffer;
while (*p) { USART_Transmit(*p++); }
The first two lines construct a null-terminated string in the buffer. The last two lines are a simple loop to send all the characters in the buffer. I put a newline in the format string to make it easier to see where one number ends and the other begins.
Technically a UART serial connection is just a stream of bits divided into symbols of a certain length. It's perfectly possible send the data in raw form, but this comes with a number of issues the must be addressed:
How to identify the start and end of a transmission unambiguously?
How to deal with endianess on either side of the connection?
How to serialize and deserialize the data in a robust way?
How to deal with transmission errors?
At the end of the day it turns out, that you never can resolve all the ambiguties and binary data somehow must be escaped or otherwise encoded to prevent misinterpretation.
As far as delimiting transmissions is concerned, that has been addressed by the creators of the ASCII standard through the set of nonprintable control characters: Of interest for you should be the special control characters
STX / 0x02 / Start of Text
ETX / 0x03 / End of Text
There are also other control characters which form a pretty complete set to make up data structures; you don't need JSON or XML for this. However ASCII itself does support the transmission of arbitrary binary data. However the standard staple for this task for a long time has been and is base64 encoding. Use that for transmission of arbitrary binary data.
Numbers you probably should not transmit in binary at all; just push digits around; if you're using octal or hexadecimal digits parsing into integers is super simple (boils down to a bunch of bit masking and shifting).

lex program to count the Number of Words

I made the following lex program to count the Number of words in a Textfile. A 'Word' for me is any string that starts with an alphabet and is followed by 0 or more occurrence of alphabets/numbers/_ .
%{
int words;
%}
%%
[a-zA-Z][a-zA-Z0-9_]* {words++; printf("%s %d\n",yytext,words);}
. ;
%%
int main(int argc, char* argv[])
{
if(argc == 2)
{
yyin = fopen(argv[1], "r");
yylex();
printf("No. of Words : %d\n",words);
fclose(yyin);
}
else
printf("Invalid No. of Arguments\n");
return 0;
}
The Problem is that for the following Textfile, I am getting the No. of Words : 13. I tried printing the yytext and it shows that it is taking 'manav' from '9manav' as a word even though it doesnot match my definition of a word.
I also tried including [0-9][a-zA-Z0-9_]* ; within my code but still shows the same output. I want to know why is this happening and possible ways to avoid it.
Textfile : -
the quick brown fox jumps right over the lazy dog cout for
9manav
-99-7-5 32 69 99 +1
First, the manav is perfectly matching your definition of word. The 9 in front of it is matched by the . rule. Remember, that white space is not special in lex.
You had the right idea by adding another rule [0-9][a-zA-Z0-9_]* ; but since the ruleset is ambiguous (there are several ways to match the input) order of the rules matters. It's a while I worked with lex but I think putting the new rule before the word rule should work.

Please explain this specific line in my Java Code?

I have just started learning Java and I reached till arrays , I was preparing this program(From a book) on replacing space ' ' with '.' (dots) and i am not able to understand this specific line (its not mentioned even in the book I am learning from).
Please help me out.
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
for(int dex = 0; dex<mf1.length;dex++)
{
char current = mf1[dex]; // What is happening in this line ??
if (current != ' ') {
System.out.print(current);
}
else{
System.out.print('.');
}
}
System.out.println();
}
}
Someone please explain what is happening in "char current = mf1[dex];"
Thanks a lot for your time.
You are getting the dexth character/item within the character array mf1 (hence mf1[dex]) and storing it into the local variable current.
Basically a String in java is an array of characters. So what the above code does is converts the string to an array of chars so that it can access each index of the array later on. Then the code enters into a for loop in order to iterate through all the indecies of the char array.
Assuming that that is already clear to you, the code now creates a char variable which holds the current index of the array.
char current = mf1[dex];
mf1 is your char array that represents the string. dex is the current index of the char that is determined by the for loop. So by doing this we can check each character (letter) of the char array. Now if the char "current" is a blank space we can replace it with a dot.
It's getting the character at index idx in the array mf1 and storing its value in the current variable.
The for-loop is iterating the string mostFamous character by character.
the line you are asking is to get the character at specific position. Function is similar to JavaScript's charAt(i)
char current = mf1[dex];
This line gets values from the mf1 char array and assign to the current variable according to the dex, dex works as index to the array element and it increments with the running loop.
The line
char current = mf1[dex];
is placed inside a for loop where the variable dex is incremented each time the loop is iterated. The variable dex is the zero-based index of the array. On the left hand side of the assignment operator (=), you are declaring a variable named current of type char. On the right hand side of the assignment operator you are accessing the dex-th character of your CharArray, if you start counting from zero. The assignment operator binds the variable you declared with the value of the character you specified on the right hand side.
For example, the first time the loop is run, dex would start at 0, hence mf1[dex] (or mf1[0]) is just 'H'.
Here is solution
class SpaceRemover{
public static void main(String[] args){
String mostFamous = "Hey there stackoverFLow ";
char [] mf1 = mostFamous.toCharArray();
// mf1 contains={'H', 'e','y',' ','t','h',.........}
for(char current: mf1)
{
//the for-each loop assigns the value of mf1 variable to the current variable
//At first time the 'H' is assigned to the current and so-on
System.out.print(current==' '?'.':current );
}
System.out.println();
}
}
}
It assigns the element of the char array mf1 at the index dex to the char variable current.
Note that the for loop and that line may be simplified by using the foreach syntax; these two code blocks are equivalent:
// Your code
for(int dex = 0; dex<mf1.length;dex++) {
char current = mf1[dex];
// Equivalent code
for (char current : mf1) {
But further, the whole method may be replaced by one line:
public static void main(String[] args){
System.out.println("Hey there stackoverFLow ".replace(" ", "."));
}
char current = mf1[dex];
this will return the char element in char array whose index is dex
This is quite a basic usage of array.
Good luck with your study.
After this statement, char [] mf1 = mostFamous.toCharArray();
mf1[0]=H, mf1[1]=e, mf1[1]=y...
so at this line, char current = mf1[dex];
so, in first iteration, current=H, second iteration current=e...

How to use XOR to Encrypt a music file

I want to encrypt and decrypt 10000 bytes of a music file. I used the code :
if(file!=NULL && g!= NULL)
{
while(!feof(file))
{
count++;
char aByte;
fread(&aByte, sizeof(char), 1, file);
if (count <= 10000)
{
if (count % 2 == 0)
{
if ((aByte ^ 'A') != EOF) aByte = aByte ^ 'A';
}
else
{
if ((aByte ^ 'B') != EOF) aByte = aByte ^ 'B';
}
}
fwrite(&aByte, sizeof(char), 1, g);
}
}
fclose(file);
But the code does not work. Please help!!!
You shouldn't do the XOR when you're checking for EOF (which, by the way, fread indicates in its return value rather than in the value it writes to your buffer); only when you're preparing the byte to be written to the file.
Also, you'll need a much longer key if you want to encrypt it properly — and the key must be unknown to anyone who isn't supposed to be able to decrypt it.
Test your file handling by removing the "encryption". Take out these lines
if (count % 2 == 0)
{
if ((aByte ^ 'A') != EOF) aByte = aByte ^ 'A';
}
else
{
if ((aByte ^ 'B') != EOF) aByte = aByte ^ 'B';
}
Is your file the exact same? I think it will not be. Here are some reasons
The file is longer than 10,000 bytes -- you close file at the end, so I assume that those bytes are lost
The file is less than 10,000 bytes -- you don't check for EOF property, so it looks like an extra byte will be written
count is not initialized in this code -- if it's odd on one run and even on another, it wont' match. Also, it's just going to copy a random number of bytes depending on what count is. Perhaps it's initialized elsewhere.
You don't close g, so you can't be sure it was flushed (fwrite can buffer writes)
UPDATE: EOF is not a byte that you can find in a file, it's a special return value used by some FILE API functions (not fread).
The function getchar() is defined like this:
int getchar();
It either returns EOF or an int that is within the range of char. If it's EOF, then that means that you are at the end of the file. If not, you may safely cast the return to a char. The only stipulation on EOF is that it isn't a valid char. getchar() returns an int because it can return any char (256 choices) + EOF for a total of 257 choices which can't be represented in a char.
If you look at fread(), you will see that it doesn't have anything to do with chars. It takes a buffer, and record size, and a record count and returns the number of bytes read. You happen to be passing in sizeof(char) and 1 to get a char. It couldn't possibly store EOF in that char because EOF is not a valid char, it's an int and won't fit. It doesn't use the same conventions as getchar(). If it encounters EOF, it returns a number less than the total number of bytes requested. In your case, that's 0. If fread() returns 0, check feof() and ferror() to find out if you got to the end-of-file or got an error (both cause fread to return less than the number requested).