Wednesday, December 14, 2011

PERL PARSER DEFINITIONS

PERL PARSER DEFINITIONS




REVISED: Sunday, March 3, 2013




Perl parser basic concepts.

I.  PERL PARSER DEFINITIONS

A. SCANNER

A scanner is the first module in a compiler or interpreter. A scanner's job is to read the source file one character at a time. A scanner can also keep track of which line number and character is currently being read.

B.  LEXER

A lexer is a module that serves to break up the source file into pieces called tokens. A lexer calls the scanner to get characters one at a time and organizes them into tokens and token types. The lexer calls the scanner to pass the scanner one character at a time and groups them together and identifies them as tokens for the language parser.

C.  PARSER

A parser is the part of a compiler that really understands the syntax of the language. A parser calls the lexer to get tokens and processes the tokens according to the syntax of the language.

Perl is "the language for text analysis."  The built-in operators make pattern matching, text searching, and replacing enjoyable.

Lets start by examining the following file, and we will have fun as we go along.

II.  PERL OUTPUT EXAMPLE SOURCE CODE

"Copy Paste" the following Perl program into your text editor:

#Unicode
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag.
use diagnostics;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
#The my() function makes the variables local to the main package.
use strict;

#stack trace
use Carp ();
   local $SIG{__WARN__} = \&Carp::confess;

open STDOUT, ">C:/Strawberry/flower.txt";      
print "A bug sat in a silver flower\n";           
print "thinking silver thoughts.\n";
print "A bigger bug out for a walk\n";   
print "climbed up that silver flower stalk\n";
print "and snapped the small bug down his jaws\n";           
print "without a pause\n";
print "without a care\n";   
print "for all the bugs small silver thoughts.\n";
print "It isnt right it isnt fair that big bug ate that little bug\n";           
print "because that little bug was there.\n";
print "\n";   
print "He also ate his underwear.\n";
close STDOUT;

From your text editor, "File Save As" writeFlower.pl using the path to the folder of your Perl download; e.g.:

C:\Strawberry\writeFlower.pl

Depending on your operating system platform, when you open Perl, you will have a Perl prompt similar to the following:

C:\WINDOWS\system32>

From the above Perl prompt type:

C:\WINDOWS\system32>C:\Strawberry\writeFlower.pl

Press Enter.

After you press Enter, Perl will display the following:

C:\WINDOWS\system32>

Instead of writing the poem to your screen, Perl wrote the poem to the file flower.txt using the path to the folder of the Strawberry Perl download. flower.txt will be the first file we will examine as we write our Perl parsers.

III.  PERL INPUT EXAMPLE SOURCE CODE

"Copy Paste" the following Perl program into your text editor:

#Unicode
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag.
use diagnostics;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
#The my() function makes the variables local to the main package.
use strict;

#stack trace
use Carp ();
   local $SIG{__WARN__} = \&Carp::confess;

#Specify the file.
$file = "C:/Strawberry/flower.txt";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";
#Read the file into the array @lines.
@lines = <FILE>; 
#Print the file to the screen.
print @lines;
#Close the file.
close FILE;

"File Save As" readFlower.pl using the path to the folder of your Perl download; e.g.:

C:\Strawberry\readFlower.pl 

From the Perl prompt type the following:

C:\WINDOWS\system32>C:\Strawberry\readFlower.pl

Press Enter and the following will display on your screen:


C:\WINDOWS\system32>C:\Strawberry\readFlower.pl
A bug sat in a silver flower
thinking silver thoughts.
A bigger bug out for a walk   
climbed up that silver flower stalk
and snapped the small bug down his jaws
without a pause
without a care
for all the bug’s small silver thoughts.
It isn’t right it isn’t fair that big bug ate that little bug
because that little bug was there.

He also ate his underwear.

C:\WINDOWS\system32>

You have learned some of the basic concepts required for a Perl parser, enjoy!

Elcric Otto Circle







-->




-->




-->













How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"








PERL WARN( ) FUNCTION

PERL WARN( ) FUNCTION


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl warn( ) function.

I.  PERL WARN( ) FUNCTION

The warn(  ) function and the die(  ) function have the same functionality; however, when the warn(  ) function is used your program is not exited.

