![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
You would need some more definitions than that, as that only seems to cover how to get from the input to the tokens.
If you have something like: <compound> ::= begin <stmt-list> end Then the recursive descent parser code would look something like: function parseCompound()
{
if (currentToken == begin)
{
getNextToken();
parseStmtList();
if (currentToken != end)
throw error
getNextToken();
}
}You don't need to use throw and catch, you can use error codes instead. |
|
|
|
|
|
#12 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
well ... i have scanner class already coded ... which basically is used to look at a stream and return tokens. now what does the parser do? it looks at the tokens.. cool ... but how? ... also ... it evaluates whether the given expression is a valid one ... but how does it knw when one expression ends and the another one starts? |
|
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
An expression begins and ends when defined begin and end conditions are met, or when an intolerable error (failure to meet expectations) occurs. Consider C/C++ and the 'if' statement. The expression begins with the first '(' after the 'if' and ends with the ')' preceding the first statement. You can see that an improper number of parentheses tokens, or some other things, can bung up the detection of that series of events. The detection of a '{', which is not required in all cases, could help, if it were.
PL/Z, on the other hand was entirely free form, in that anything could continue over any number of lines. That required a greater number of defined tokens. The 'if' expression begin following the 'if' and continued until a 'then' was encountered, wherever that might me. The 'then' block terminated with either an 'else' or a 'fi'. So, on it went. Consider, then, a scientific calculator employing RPN and a stack. Sequence is important, not just tokens. There are many colors of frogs. The workings of your parser, then, depend entirely upon your definitions of what constitutes a what, when it's allowed and when it's not, and whether or not its meaning might change with sequence or context. A whore defined as a saint will never be stoned. Well, that's not true, saints get stoned, but you see what I mean. The accumulation of tokens is merely a small part. You might even have to disburse them in different piles and fetch them in different orders, depending upon YOUR (or your teacher/boss/client's) definition of how things work. I can't think that this post is much help, at all. Perhaps it can stir a few thoughts, but you are going to have to gather your requirements and make some sense of them. Breaking the job into small pieces is a good approach, but it's worthless if you break it into meaningless pieces that have no ultimate applicability.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#14 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
as far as i know, if an expression is not valid, the program is just supposed to tell the user that it's invalid ... thats it, right? |
|
|
|
|
|
|
#15 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If you check your oil, you may catch an error. That's an error code. If you forget about it and the engine throws a rod, that's an error indication you can catch, but it was actually arranged by the designer of the engine. Sort of.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#16 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
http://www.ross.net/funnelweb/refere...er_parser.html
^the link above sorta clears my doubts but it also indicates that i coded my scanner wrong (which i was sorta worried about from the very beginning) what my scanner does is it returns tokens, it also stores them in a hash table called symbol table, but there is no way of telling the order the tokens were stored in the table.... so .... when the main function in main.cpp calls the scanner class and the class returns a tokens ... maybe I should have a function in main.cpp to store the tokens etc in the order they are received, no ?? i have attached my full working code for scanner so that you would know what's going on ... coz i am not good at explaining things ... i try though |
|
|
|
|
|
#17 |
|
Programmer
Join Date: Aug 2005
Location: Norway
Posts: 56
Rep Power: 0
![]() |
I'm also wrting a compiler, but a little different way.
__________________
Heh. |
|
|
|
|
|
#18 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#19 |
|
Programmer
Join Date: Aug 2005
Location: Norway
Posts: 56
Rep Power: 0
![]() |
The scanner breaks the source to tokens. Every single token will be added to a symbol table. The parser will try to assemble tokens to statements, and generate output code of the parsed tokens.
__________________
Heh. |
|
|
|
|
|
#20 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|