Previous  TOC  NextParsers/SimpleCalcYYLexer.pmCode | PDF | PDFA4




use Parsers::SimpleCalcYYLexer;

use Parsers::SimpleCalcYYLexer qw(:all);


SimpleCalcYYLexer class provides the following methods:

new, YYLex, GetYYLex, StringifySimpleCalcYYLexer

Parser::SimpleCalcYYLexer class is derived from Parser::YYLexer class, which in turn is derived from base class Parser::Lexer that provides all the underlying lexer functionality. SimpleCalcYYLexer class is designed to be used with yyparse code generated by running byacc on a parser defined using parser definition SimpleCalcParser.yy file.

The parser package and token table files, and, are automatically generated from parser grammar definition file, SimpleCalcParser.yy, using byacc available through perl-byacc1.8 modified with perl5-byacc-patches-0.5 for generation of object oriented parser:

byacc -l -P -d -b SimpleCalcParser SimpleCalcParser.yy
mv class implements a lexer for a simple calculator and is provided to highlight usasge of YYLex through yyparse.

The default specification of lexer tokens for includes:

@YYLexerTokensSpec = (
     [ 'LETTER', qr/[a-zA-Z]/ ],
     [ 'NUMBER', qr/\d+/ ],
     [ 'SPACE', qr/[ ]*/,
         sub { my($This, $TokenLabel, $MatchedText) = @_; return ''; }
     [ 'NEWLINE', qr/(?:\r\n|\r|\n)/,
         sub { my($This, $TokenLabel, $MatchedText) = @_; return "\n"; }
     [ 'CHAR', qr/./ ]

The default file containing token identifiers for SimpleCalcParser.yy includes:



$SimpleCalcYYLexer = new Parsers::SimpleCalcYYLexer($Input);

Using specified Input, new method generates a new SimpleCalcYYLexer and returns a reference to newly created SimpleCalcYYLexer object.


# Input string...
$InputText = "3 + 4 +6\nx=3\ny=5\nx+y\nx+z\n";

$YYLexer = new Parsers::SimpleCalcYYLexer($InputText);
$YYLex = $YYLexer->GetYYLex();

$Debug = 0;
$CalcParser = new Parsers::SimpleCalcParser($YYLex,
                         \&Parsers::SimpleCalcParser::yyerror, $Debug);
$Value = $SimpleCalcParser->yyparse();
print "Value: $Value\n";

# Input file...
$InputFile = "Input.txt";
open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n";
$YYLexer = new Parsers::SimpleCalcYYLexer($InputFile);
$YYLex = $YYLexer->GetYYLex();

$CalcParser = new Parsers::SimpleCalcParser($YYLex,
$Value = $SimpleCalcParser->yyparse();
print "Value: $Value\n";

# Input file iterator...
$InputFile = "TestSimpleCalcParser.txt";
open INPUTFILE, "$InputFile" or die "Couldn't open $InputFile: $!\n";
$InputIterator = sub { return <INPUTFILE>; };
$YYLexer = new Parsers::SimpleCalcYYLexer($InputIterator);
$YYLex = $YYLexer->GetYYLex();

$CalcParser = new Parsers::SimpleCalcParser($YYLex,
$Value = $SimpleCalcParser->yyparse();
print "Value: $Value\n";
$YYLexerString = $YYLexer->StringifySimpleCalcYYLexer();

Returns a string containing information about YYLexer object.


Manish Sud

SEE ALSO,, SimpleCalcParser.yy


Copyright (C) 2024 Manish Sud. All rights reserved.

This file is part of MayaChemTools.

MayaChemTools is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.



Previous  TOC  NextJuly 1, 2024Parsers/