The warn(  ) function is better suited for non-fatal messages like low memory or disk space conditions.

If we try to change to the /examples directory and the change fails, the consequences are not fatal because the files can still be written to the current directory.

chdir('/examples') ||
   warn("WARNING: Using current directory instead of /examples: $!\n");
 
If the /examples directory does not exist, the above code displays the following error message:

WARNING: Using current directory instead of /examples:

II.  PERL $!

The $! variable is used in the above error message to have it followed by a display of the appropriate system error message.

You have learned how to use the Perl warn( ) function.

Elcric Otto Circle




-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






PERL FOREACH LOOP

PERL FOREACH LOOP


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl foreach loop.

I.  PERL FOREACH LOOP

The basic kinds of loops in Perl are the for loop, the while loop, and the foreach loop. The foreach loop is a form of the for loop.

The foreach loop is used to iterate through the elements of an array.

The foreach loop can be used to:

A.  Find the largest element in an array.

B.  Print the elements of an array.

C.  Determine if a certain value is one of the elements of an array.

II.  PERL FOREACH LOOP SYNTAX

The syntax for the foreach loop is as follows:

foreach loopVariable (ARRAY) {
    statements;
}

The loopVariable is assigned the value of each array element, until the end of the array is reached.

III.  PERL FOREACH LOOP EXAMPLE

In addition to the Perl builtin system functions Perl gives you the ability to make subroutines, which are user-defined functions.  The Perl builtin system functions can be combined and incorporated into your user-defined functions.

The following subroutine uses the foreach loop to iterate through the elements of an array:

#Subroutine max definition
#Parameter array @_
sub max {
   my($max) = shift(@_);

   foreach my $temp (@_) {
   $max = $temp if $temp > $max;
   }

   return($max);
}

IV.  PERL FOREACH LOOP SOURCE CODE

A.  CREATE TEXT INPUT DATA FILE

First we need to create the text input data file.

A Perl script is just a text file.  Perl script files end with the .pl extension.

"Copy Paste" the following Perl program into your text editor:

#Unicode
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag.
#Turn on verbose warnings.
use diagnostics;

#Helps you catch typos.
#Forces you to use my( ) function to declare all variables.
#my variables (lexical variables) are faster than globals.
use strict;

#Stack trace.
use Carp ();
      local $SIG{__WARN__} = \&Carp::confess;

open STDOUT, ">c:/strawberry/array.txt";

print "0\n";
print "1\n";
print "2\n";
print "3\n";
print "4\n";
print "5\n";
print "6\n";
print "7\n";
print "8\n";
print "9\n";

close STDOUT;

"File Save As" writeArray.pl using the path of the Perl download; e.g.:

c:\strawberry\writeArray.pl

Depending on your platform you will have a Perl prompt as follows:

C:\WINDOWS\system32>

From the above Perl prompt type:

C:\WINDOWS\system32>c:\strawberry\writeArray.pl

Press Enter.

Perl will display the following on your Perl prompt:

C:\WINDOWS\system32>

The Perl prompt screen is blank because the numbers we plan to load into our array were printed to the array.txt file on your hard drive and not your Perl prompt screen.

Now we have a text file named array.txt which contains the numbers we want to read from the text file into an array.

B.  PROCESS DATA FILE

"Copy Paste" the following Perl program into your text editor:

#Unicode.
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag
#Turn on verbose warnings.
use diagnostics;

#Helps you catch typos.
#Forces you to use my( ) function to declare all variables.
#my variables (lexical variables) are faster than globals.
use strict;

#Stack trace.
use Carp ();
local $SIG{__WARN__} = \&Carp::confess;

#Subroutine max definition
#Parameter array @_
sub max {
my($max) = shift(@_);

foreach my $temp (@_) {
$max = $temp if $temp > $max;
}

return($max);
}

#Specify the file.
my $file = "c:/strawberry/array.txt";

#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";

#Read text data file into array @lines.
my @lines =  <FILE>;

#Call (invoke) subroutine and print return.
print &max(@lines);

#Close file handle FILE.
close FILE;

"File Save As" readArray.pl using the path of the Perl download; e.g.:

c:\strawberry\readArray.pl

