7267
|
1 yply.py
|
|
2
|
|
3 This example implements a program yply.py that converts a UNIX-yacc
|
|
4 specification file into a PLY-compatible program. To use, simply
|
|
5 run it like this:
|
|
6
|
|
7 % python yply.py [-nocode] inputfile.y >myparser.py
|
|
8
|
|
9 The output of this program is Python code. In the output,
|
|
10 any C code in the original file is included, but is commented out.
|
|
11 If you use the -nocode option, then all of the C code in the
|
|
12 original file is just discarded.
|
|
13
|
|
14 To use the resulting grammer with PLY, you'll need to edit the
|
|
15 myparser.py file. Within this file, some stub code is included that
|
|
16 can be used to test the construction of the parsing tables. However,
|
|
17 you'll need to do more editing to make a workable parser.
|
|
18
|
|
19 Disclaimer: This just an example I threw together in an afternoon.
|
|
20 It might have some bugs. However, it worked when I tried it on
|
|
21 a yacc-specified C++ parser containing 442 rules and 855 parsing
|
|
22 states.
|
|
23
|
|
24 Comments:
|
|
25
|
|
26 1. This example does not parse specification files meant for lex/flex.
|
|
27 You'll need to specify the tokenizer on your own.
|
|
28
|
|
29 2. This example shows a number of interesting PLY features including
|
|
30
|
|
31 - Parsing of literal text delimited by nested parentheses
|
|
32 - Some interaction between the parser and the lexer.
|
|
33 - Use of literals in the grammar specification
|
|
34 - One pass compilation. The program just emits the result,
|
|
35 there is no intermediate parse tree.
|
|
36
|
|
37 3. This program could probably be cleaned up and enhanced a lot.
|
|
38 It would be great if someone wanted to work on this (hint).
|
|
39
|
|
40 -Dave
|
|
41
|