comparison lib/Parsers/SimpleCalcParser.yy @ 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 %{
2 package Parsers::SimpleCalcParser;
3 #
4 # $RCSfile: SimpleCalcParser.yy,v $
5 # $Date: 2015/02/28 20:50:55 $
6 # $Revision: 1.10 $
7 #
8 # Author: Manish Sud <msud@san.rr.com>
9 #
10 # Copyright (C) 2015 Manish Sud. All rights reserved.
11 #
12 # This file is part of MayaChemTools.
13 #
14 # MayaChemTools is free software; you can redistribute it and/or modify it under
15 # the terms of the GNU Lesser General Public License as published by the Free
16 # Software Foundation; either version 3 of the License, or (at your option) any
17 # later version.
18 #
19 # MayaChemTools is distributed in the hope that it will be useful, but without
20 # any warranty; without even the implied warranty of merchantability of fitness
21 # for a particular purpose. See the GNU Lesser General Public License for more
22 # details.
23 #
24 # You should have received a copy of the GNU Lesser General Public License
25 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
26 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
27 # Boston, MA, 02111-1307, USA.
28 #
29 #
30 # A WORD TO THE WISE:
31 #
32 # The parser package and token table files, SimpleCalcParser.pm and
33 # SimpleCalcParser.tab.ph, are automatically generated from parser grammar
34 # definition file, SimpleCalcParser.yy, using byacc available through perl-byacc1.8
35 # modified with perl5-byacc-patches-0.5 for generation of object oriented parser:
36 #
37 # byacc -l -P -d -b SimpleCalcParser SimpleCalcParser.yy
38 # mv SimpleCalcParser.tab.pl SimpleCalcParser.pm
39 #
40
41 use Carp;
42
43 # Setup a hash map for mapping of words/letters to values...
44 %LetterToValueMap = ();
45
46 %}
47
48 %start list
49
50 %token NUMBER LETTER
51
52 %left '+' '-'
53 %left '*' '/'
54 %left '%'
55
56 %%
57
58 list :
59 | list stat '\n'
60 { $$ = $2; }
61 | list error '\n'
62 { $p->yyerrok; $p->yyclearin; }
63 ;
64
65
66 stat : expr
67 { $ExprOut = sprintf "%5i", $1; print "$ExprOut\n"; $$ = $1; }
68 | LETTER '=' expr
69 { $LetterToValueMap{$1} = $3; }
70 ;
71
72 expr : '(' expr ')'
73 { $$ = $2; }
74 | expr '+' expr
75 { $$ = $1 + $3; }
76 | expr '-' expr
77 { $$ = $1 - $3; }
78 | expr '*' expr
79 { $$ = $1 * $3; }
80 | expr '/' expr
81 { $$ = $1 / $3; }
82 | expr '%' expr
83 { $$ = $1 % $3; }
84 | NUMBER
85 | LETTER
86 {
87 if (exists $LetterToValueMap{$1}) {
88 $$ = $LetterToValueMap{$1};
89 }
90 else {
91 $Letter = $1;
92 print "Undefined variable $Letter encountered by SimpleCalcParser; Value set to 0\n";
93 $$ = 0;
94 }
95 }
96 ;
97
98 %%
99
100 # yyerror function supplied to parser along with a lexer during initialization of
101 # the parser...
102 #
103 sub yyerror {
104 my ($msg) = @_;
105 print "yyerror: $msg...\n";
106 }
107
108 1;
109
110 __END__
111
112 =head1 NAME
113
114 Parsers::SimpleCalcParser
115
116 =head1 SYNOPSIS
117
118 use Parsers::SimpleCalcParser ;
119
120 use Parsers::SimpleCalcParser qw(:all);
121
122 =head1 DESCRIPTION
123
124 B<Parsers::SimpleCalcParser> class provides the following methods:
125
126 new, yyclearin, yyerrok, yyerror, yyparse
127
128 B<Parsers::SimpleCalcParse.yy> parser grammer definition file implements a simple
129 calculator and is provided to highlight usage of lexer capability available through
130 B<Parsers::SimpleCalcYYLexer>, which in turn uses B<Parsers::YYLexer> and
131 B<Parsers::Lexer> classes to procide underlying lexer functionality.
132
133 The parser package and token table files, B<Parsers::SimpleCalcParser.pm> and
134 B<SimpleCalcParser.tab.ph>, are automatically generated from parser grammar definition
135 file, B<Parsers::SimpleCalcParser.yy>, using byacc available through perl-byacc1.8 modified
136 with perl5-byacc-patches-0.5 for generation of object oriented parser:
137
138 byacc -l -P -d -b SimpleCalcParser SimpleCalcParser.yy
139 mv SimpleCalcParser.tab.pl SimpleCalcParser.pm
140
141 =head2 METHODS
142
143 =over 4
144
145 =item B<new>
146
147 $SimpleCalcParser = new Parsers::SimpleCalcParser($YYLex,
148 \&Parsers::SimpleCalcParser::yyerror);
149 $SimpleCalcParser = new Parsers::SimpleCalcParser($YYLex,
150 \&Parsers::SimpleCalcParser::yyerror, $Debug);
151
152 Using specified I<YYLex> I<YYError> functions, B<new> method generates a new
153 B<SimpleCalcParser> and returns a reference to newly created B<SimpleCalcYYParser> object.
154
155 Examples:
156
157 # Input string...
158 $InputText = "3 + 4 +6\nx=3\ny=5\nx+y\nx+z\n";
159 $YYLexer = new Parsers::SimpleCalcYYLexer($InputText);
160 $YYLex = $YYLexer->GetYYLex();
161
162 $Debug = 0;
163 $SimpleCalcParser = new Parsers::SimpleCalcParser($YYLex,
164 \&Parsers::SimpleCalcParser::yyerror, $Debug);
165 $Value = $SimpleCalcParser->yyparse();
166 print "Value = " . (defined($Value) ? "$Value" : "Undefined") . "\n";
167
168 # Input file...
169 $InputFile = "TestSimpleCalcParser.txt";
170 open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n";
171
172 $YYLexer = new Parsers::SimpleCalcYYLexer(\*INPUTFILE);
173 $YYLex = $YYLexer->GetYYLex();
174
175 $Debug = 0;
176 $SimpleCalcParser = new Parsers::SimpleCalcParser($YYLex,
177 \&Parsers::SimpleCalcParser::yyerror, $Debug);
178 $Value = $SimpleCalcParser->yyparse();
179 print "Value = " . (defined($Value) ? "$Value" : "Undefined") . "\n";
180
181 close INPUTFILE;
182
183 # Input iterator...
184 $InputFile = "TestSimpleCalcParser.txt";
185 open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n";
186 $InputIterator = sub { return <INPUTFILE>; };
187
188 $YYLexer = new Parsers::SimpleCalcYYLexer($InputIterator);
189 $YYLex = $YYLexer->GetYYLex();
190
191 $Debug = 0;
192 $SimpleCalcParser = new Parsers::SimpleCalcParser($YYLex,
193 \&Parsers::SimpleCalcParser::yyerror, $Debug);
194 $Value = $SimpleCalcParser->yyparse();
195 print "Value = " . (defined($Value) ? "$Value" : "Undefined") . "\n";
196
197 close INPUTFILE;
198
199 =item B<yyclearin>
200
201 $SimpleCalcParser->yyclearin();
202
203 B<yyclearin> method clears any previous look-ahead token after encountering a syntax error
204 during parsing. It can be used after B<yyerrok> in a grammer rule with the reserved word
205 B<error>.
206
207 =item B<yyerrok>
208
209 $SimpleCalcParser->yyerrok();
210
211 B<yyerrok> method is used with the reserved word B<error> in grammer rule to indcate
212 error recovery is complete after encountering a syntax error during parsing.
213
214 =item B<yyerror>
215
216 $SimpleCalcParser->yyerror();
217
218 B<yyerror> function is provided for the caller to use during initialization of a parser. It
219 is used by B<yyparse> to print any error messages encountered during parsing of the
220 input.
221
222 =item B<yyparse>
223
224 $Value = $SimpleCalcParser->yyparse();
225
226 Returns I<Value> after parsing all the input from a input stream using specified
227 grammer rules.
228
229 =back
230
231 =head1 AUTHOR
232
233 Manish Sud <msud@san.rr.com>
234
235 =head1 SEE ALSO
236
237 Lexer.pm, YYLexer.pm, SimpleCalcYYLexer.pm
238
239 =head1 COPYRIGHT
240
241 Copyright (C) 2015 Manish Sud. All rights reserved.
242
243 This file is part of MayaChemTools.
244
245 MayaChemTools is free software; you can redistribute it and/or modify it under
246 the terms of the GNU Lesser General Public License as published by the Free
247 Software Foundation; either version 3 of the License, or (at your option)
248 any later version.
249
250 =cut
251