The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Parse::YYLex - Version of Parse::Lex to be used by a byacc parser.


Parse::Lex requires this perl version: require 5.004; use Parse::YYLex;

If using a procedural parser: Parse::YYLex->create ...; # exports &yylex and $yylval # see Parse::Lex for the token table args <...> Parse::YYLex::lex->from(\*FH); require ''; # generated by byacc yyparse();

If using an object-oriented parser: $lexer = new Parse::YYLex ...; # see Parse::Lex for the token table args <...> use MyParser; # generated by byacc5 $parser = new MyParser($lexer->getyylex, \&yyerror, $debug); # you must write &yyerror $lexer->from(\*STREAM); $parser->yyparse(*STREAM);

To get the token definitions from instead of or to change the skip regexp (default whitespace), do this before calling new or create: Parse::YYLex->ytab(''); Parse::YYLex->skip('');


Often times you'd use a lexer in conjunction with a parser. And most of the time you'd want to generate that parser with a yacc parser generator. Parse::YYLex is a derivative of Parse::Lex compatible with yacc parsers, by adapting it to the byacc calling conventions:

  • The parser wants to receive integer token types as defined in instead of the symbolic types that Parse::Lex returns.

  • The parser wants its tokens as two components (type and value), whereas Parse::Lex returns one object with these two components. Furthermore, a procedural parser wants the value stored in a variable $yylval.

  • The parser wants to receive the tokens by calling a yylex function, not an object method. Thus we have to give the parser a curried form of the lexer function, where the self argument is fixed.

Procedural Parsers

Yacc (and Bison) traditionally generate C or C++ parsers. Fortunately, Berkeley yacc has been modified to generate Perl, see

Byacc with the -P option generates procedural perl code that is compatible with both perl4 and perl5. (However you cannot use Parse::YYLex with perl4.) Use this variant for quick hacks, as it is more convenient than the one below. In this case Parse::YYLex-create> instantiates a lexer and exports a &yylex function (the lexer) and a $yylval variable (the token value) to its caller's namespace (which should be the namespace of the parser).

If you need to call any object methods of the created lexer (see Parse::Lex for documentation), use the $Parse::YYLex::lex variable.

Object-Oriented Parsers

Another byacc modification (I call it byacc5) generates object-oriented Perl5 code: CPAN/authors/id/JAKE/perl5-byacc-patches-0.5.tar.gz

Use this variant if you need more than one parser, you need flexibility, or you simply like OO. In this case you need to use new, and pass the return value of getyylex (a reference to the curried lexing function) to the parser. The lexing function returns a two-element array, the token type and value.

Numeric Token Table

Yacc parsers insist on using numeric token types, and define these in a file customarily named That is where Parse::YYLex will look by default, and the file has to be in the @INC path (which includes the current directory).

You can specify a different token table before calling new or create: Parse::YYLex->ytab('');


Parse::YYLex is based on Parse::Lex which requires perl 5.004 and will not work with earlier versions. A slightly different version, Parse::CLex, works with earlier perl versions. It would be easy to allow a choice between Parse::Lex and Parse::CLex, but the latter has some limitations, and presently seems to have some bugs.


Vladimir Alexiev <>


byacc(1), Parse::Lex.