Mercurial > repos > deepakjadmin > mayatool3_test2
comparison lib/Parsers/SimpleCalcYYLexer.pm @ 0:4816e4a8ae95 draft default tip
Uploaded
author | deepakjadmin |
---|---|
date | Wed, 20 Jan 2016 09:23:18 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4816e4a8ae95 |
---|---|
1 package Parsers::SimpleCalcYYLexer; | |
2 # | |
3 # $RCSfile: SimpleCalcYYLexer.pm,v $ | |
4 # $Date: 2015/02/28 20:50:55 $ | |
5 # $Revision: 1.9 $ | |
6 # | |
7 # Author: Manish Sud <msud@san.rr.com> | |
8 # | |
9 # Copyright (C) 2015 Manish Sud. All rights reserved. | |
10 # | |
11 # This file is part of MayaChemTools. | |
12 # | |
13 # MayaChemTools is free software; you can redistribute it and/or modify it under | |
14 # the terms of the GNU Lesser General Public License as published by the Free | |
15 # Software Foundation; either version 3 of the License, or (at your option) any | |
16 # later version. | |
17 # | |
18 # MayaChemTools is distributed in the hope that it will be useful, but without | |
19 # any warranty; without even the implied warranty of merchantability of fitness | |
20 # for a particular purpose. See the GNU Lesser General Public License for more | |
21 # details. | |
22 # | |
23 # You should have received a copy of the GNU Lesser General Public License | |
24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or | |
25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330, | |
26 # Boston, MA, 02111-1307, USA. | |
27 # | |
28 | |
29 use strict; | |
30 use Carp; | |
31 use Exporter; | |
32 use Scalar::Util (); | |
33 use Parsers::YYLexer; | |
34 | |
35 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | |
36 | |
37 @ISA = qw(Parsers::YYLexer Exporter); | |
38 @EXPORT = qw(); | |
39 @EXPORT_OK = qw(); | |
40 | |
41 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |
42 | |
43 # Setup class variables... | |
44 my($ClassName, $YYTabFile, @YYLexerTokensSpec); | |
45 _InitializeClass(); | |
46 | |
47 # Overload Perl functions... | |
48 use overload '""' => 'StringifySimpleCalcYYLexer'; | |
49 | |
50 # Class constructor... | |
51 sub new { | |
52 my($Class, $Input) = @_; | |
53 my(@TokensSpec); | |
54 | |
55 # Initialize object... | |
56 my $This = $Class->SUPER::new($Input, @YYLexerTokensSpec); | |
57 bless $This, ref($Class) || $Class; | |
58 $This->_InitializeYYLexer(); | |
59 | |
60 return $This; | |
61 } | |
62 | |
63 # Initialize object data... | |
64 # | |
65 sub _InitializeYYLexer { | |
66 my($This) = @_; | |
67 | |
68 # Setup default YYTabFile containing mapping of token names to numbers... | |
69 $This->SetupYYTabFile($YYTabFile); | |
70 | |
71 return $This; | |
72 } | |
73 | |
74 # Initialize class ... | |
75 sub _InitializeClass { | |
76 #Class name... | |
77 $ClassName = __PACKAGE__; | |
78 | |
79 # Setup default token table file... | |
80 $YYTabFile = "Parsers/SimpleCalcParser.tab.ph"; | |
81 | |
82 # Setup default lexer tokens specs... | |
83 @YYLexerTokensSpec = ( | |
84 [ 'LETTER', qr/[a-zA-Z]/ ], | |
85 [ 'NUMBER', qr/\d+/ ], | |
86 [ 'SPACE', qr/[ ]*/, sub { my($This, $TokenLabel, $MatchedText) = @_; return ''; } ], | |
87 [ 'NEWLINE', qr/(?:\r\n|\r|\n)/, sub { my($This, $TokenLabel, $MatchedText) = @_; return "\n"; } ], | |
88 [ 'CHAR', qr/./ ] | |
89 ); | |
90 } | |
91 | |
92 # Is it a lexer object? | |
93 sub _IsSimpleCalcYYLexer { | |
94 my($Object) = @_; | |
95 | |
96 return (Scalar::Util::blessed($Object) && $Object->isa($ClassName)) ? 1 : 0; | |
97 } | |
98 | |
99 # Return a string containing information about lexer... | |
100 sub StringifySimpleCalcYYLexer { | |
101 my($This) = @_; | |
102 my($SimleCalcYYLexerString); | |
103 | |
104 $SimleCalcYYLexerString = "SimpleCalcYYLexer: PackageName: $ClassName; " . $This->_GetYYLexerInfoString(); | |
105 | |
106 return $SimleCalcYYLexerString; | |
107 } | |
108 | |
109 1; | |
110 | |
111 __END__ | |
112 | |
113 =head1 NAME | |
114 | |
115 Parsers::SimpleCalcYYLexer | |
116 | |
117 =head1 SYNOPSIS | |
118 | |
119 use Parsers::SimpleCalcYYLexer; | |
120 | |
121 use Parsers::SimpleCalcYYLexer qw(:all); | |
122 | |
123 =head1 DESCRIPTION | |
124 | |
125 B<SimpleCalcYYLexer> class provides the following methods: | |
126 | |
127 new, YYLex, GetYYLex, StringifySimpleCalcYYLexer | |
128 | |
129 B<Parser::SimpleCalcYYLexer> class is derived from B<Parser::YYLexer> class, which in | |
130 turn is derived from base class B<Parser::Lexer> that provides all the underlying | |
131 lexer functionality. B<SimpleCalcYYLexer> class is designed to be used with | |
132 B<yyparse> code generated by running B<byacc> on a parser defined using | |
133 parser definition B<SimpleCalcParser.yy> file. | |
134 | |
135 The parser package and token table files, B<SimpleCalcParser.pm> and B<SimpleCalcParser.tab.ph>, | |
136 are automatically generated from parser grammar definition file, B<SimpleCalcParser.yy>, using | |
137 byacc available through perl-byacc1.8 modified with perl5-byacc-patches-0.5 for generation | |
138 of object oriented parser: | |
139 | |
140 byacc -l -P -d -b SimpleCalcParser SimpleCalcParser.yy | |
141 mv SimpleCalcParser.tab.pl SimpleCalcParser.pm | |
142 | |
143 B<SimpleCalcYYLexer.pm> class implements a lexer for a simple calculator and is provided | |
144 to highlight usasge of B<YYLex> through B<yyparse>. | |
145 | |
146 The default specification of lexer tokens for B<SimpleCalcYYLexer.pm> includes: | |
147 | |
148 @YYLexerTokensSpec = ( | |
149 [ 'LETTER', qr/[a-zA-Z]/ ], | |
150 [ 'NUMBER', qr/\d+/ ], | |
151 [ 'SPACE', qr/[ ]*/, | |
152 sub { my($This, $TokenLabel, $MatchedText) = @_; return ''; } | |
153 ], | |
154 [ 'NEWLINE', qr/(?:\r\n|\r|\n)/, | |
155 sub { my($This, $TokenLabel, $MatchedText) = @_; return "\n"; } | |
156 ], | |
157 [ 'CHAR', qr/./ ] | |
158 ); | |
159 | |
160 The default B<SimpleCalcParser.tab.ph> file containing token identifiers for | |
161 B<SimpleCalcParser.yy> includes: | |
162 | |
163 $NUMBER=257; | |
164 $LETTER=258; | |
165 | |
166 =head2 METHODS | |
167 | |
168 =over 4 | |
169 | |
170 =item B<new> | |
171 | |
172 $SimpleCalcYYLexer = new Parsers::SimpleCalcYYLexer($Input); | |
173 | |
174 Using specified I<Input>, B<new> method generates a new B<SimpleCalcYYLexer> | |
175 and returns a reference to newly created B<SimpleCalcYYLexer> object. | |
176 | |
177 Examples: | |
178 | |
179 # Input string... | |
180 $InputText = "3 + 4 +6\nx=3\ny=5\nx+y\nx+z\n"; | |
181 | |
182 $YYLexer = new Parsers::SimpleCalcYYLexer($InputText); | |
183 $YYLex = $YYLexer->GetYYLex(); | |
184 | |
185 $Debug = 0; | |
186 $CalcParser = new Parsers::SimpleCalcParser($YYLex, | |
187 \&Parsers::SimpleCalcParser::yyerror, $Debug); | |
188 $Value = $SimpleCalcParser->yyparse(); | |
189 print "Value: $Value\n"; | |
190 | |
191 # Input file... | |
192 $InputFile = "Input.txt"; | |
193 open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n"; | |
194 $YYLexer = new Parsers::SimpleCalcYYLexer($InputFile); | |
195 $YYLex = $YYLexer->GetYYLex(); | |
196 | |
197 $CalcParser = new Parsers::SimpleCalcParser($YYLex, | |
198 \&Parsers::SimpleCalcParser::yyerror); | |
199 $Value = $SimpleCalcParser->yyparse(); | |
200 print "Value: $Value\n"; | |
201 | |
202 # Input file iterator... | |
203 $InputFile = "TestSimpleCalcParser.txt"; | |
204 open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n"; | |
205 $InputIterator = sub { return <INPUTFILE>; }; | |
206 $YYLexer = new Parsers::SimpleCalcYYLexer($InputIterator); | |
207 $YYLex = $YYLexer->GetYYLex(); | |
208 | |
209 $CalcParser = new Parsers::SimpleCalcParser($YYLex, | |
210 \&Parsers::SimpleCalcParser::yyerror); | |
211 $Value = $SimpleCalcParser->yyparse(); | |
212 print "Value: $Value\n"; | |
213 | |
214 =item B<StringifySimpleCalcYYLexer> | |
215 | |
216 $YYLexerString = $YYLexer->StringifySimpleCalcYYLexer(); | |
217 | |
218 Returns a string containing information about I<YYLexer> object. | |
219 | |
220 =back | |
221 | |
222 =head1 AUTHOR | |
223 | |
224 Manish Sud <msud@san.rr.com> | |
225 | |
226 =head1 SEE ALSO | |
227 | |
228 Lexer.pm, YYLexer.pm, SimpleCalcParser.yy | |
229 | |
230 =head1 COPYRIGHT | |
231 | |
232 Copyright (C) 2015 Manish Sud. All rights reserved. | |
233 | |
234 This file is part of MayaChemTools. | |
235 | |
236 MayaChemTools is free software; you can redistribute it and/or modify it under | |
237 the terms of the GNU Lesser General Public License as published by the Free | |
238 Software Foundation; either version 3 of the License, or (at your option) | |
239 any later version. | |
240 | |
241 =cut |