Trying to check a valid email using Lex, getting "Undefined definition" error instead - lex

I'm trying to create a simple program using Lex that checks if a given email address is valid or not. This is how I'm actually trying to achieve this:
Declarations section
%{
#include <stdio.h>
#include <string>
int correct = 0;
string email;
void write_data(string email, int correct);
/* Alias section */
name [a-zA-Z]
special [-_%+-]
digit [0-9]
domain [a-z]\.[a-z]{2,5}
email {name}+({digit} | {special})*#{domain}{1}
%}
Rules section
%%
{email} {correct = 1;}
%%
Programs section
int main(int argc, char* argv[])
{
if(argc == 2){
yyin = fopen(argv[1], "r");
email = yyin.getline();
if(yyin == NULL){
printf("Couldn't open %s file\n", argv[1]);
exit(-1);
}
}
else{
yyin = stdin;
email = yytext;
}
correct = 0;
yylex();
write_data(email, correct);
return 0;
}
void write_data(string email, int correct)
{
if(correct == 1)
printf("Email %s is correct\n", correo);
else
printf("Email %s is not correct\n", correo);
}
I'm trying to invoke the Lex tool using
lex template.l
And I'm getting the following error:
template.h:40: undefined definition {email}
I've been trying for a while already but I couldn't even run Lex on it so I don't know what could be wrong.

I figured it out for anyone interested. You MUST place the aliases out of the first %{%} section. The result of the Declaration section is as follows:
%{
#include <stdio.h>
#include <string>
int correct = 0;
string email;
void write_data(string email, int correct);
%}
/* Alias section */
name [a-zA-Z]
special [-_%+-]
digit [0-9]
domain [a-z]\.[a-z]{2,5}
email {name}+({digit} | {special})*#{domain}{1}

Related

How to display the result of function called using object reference in c++

#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
using namespace std;
class LetterDistribution
{
public: char district, trace;
public: int random_num;
public : LetterDistribution(){}
public: LetterDistribution(char dis)
{
district = dis;
trace = 'Z';
}
public: string LetterNumbers()
{
random_num = rand();
string letter_no ( district + " " + random_num);
return letter_no;
}
};
int main()
{
srand(time(0));
cout << "Enter district\n"<<endl;
char dis ;
cin >> dis;
LetterDistribution ld(dis);
cout << ld.LetterNumbers();
return 0;}
I am getting error in second last line inside main "cout << ld.LetterNumbers();". I am new to c++ , I have been working on C# earlier. I shall be thankful if someone could help me .
You have 2 issues in LetterNumbers function:
You can't add to string a number, you should convert the number to string first. you can do so by std::to_string(random_num)
You can't start concatenate string with a character, since character is like number in c++, and adding anything to number is a number. You should start from a string, even an empty one.
So the whole function can be something like:
string LetterNumbers()
{
random_num = rand();
string letter_no ( std::string("") + district + " " + std::to_string(random_num));
return letter_no;
}
Another issues: (but not errors!)
in c++ you can specify public: once, and everything after it is still public, until you change it. same thing for private and protected.
instead of <stdio.h> you should use <cstdio> which is the c++ wrapper for the c header.

perl match function for C program

Trying to use perl API functions in C program. Couldn't find the function to do regular expression match. Wish there is a function like regexmatch in the following program.
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
#include <sys/time.h>
typedef unsigned long ulong;
static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
int main(int argc, char **argv, char **env) {
int numOfArgs = 0;
PERL_SYS_INIT3(&numOfArgs, NULL, NULL);
my_perl = perl_alloc();
perl_construct(my_perl);
SV* str = newSVpv(argv[1], strlen(argv[1]));
if (regexmatch(str, "/hi (\S+)/")) {
printf("found a match\n");
}
return 0;
}
I know it's possible to use pcre library, just wonder if it's possible to get it from perl library here (libperl.so.5.14.2 on ubuntu 12.04)
Thanks!
UPDATE 1:
Did some google search and got the following simple program compiling. But when I ran the program as ./a.out ping pin, it gave "Segmentation fault" in the "pregcomp" function. Not sure why.
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
#include <sys/time.h>
#include <embed.h>
typedef unsigned long ulong;
static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
struct REGEXP * const engine;
int main(int argc, char **argv, char **env) {
int numOfArgs = 0;
PERL_SYS_INIT3(&numOfArgs, NULL, NULL);
my_perl = perl_alloc();
perl_construct(my_perl);
SV* reStr = newSVpv(argv[2], strlen(argv[2]));
printf("compiling regexp\n");
REGEXP * const compiled_regex = pregcomp(reStr, 0);
printf("execing regexp\n");
int len = strlen(argv[1]);
pregexec(compiled_regex, argv[1], argv[1] + len, argv[1], 5, NULL, 0);
return 0;
}
Don't mess with Perl's private internals. Call a Perl sub that uses the match operator.
Say you previously compiled the following in your interpreter (using eval_pv),
sub regex_match { $_[0] =~ $_[1] }
Then you can call
static bool regex_match_sv(SV* str, SV* re) {
dSP;
bool matched;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(str);
XPUSHs(re);
PUTBACK;
call_pv("regex_match", G_SCALAR);
SPAGAIN;
matched = SvTRUE(POPs);
PUTBACK;
FREETMPS;
LEAVE;
return matched;
}

Error while parsing comments in lex