From the Perl prompt window type the following:

C:\WINDOWS\system32>c:\strawberry\readArray.pl

Press Enter and the following will display on your Perl prompt window:

C:\WINDOWS\system32>c:\strawberry\readArray.pl
9

C:\WINDOWS\system32>

The above example demonstrates the basic steps you will perform when you want to use data file input with your Perl programs.

V.  PERL FOREACH LOOP SUMMARY

When you want to solve a problem that includes using an array always think about how the foreach loop can help you.  Using the foreach loop we easily found the largest element in our array.  All we needed was data to load into the array.  The data will be the information you want to process.

You have learned how to use the Perl foreach loop.

Elcric Otto Circle




-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




PERL FUNCTIONS AND OPERATORS

PERL FUNCTIONS AND OPERATORS



 

REVISED: Sunday, March 3, 2013



PERL FUNCTIONS AND OPERATIONS


absabsolute value
chdirchange current directory
chmodchange permissions of file/directory
chompremove terminal newline from string variable
chopremove last character from string variable
chownchange ownership of file or directory
closeclose a file handle
closedirclose a directory handle
coscosine
definedtest whether variable is defined
deletedelete a key from a hash
dieexit with an error message
eachiterate through keys and values of a hash
eoftest a filehandle for end of file
evalevaluate a string as a perl expression
execquit Perl and execute a system command
existstest that a hash key exists
exitexit from the Perl script
globexpand a directory listing using shell wildcards
gmtimecurrent time in GMT
grepfilter an array for entries that meet a criterion
indexfind location of a substring inside a larger string
intthrow away the fractional part of a floating point number
joinjoin an array together into a string
keysreturn the keys of a hash
killsend a signal to one or more processes
lastexit enclosing loop
lcconvert string to lowercase
lcfirstlowercase first character of string
lengthfind length of string
localtemporarily replace the value of a global variable
localtimereturn time in local timezone
lognatural logarithm
m//pattern match operation
mapperform on operation on each member of array or list
mkdirmake a new directory
mycreate a local variable
nextjump to the top of enclosing loop
openopen a file for reading or writing
opendiropen a directory for listing
packpack a list into a compact binary representation
packagecreate a new namespace for a module
poppop the last item off the end of an array
printprint to terminal or a file
printfformatted print to a terminal or file
pushpush a value onto the end of an array
q/STRING/generalized single-quote operation
qq/STRING/generalized double-quote operation
qx/STRING/generalized backtick operation
qw/STRING/turn a space-delimited string of words into a list
randrandom number generator
readread binary data from a file
readdirread the contents of a directory
readlineread a line from a text file
readlinkdetermine the target of a symbolic link
redorestart a loop from the top
refreturn the type of a variable reference
renamerename or move a file
requireload functions defined in a library file
returnreturn a value from a user-defined subroutine
reversereverse a string or list
rewinddirrewind a directory handle to the beginning
rindexfind a substring in a larger string, from right to left
rmdirremove a directory
s///pattern substitution operation
scalarforce an expression to be treated as a scalar
seekreposition a filehandle to an arbitrary point in a file
selectmake a filehandle the default for output
shiftshift a value off the beginning of an array
sinsine
sleepput the script to sleep for a while
sortsort an array or list by user-specified criteria
spliceinsert/delete array items
splitsplit a string into pieces according to a pattern
sprintfformatted string creation
sqrtsquare root
statget information about a file
subdefine a subroutine
substrextract a substring from a string
symlinkcreate a symbolic link
systemexecute an operating system command, then return to Perl
tellreturn the position of a filehandle within a file
tieassociate a variable with a database
timereturn number of seconds since a given date
tr///replace characters in a string
truncatetruncate a file makes file smaller
ucuppercase a string
ucfirstuppercase first character of a string
umaskchange file creation mask
undefundefine removes a variable
unlinkdelete a file
unpackthe reverse of pack
untiethe reverse of tie
unshiftmove a value onto the beginning of an array
useimport variables and functions from a library module
valuesreturn the values of a hash variable
wantarrayreturn true in an array context
warnprint a warning to standard error
writeformatted report generation

Elcric Otto Circle


-->   -->   -->








How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"