annotate docs/modules/txt/YYLexer.txt @ 0:4816e4a8ae95 draft default tip

Uploaded
author deepakjadmin
date Wed, 20 Jan 2016 09:23:18 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
1 NAME
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
2 Parsers::YYLexer
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
3
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
4 SYNOPSIS
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
5 use Parseres::YYLexer;
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
6
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
7 use Parsers::YYLexer qw(:all);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
8
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
9 DESCRIPTION
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
10 YYLexer class provides the following methods:
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
11
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
12 new, GetYYLex, Next, Peek, SetupYYTabFile, StringifyYYLexer, YYLex
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
13
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
14 Parsers::YYLexer class is derived from Parsers::Lexer base class, which
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
15 provides all the underlying lexer functionality. YYLexer class is
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
16 designed to be used with yyparse code generated by running byacc on a
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
17 parsers defined using parser definition ParserName.yy file.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
18
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
19 *YYTabFile* containing mapping of token labels to integers must be
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
20 explicitly specified by the caller. This file is processed during new
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
21 method invocation and mapping of token labels to integers is loaded in a
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
22 hash to be used later by YYLex method to return token number and text
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
23 pairs to the parser.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
24
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
25 METHODS
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
26 new
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
27 $YYLexer = new Parsers::YYLexer($Input, @YYLexerTokensSpec);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
28
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
29 Using specified *Input* and *YYLexerTokensSpec*, new method
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
30 generates a new YYLexer and returns a reference to newly created
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
31 YYLexer object.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
32
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
33 Examples:
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
34
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
35 # Tokens specifications supplied by the caller. It's an array containing references
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
36 # to arrays with each containing TokenLabel and TokenMatchRegex pair along with
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
37 # an option reference to code to be executed after a matched.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
38 #
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
39 @LexerTokensSpec = (
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
40 [ 'LETTER', qr/[a-zA-Z]/ ],
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
41 [ 'NUMBER', qr/\d+/ ],
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
42 [ 'SPACE', qr/[ ]*/,
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
43 sub { my($This, $TokenLabel, $MatchedText) = @_; return ''; }
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
44 ],
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
45 [ 'NEWLINE', qr/(?:\r\n|\r|\n)/,
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
46 sub { my($This, $TokenLabel, $MatchedText) = @_; return "\n"; }
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
47 ],
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
48 [ 'CHAR', qr/./ ]
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
49 );
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
50
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
51 # Input string...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
52 $InputText = 'y = 3 + 4';
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
53
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
54 $YLexer = new Parsers::YYLexer($InputText, @LexerTokensSpec);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
55
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
56 # Setup default token table file...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
57 $YYTabFile = "Parsers/SimpleCalcParser.tab.ph";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
58 $This->SetupYYTabFile($YYTabFile);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
59
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
60 # Process input stream...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
61 ($TokenNumber, $TokenText) = $YYLexer->Lex();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
62 print "TokenNumber: $TokenNumber; TokenText: $TokenText\n";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
63
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
64 # Input file...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
65 $InputFile = "Input.txt";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
66 open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
67 $Lexer = new Parsers::YYLexer(\*INPUTFILE, @LexerTokensSpec);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
68
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
69 # Input file iterator...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
70 $InputFile = "TestSimpleCalcParser.txt";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
71 open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
72 $InputIterator = sub { return <INPUTFILE>; };
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
73 $Lexer = new Parsers::YYLexer($InputIterator, @LexerTokensSpec);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
74
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
75 # Usage with code generated by byacc from a parser definition
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
76 # file SimpleCalcParser.yy...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
77
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
78 $InputText = "3 + 4 +6\nx=3\ny=5\nx+y\nx+z\n";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
79
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
80 $YYLexer = new Parsers::YYLexer($InputText,@LexerTokensSpec);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
81
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
82 $YYLex = $YYLexer->GetYYLex();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
83
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
84 $YYTabFile = "Parsers/SimpleCalcParser.tab.ph";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
85 $YYLexer->SetupYYTabFile($YYTabFile);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
86
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
87 $Debug = 0;
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
88 $SimpleCalcParser = new Parsers::SimpleCalcParser($YYLex,
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
89 \&Parsers::SimpleCalcParser::yyerror, $Debug);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
90
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
91 $Value = $SimpleCalcParser->yyparse();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
92 print "Value = " . (defined($Value) ? "$Value" : "Undefined") . "\n";
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
93
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
94 GetYYLex
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
95 $YYLex = $YYLexer->GetYYLex();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
96
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
97 Returns a curried verson of YYLexer as YYLex: yyparse in parser
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
98 generated by byacc expects it to call without passing any argument
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
99 for the *YYLexer* object.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
100
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
101 Next
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
102 ($TokenNumber, $TokenText) = $YYLexer->Next();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
103
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
104 Returns next available TokenNumber and any matched TokenText from
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
105 input stream by removing it from the input stream. Token number and
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
106 text of zero corresponds to end of input (EOI).
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
107
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
108 Peek
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
109 ($TokenNumber, $TokenText) = $YYLexer->Peek();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
110
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
111 Returns next available TokenNumber and any matched TokenText from
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
112 input stream by simply looking ahead and without removing it from
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
113 the input stream. Token number and text of zero corresponds to end
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
114 of input (EOI).
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
115
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
116 SetupYYTabFile
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
117 $YYLexer = $YYLexer->SetupYYTabFile($YYTabFile);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
118
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
119 Processes token labels to integers data map in specified *YYTabFile*
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
120 and returns *YYLexer*.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
121
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
122 Notes:
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
123
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
124 . YYTabFile must be a complete path or available through @INC path in the
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
125 same directory where this package is located.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
126 . Name of YYTabFile might start with any valid sub directory name in @INC
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
127 For example, "Parsers/<YYTablFile>" implies the tab file in parsers sub
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
128 directory under MayaChemTools lib directory as it would be already in @INC
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
129 path.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
130 . YYTabFile must be explicitly set by the caller. The default YYTabFile name,
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
131 y.tab.ph, generated by byacc is not used implicitly to avoid confusion
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
132 among multiple distinct instances of YYLexer.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
133 . YYTabFile is generated by byacc during its usage with -d options and
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
134 contains mapping of token codes to token names/labels. YYLexer used this
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
135 file to map token labels to token codes before returning token code and
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
136 value pair back to yyparse function used by byacc.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
137 . User defined token numbers start from 257
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
138 . Token number for any user defined token EOI is mapped to its value before
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
139 default token number of 0 for EOI.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
140
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
141 The format of YYTabFile generated by byacc during generation of parser code in
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
142 Perl code is:
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
143
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
144 ... ...
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
145 $NUMBER=257;
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
146 $ADDOP=258;
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
147 $SUBOP=259;
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
148 ... ..
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
149
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
150 YYLex
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
151 ($TokenNumber, $TokenText) = $YYLexer->YYLex();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
152 ($TokenNumber, $TokenText) = $YYLexer->YYLex($Mode);
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
153
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
154 Returns available TokenNumber and any matched TokenText from input
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
155 stream by either removing it from the input stream or by simply
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
156 looking ahead and without removing it from the input stream. Token
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
157 number and text of zero corresponds to end of input (EOI).
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
158
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
159 Possible *Mode* values: *Peek, Next*. Default: *Next*.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
160
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
161 *YYLex* is designed to be used with yyparse code generated by
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
162 running byacc on a parsers defined using parser definition
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
163 ParserName.yy file.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
164
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
165 Notes:
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
166
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
167 . Token label and value pairs returned by Lexer from by base class, which
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
168 can't be mapped to token labels specified in YYTabFile are ignored.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
169 . Token text of length 1 returned by Lexer from base class without a
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
170 corresponding explicit token label, which can't be mapped to a token
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
171 number using Perl ord function, is ignored.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
172
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
173 StringifyYYLexer
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
174 $YYLexerString = $YYLexer->StringifyYYLexer();
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
175
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
176 Returns a string containing information about *YYLexer* object.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
177
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
178 AUTHOR
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
179 Manish Sud <msud@san.rr.com>
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
180
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
181 SEE ALSO
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
182 Lexer.pm, SimpleCalcYYLexer.pm, SimpleCalcParser.yy
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
183
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
184 COPYRIGHT
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
185 Copyright (C) 2015 Manish Sud. All rights reserved.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
186
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
187 This file is part of MayaChemTools.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
188
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
189 MayaChemTools is free software; you can redistribute it and/or modify it
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
190 under the terms of the GNU Lesser General Public License as published by
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
191 the Free Software Foundation; either version 3 of the License, or (at
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
192 your option) any later version.
4816e4a8ae95 Uploaded
deepakjadmin
parents:
diff changeset
193