Company Offer Community
Conformance
Misc

Generated on 2024-11-21

Contact

Perl tricks

Cipher Brain has developped some expertise in Perl, and has notes here on some points which where not trivial, and for which documentation was not found easily.

Use of pbyacc

Yacc, and its GNU replacement Bison is a parser generator, i.e. a program to generate some routines which can parse some text and data according to a user-specified grammar.

The basic Yacc documentation is availlable on the net, or with man yacc. We detail here only some point.

yylex

The method yyparse of the object generated by pbyacc takes one argument, which is passed to the function yylex.

The function yylex should return one of:

  • 0: to indicate that there is no tokens left.
  • a literal number: given by ord($c) on a character, to indicate a character literal.
  • a list ($type,$value): where $type is one of the constants corresponding to the defined after the %TOKEN directive of the grammar, and the value is an associated value.

Note that the same argument of yyparse is given as the second argument of yyerror if it is called, the first argument being an error message.

The only two usages of the argument of yyparse are for calling yylex and yyerror, which are written by the user. Therefore, anything can be given: nothing (i.e. undef), a string, an object (like a stream handle,...) or a hash or list reference, to be able to simulate passing more than one value.