From: | Raja Mukherji <rajamukherji@gmail.com> |
Newsgroups: | comp.compilers |
Date: | Sun, 16 Nov 2008 14:21:07 -0800 (PST) |
Organization: | Compilers Central |
References: | 08-11-061 |
Keywords: | lex, parse, practice |
Posted-Date: | 16 Nov 2008 17:56:06 EST |
On Nov 15, 5:49 pm, "tuxisthebirdfo...@gmail.com"
<tuxisthebirdfo...@gmail.com> wrote:
> I know most people anymore use lex/yacc or some derivative of these
> tools to create scanner/parser modules for their compiler projects. I
> was wondering if anyone has developed a scanner or parser that they
> personally hand-wrote? ...
I've hand written the scanner/parser for my language Wrapl for a
while. I originally wrote them in Icon, then Modula-2, Modula-3, C and
currently in C++. However they all had the same basic structure, which
you find in many scanner/parsers:
The scanner has 3 main functions:
a method/function "next" which advances the scanner to the next token
a method/function "parse" which checks that the current token is a
particular token or in a set of tokens. If the current token is
"none", call next(). If the current token matches, the current token
is set to "none". Returns true/false if the current token matched or
not. The current token values (e.g. line/column number, integer/string
value) is available from the scanner.
a method/function "accept" which behaves similarly to "parse" but
throws an error if the match fails.
The parser consists of many "parse_???" and "accept_???" functions,
built up from the other parse/accept functions. The trickiest part is
the infix operation parser, since it has to deal with different levels
of precedence. At first I had a different function for each level,
like "parse_expr_NN" where NN was the precedence level. But that
became tedious to modify, so I now use a function "parse_expr(level)"
where level is a parameter and the function contains a switch
statement that switches on the level, each case falling through to
higher precedence expressions.
If you're brave you can find the source for my scanner/parser at
http://wrapl.sf.net/download.html#source; the scanner/parser is in the
dev/src/Lib/Wrapl/Loader/scanner.c and dev/src/Lib/Wrapl/Loader/
parser.c files, but they lack commenting.
Raja
Return to the
comp.compilers page.
Search the
comp.compilers archives again.