WLP4 Intermediate (.wlp4i) Format
A .wlp4i file (pronounced woolpy) is a text file containing
a representation of a parse tree for a WLP4 program.
- The first line of the file contains the rule start BOF procedures EOF.
- Following every line containing a production rule, there is one set of lines corresponding to each
symbol (terminal or nonterminal) in its right-hand-side, in order.
(If the right-hand-side of the production rule is
.EMPTY
,
then this set of lines is empty.)
- For a nonterminal, the set of lines begins with a rule whose left-hand-side is the nonterminal; the
remaining lines in the set are defined recursively.
- For a terminal other than BOF or EOF, which represents a token kind,
the set of lines is a singleton containing the token kind, followed by a space,
followed by the token lexeme (the string corresponding to the token).
- For the terminal BOF or EOF, the set of lines is a singleton containing BOF BOF or EOF EOF respectively.
The format can be produced by building a parse tree for the WLP4 program
and performing a preorder traversal of the tree.
For nodes representing terminals, print the token
kind and lexeme (or BOF BOF
or EOF EOF
for those special cases).
For nodes representing nonterminals, print the rule used to expand the
nonterminal, and then process each child subtree recursively
from left to right.
Example
WLP4 program:
int wain(int foo, int bar) { return 42; }
Result of scanning:
INT int
WAIN wain
LPAREN (
INT int
ID foo
COMMA ,
INT int
ID bar
RPAREN )
LBRACE {
RETURN return
NUM 42
SEMI ;
RBRACE }
Result of parsing (.wlp4i file):
start BOF procedures EOF
BOF BOF
procedures main
main INT WAIN LPAREN dcl COMMA dcl RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE
INT int
WAIN wain
LPAREN (
dcl type ID
type INT
INT int
ID foo
COMMA ,
dcl type ID
type INT
INT int
ID bar
RPAREN )
LBRACE {
dcls .EMPTY
statements .EMPTY
RETURN return
expr term
term factor
factor NUM
NUM 42
SEMI ;
RBRACE }
EOF EOF