Re: A question about a LL parser
- From: thomas.mertes@xxxxxx
- Date: Sun, 27 Apr 2008 06:40:59 -0700 (PDT)
On 27 Apr., 02:45, Chad <cdal...@xxxxxxxxx> wrote:
The question comes from the following URL
http://en.wikipedia.org/wiki/LL_parser
They parse ( 1 + 1 ), using the following grammar rules.
1. S → F
2. S → ( S + F )
3. F → 1
On the first '(' from the input stream and from the 'S' stack, they
apply rule 2. How do they know it's rule 2 and say not rule 1?
I would say: Because rule 2 starts with an '('.
If you want to understand parsing look at how a Pascal
compiler parses a program. Pascal can be parsed with LL(1).
There is something like the current symbol (which is not the
same as a character). A symbol can be an identifier, a literal
one of the reserved words, an operator, a paren, ... Symbols
are read by the scanner (which also has the job of skipping
comments, and processing pragmas).
The parser does all decisions based on the current symbol.
When necessary the parser calls the scanner to get the next
symbol. Pascal uses a recursive descend parser which maps
very closely to the syntax of Pascal.
For example:
The procedure to parse statements looks at the current
symbol and when it is the reserved symbol 'if' it calls the
procedure 'ifstatement'. If a statement does not start with a
reserved word it can be a procedure call or an assignment...
The source of the P4 Pascal compiler can be found here:
http://homepages.cwi.nl/~steven/pascal/
Hope that helps.
Greetings Thomas Mertes
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
.
- References:
- A question about a LL parser
- From: Chad
- A question about a LL parser
- Prev by Date: Re: My hear is turning grey!!
- Next by Date: Re: My hear is turning grey!!
- Previous by thread: A question about a LL parser
- Next by thread: Programming Discussion Forum
- Index(es):
Relevant Pages
|