Lex code:
identifier [\._a-zA-Z0-9\/]+
comment "//"
<*>{comment} {
cout<<"Comment\n";
char c;
while((c= yyinput()) != '\n')
{
}
}
<INITIAL>{s}{e}{t} {
BEGIN(SAMPLE_STATE);
return SET;
}
<SAMPLE_STATE>{identifier} {
strncpy(yylval.str, yytext,1023);
yylval.str[1023] = '\0';
return IDENTIFIER;
}
In the above lex code, there is no error when "// set name" is parsed. Please notice the space after "//" in the sentence parsed. However, when "//set name" is parsed, there is an error reported. Could you point to where I am going wrong? Thanks.
The error is caught by yyerror and reports
SampleParser.y:43: int CMTSTapTestSeq_yyerror(char*): Assertion `0 && "Error parsing Sample file\n"' failed.
This assertion is added by me.
I think you have made a mistake in simplifying your example, as the code you supplied works fine and does not have the fault you indicated. I coded it up and tested it (I used C instead of C++ for convenience). However, I see you posted a later question with more code that explained the problem better. I answered that one also.
s s
e e
t t
identifier [\._a-zA-Z0-9\/]+
comment "//"
%s SAMPLE_STATE
%{
//#include <iostream>
//using namespace std;
#include <stdio.h>
#define SET 1
#define IDENTIFIER 2
#define yyinput input
%}
%%
<*>{comment} {
// cout<<"Comment\n";
printf("Comment\n");
char c;
while((c= yyinput()) != '\n')
{
}
}
<INITIAL>{s}{e}{t} {
BEGIN(SAMPLE_STATE);
//return SET;
printf("SET\n");
}
<SAMPLE_STATE>{identifier} {
//strncpy(yylval.str, yytext,1023);
//yylval.str[1023] = '\0';
//return IDENTIFIER;
printf("identifier");
}
The accepts both:
//set name
// set name

implement multiple pipes in c

I'm trying to implement multiple pipes in c++. Namely, I want to write a program to simulate the execution of, for example, ls -l | head -n 10 | wc -l.
The code works all fine. But after all the commands are executed correctly, I need to hit enter to return to the command line. I guess I have to "wait()" somewhere
Here is the code I'm having now.
using namespace std;
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(){
int pid;
int fd[4];
pipe(fd + 0); // pipe between the 1st and 2nd command
pipe(fd + 2); // pipe between the 2nd and 3rd command
for( int i = 0; i < 3; i++){ // 3 commands
pid = fork();
if(pid == 0){// child process
if( i == 0 ){// first command
char *arg[10];
arg[0] = "ls";
arg[1] = NULL;
close(fd[0]);
dup2(fd[1], 1);
execvp(arg[0], arg);
}
else if( i == 1){// second command
char *arg[10];
arg[0] = "head";
arg[1] = "-n1";
arg[2] = NULL;
dup2(fd[0], 0);
dup2(fd[3], 1);
execvp(arg[0], arg);
}
else if( i== 2){// third command
char *arg[10];
arg[0] = "wc";
arg[1] = "-l";
arg[2] = NULL;
close(fd[3]);
dup2(fd[2], 0);
execvp(arg[0], arg);
}
}
else{// parent
}
}
}
I think I have gone thru all posts similar to mine, but still can't figure this out.
Can anyone help?

Drawing currency symbol

How to draw a currency symbol in a custom label using CGContextShowTextAtPoint method in draw rect.
Here the symbol is in string format.
Any help!!
Thanks
You have to resort to C style strings, since this is what CGContextShowTextAtPoint() requires. In order to correctly handle the locale (the currency symbol changes with the locale) you must use setlocale(), then you format your string using strfmon() and finally you pass the string created with strfmon() to CGContextShowTextAtPoint().
Documentation is available as follows from the terminal:
man 3 setlocale
man 3 strfmon
EDIT/UPDATE: For your information, strfmon() internally uses struct lconv. The structure can be retrieved with the function localeconv(). See man 3 localeconv for a detailed description of the fields available in the structure.
for instance, try the following simple C program setting different locales
#include <stdio.h>
#include <locale.h>
#include <monetary.h>
int main(void)
{
char buf[BUFSIZ];
double val = 1234.567;
/* use your current locale */
setlocale(LC_ALL, "");
/* uncomment the next line and try this to use italian locale */
/* setlocale(LC_ALL, "it_IT"); */
strfmon(buf, sizeof buf, "You owe me %n (%i)\n", val, val);
fputs(buf, stdout);
return 0;
}
The following uses localeconv():
#include <stdio.h>
#include <limits.h>
#include <locale.h>
int main(void)
{
struct lconv l;
int i;
setlocale(LC_ALL, "");
l = *localeconv();
printf("decimal_point = [%s]\n", l.decimal_point);
printf("thousands_sep = [%s]\n", l.thousands_sep);
for (i = 0; l.grouping[i] != 0 && l.grouping[i] != CHAR_MAX; i++)
printf("grouping[%d] = [%d]\n", i, l.grouping[i]);
printf("int_curr_symbol = [%s]\n", l.int_curr_symbol);
printf("currency_symbol = [%s]\n", l.currency_symbol);
printf("mon_decimal_point = [%s]\n", l.mon_decimal_point);
printf("mon_thousands_sep = [%s]\n", l.mon_thousands_sep);
printf("positive_sign = [%s]\n", l.positive_sign);
printf("negative_sign = [%s]\n", l.negative_sign);
}
I don't really get what you're asking,
checking the documentation, the method would look something like that:
CGContextRef ctx = UIGraphicsGetCurrentContext();
const char *string = "$";
CGContextShowTextAtPoint (ctx, 160, 240, string, 1);
Haven't tested it, but this should draw $ in the center of the screen.
BTW, why not use images?
~ Natanavra.