REVISED: Sunday, March 3, 2013
Perl parser basic concepts.
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.
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 bug’s small silver thoughts.\n";
print "It isn’t right it isn’t 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.:
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.
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>
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"