Errors with the Flex Tool - lex

/*Regular Definitions*/
delim[\t\n]
WS{delim}+
letter[A-Za-z]
digit[0-9]
id{letter}({letter|digit})*
number{digit}+(\.{digit}+?(E[+-]?{digit}+)?
%%
{WS}{/*do nothing*/}
if{printf("\nIF found");return 0;}
then{printf("\nTHEN found");return 0;}
else{printf("\nELSE found");return 0;}
{id}{printf("\nID found");return 0;}
{number}{printf("\nNUMBER found");return 0;}
"<"{printf("\nLess than symbol found.");return 0;}
"<="{printf("\nLess than or Equals to symbol found.");return 0;}
"="{printf("\nEquals to symbol found.");return 0;}
"<>"{printf("\nNot equals to symbol found.");return 0;}
">"{printf("\nGreater than symbol found.");return 0;}
">="{printf("\nGreater than or equal to symbol found.");return 0;}
%%
While using flex to compile this
G:\>flex Lex.l
I get the following errors:
"Lex.l", line 14: unrecognized rule
"Lex.l", line 14: unrecognized rule
"Lex.l", line 14: unrecognized rule
"Lex.l", line 14: unrecognized rule
"Lex.l", line 14: unrecognized rule
"Lex.l", line 27: EOF encountered inside an action
Can anyone help me with it ?
Thanks.

1.- Surround with brackets each expression of the alternation.
{letter}|{digit}
2.- A closing parentheses was missed in the number definition. I added it before first ?, but not sure.
number{digit}+(\.{digit}+)?(E[+-]?{digit}+)?
3.- In the rule section, separate with spaces the pattern and the C code.
{WS} {/*do nothing*/}
This should work, or at least compile:
/*Regular Definitions*/
delim[\t\n]
WS{delim}+
letter[A-Za-z]
digit[0-9]
id{letter}({letter}|{digit})*
number{digit}+(\.{digit}+?(E[+-]?{digit}+)?)
%%
{WS} {/*do nothing*/}
if {printf("\nIF found");return 0;}
then {printf("\nTHEN found");return 0;}
else {printf("\nELSE found");return 0;}
{id} {printf("\nID found");return 0;}
{number} {printf("\nNUMBER found");return 0;}
"<" {printf("\nLess than symbol found.");return 0;}
"<=" {printf("\nLess than or Equals to symbol found.");return 0;}
"=" {printf("\nEquals to symbol found.");return 0;}
"<>" {printf("\nNot equals to symbol found.");return 0;}
">" {printf("\nGreater than symbol found.");return 0;}
">=" {printf("\nGreater than or equal to symbol found.");return 0;}
%%

Related

Why is a line break added when an element node is appended to a node that already has a text node?

I'm probably overlooking the obvious, but would you please explain why this code produces a line break after the span tag?
If the text node is not first appended to $div then there is not a line break after the span.
Thank you.
set div [$doc createElement div]
$div appendChild [$doc createTextNode Hello]
chan put stdout "[$div asHTML] -- line break: [string first \n [$div asHTML]]"
set span [$doc createElement span]
$span setAttribute class orig
$span appendChild [$doc createTextNode { there!}]
chan put stdout "[$span asHTML] -- line break: [string first \n [$span asHTML]]"
$div appendChild $span
chan put stdout "[$div asHTML] -- line break: [string first \n [$div asHTML]]"
# Results:
# <div>Hello</div> -- line break: -1
# <span class="orig"> there!</span> -- line break: -1
# <div>Hello<span class="orig"> there!</span>
# </div> -- line break: 43
It doesn't appear to be something in the above code because this code generates a line break also.
set em [$::doc createElement em]
$em appendFromList [ list {span} {class added} { {{#text} Hello}\
{{span} {class inner} {{{#text} { there!}}}}}]
set html [$em asHTML]
chan puts stdout "$html -- [string first \n $html]"
# <em><span class="added">Hello<span class="inner"> there!</span>
# </span></em> -- 63

PERL syntax error near "= ) "

I have found this example but I get this error:
syntax error at ./test.pl line 11, near "= ) " Execution of ./test.pl
aborted due to compilation errors.
Line 11: while($line = ) {
The author of the script didn't prepare the script to be included in an HTML. The actual code was
while ($line = <INFILE>) {
See readline for details.

Deciphering this syntax error

I cant seem to understand the reason for these syntax errors. The following is part of my code. I have strict and warnings implemented.
my $res = $s->scrape(URI->new($urlToScrape));
#warn Dump $res;
print "Show :".$res->{showtitle}[0];
my #sct;
if ( defined {season}[0] ) {
print $res->{season}[0];
#sct=split(' ', $res->{season}[0]);
} else {
my #spaa=split( {showtitle}[0], {fulltitle}[0] );
print "Couldnt find season num at proper position\n";
print $spaa[0]."\n";
print $spaa[1]."\n";
exit;
}
The error I get is:
$ ./htmlscrape.pl
"my" variable #spaa masks earlier declaration in same scope at ./htmlscrape.pl line 43.
"my" variable #spaa masks earlier declaration in same scope at ./htmlscrape.pl line 44.
syntax error at ./htmlscrape.pl line 37, near "}["
syntax error at ./htmlscrape.pl line 40, near "}"
syntax error at ./htmlscrape.pl line 46, near "}"
Execution of ./htmlscrape.pl aborted due to compilation errors.
There's syntax error in your code. Change:
if ( defined {season}[0] )
to
if ( defined $res->{season}[0] )
and
my #spaa=split( {showtitle}[0], {fulltitle}[0] );
to
my #spaa=split( $res->{showtitle}[0], $res->{fulltitle}[0] );
Also you are getting the warning
"my" variable #spaa masks earlier declaration in same scope at ./htmlscrape.pl line 43.
That means you have declared two arrays with the same name #spaa in same scope. You'll probably find Coping with Scoping by Dominus worth reading. Pay particular attention to the section called "Lexical Variables".

Perl syntax error: Bareword found where operator expected

As the title suggests how could I accomplish this?
I have been following a tutorial, but I get a syntax error:
Bareword found where operator expected at arrays_and_variables.pl line
26, near "$2names"
(Missing operator before names?) syntax error at
arrays_and_variables.pl line 26, near "$2names " Execution of
arrays_and_variables.pl aborted due to compilation errors.
The code I have so far is:
#names = ('james','dylan','max');
# join elements of array into a schalar variable.
$2names = join ('', #names);
print $s2names;
2names is an invalid variable name. Names can't start with a number—they have to begin with a letter or an underscore.

Perl syntax error, but I can't find it for the life of me

This error is angering me. I can't see anything near those lines with a parenthesis error or missing brackets. Someone give me a hand? This is my first post, forgive me if the formatting is off; I think I got it right.
EDIT: line 87, the ');' error, is this line: select(SEXTANT_DAEMON_LOG);
syntax error at -edited- line 87, near ");"
syntax error at -edited- line 92, near "if"
syntax error at -edited- line 99, near "if"
Unmatched right curly bracket at -edited- line 102, at end of line
syntax error at -edited- line 102, near "}"
syntax error at -edited- line 109, near "}"
syntax error at -edited- line 120, near ");"
BEGIN not safe after errors--compilation aborted at -edited- line 122.
This is the code near the error (full code here):
$MAIN_DBH = getConnection('Main');
$fs_logfile = getCSConfigValue($MAIN_DBH, 'Log', 'Sextant Update Daemon') or die "pid$$[" . localtime(time()) . "] Main dbh error: " . DBI::errstr;
open(SEXTANT_DAEMON_LOG, '>>', $fs_logfile) or die "pid$$[" . localtime(time()) . "] unable to open log file '$fs_logfile'\n";
$tmp = select(SEXTANT_DAEMON_LOG);
$| = 1;
select(SEXTANT_DAEMON_LOG);
Perl isn't giving a very good error message, but what it's actually complaining about is that "pid$$[" looks like an invalid attempt to access the array #$. Try replacing it with "pid$$\[".
The way I found that was by inserting __END__ near the reported location of the first error. I moved it up and down until I found the first line that caused an error, which was
$fs_logfile = getCSConfigValue($MAIN_DBH, 'Log', 'Sextant Update Daemon') or die "pid$$[" . localtime(time()) . "] Main dbh error: " . DBI::errstr;
Then I tried adding the backslash, and it fixed the error.
Note: perl -c is very useful in situations like this.