1Parse::YYLex(3) User Contributed Perl Documentation Parse::YYLex(3)
2
3
4
6 "Parse::YYLex" - Version of Parse::Lex to be used by a byacc parser.
7
9 Parse::Lex requires this perl version:
10 require 5.004;
11 use Parse::YYLex;
12
13 If using a procedural parser:
14 Parse::YYLex->create ...; # exports &yylex and $yylval
15 # see Parse::Lex for the token table args <...>
16 Parse::YYLex::lex->from(\*FH);
17 require 'MyParser.pl'; # generated by byacc
18 yyparse();
19
20 If using an object-oriented parser:
21 $lexer = new Parse::YYLex ...;
22 # see Parse::Lex for the token table args <...>
23 use MyParser; # generated by byacc5
24 $parser = new MyParser($lexer->getyylex, \&yyerror, $debug);
25 # you must write &yyerror
26 $lexer->from(\*STREAM);
27 $parser->yyparse(*STREAM);
28
29 To get the token definitions from MyParser.ph instead of y.tab.ph or to
30 change the skip regexp (default whitespace), do this before calling
31 "new" or "create":
32 Parse::YYLex->ytab('MyParser.ph');
33 Parse::YYLex->skip('');
34
36 Often times you'd use a lexer in conjunction with a parser. And most
37 of the time you'd want to generate that parser with a yacc parser
38 generator. Parse::YYLex is a derivative of Parse::Lex compatible with
39 yacc parsers, by adapting it to the byacc calling conventions:
40
41 · The parser wants to receive integer token types as defined in
42 y.tab.ph instead of the symbolic types that Parse::Lex returns.
43
44 · The parser wants its tokens as two components (type and value),
45 whereas Parse::Lex returns one object with these two components.
46 Furthermore, a procedural parser wants the value stored in a
47 variable $yylval.
48
49 · The parser wants to receive the tokens by calling a yylex function,
50 not an object method. Thus we have to give the parser a curried
51 form of the lexer function, where the self argument is fixed.
52
53 Procedural Parsers
54 Yacc (and Bison) traditionally generate C or C++ parsers. Fortunately,
55 Berkeley yacc has been modified to generate Perl, see
56 ftp://ftp.sterling.com/local/perl-byacc.tar.Z
57
58 Byacc with the -P option generates procedural perl code that is
59 compatible with both perl4 and perl5. (However you cannot use
60 Parse::YYLex with perl4.) Use this variant for quick hacks, as it is
61 more convenient than the one below. In this case "Parse::YYLex-create">
62 instantiates a lexer and exports a &yylex function (the lexer) and a
63 $yylval variable (the token value) to its caller's namespace (which
64 should be the namespace of the parser).
65
66 If you need to call any object methods of the created lexer (see
67 Parse::Lex for documentation), use the $Parse::YYLex::lex variable.
68
69 Object-Oriented Parsers
70 Another byacc modification (I call it byacc5) generates object-oriented
71 Perl5 code:
72 CPAN/authors/id/JAKE/perl5-byacc-patches-0.5.tar.gz
73
74 Use this variant if you need more than one parser, you need
75 flexibility, or you simply like OO. In this case you need to use new,
76 and pass the return value of getyylex (a reference to the curried
77 lexing function) to the parser. The lexing function returns a two-
78 element array, the token type and value.
79
80 Numeric Token Table
81 Yacc parsers insist on using numeric token types, and define these in a
82 file customarily named y.tab.ph. That is where Parse::YYLex will look
83 by default, and the file has to be in the @INC path (which includes the
84 current directory).
85
86 You can specify a different token table before calling "new" or
87 "create":
88 Parse::YYLex->ytab('MyParser.ph');
89
91 "Parse::YYLex" is based on Parse::Lex which requires perl 5.004 and
92 will not work with earlier versions. A slightly different version,
93 Parse::CLex, works with earlier perl versions. It would be easy to
94 allow a choice between Parse::Lex and Parse::CLex, but the latter has
95 some limitations, and presently seems to have some bugs.
96
98 Vladimir Alexiev <vladimir@cs.ualberta.ca>
99
101 byacc(1), Parse::Lex.
102
103
104
105perl v5.32.0 2020-07-28 Parse::YYLex(3)