Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2006, 3:56 PM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
writing a parser in c++

so i have the scanner ready ... and now i am supposed to write a parser

so i have two questions

1) how does the scanner help the parser? let me get the process straight ... my scanner looks at each character and then returns a token .... so is parser now going to look at that token and somehow store it somewhere and then take the next token and keeps getting the next token until the end of line or something? and then form a parse tree and print it?
and if the expression makes no sense, then print error or something like that?


2) How do I incorporate BNF in the entire process? what do i do with it? is BNF supposed to tell me whether the given expression is valid or not?


(i'm sure i will have lots of doubts when i actually start coding)
programmingnoob is offline   Reply With Quote
Old Apr 26th, 2006, 4:34 PM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Might wish to read a piece on Lex and Yacc. Lex and Yacc are tools used to generate lexical analyzers and parsers.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 26th, 2006, 4:56 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
If the assignment is to write a parse, I am not sure that getting a program to generate one for you would be good enough.

You pretty much seem on track, programmingnoob. The parser takes tokens from the scanner and then processes them to see if there is a valid expression. IMO "Recursive Descent Parsers" are the easiest to write.
The Dark is offline   Reply With Quote
Old Apr 26th, 2006, 8:39 PM   #4
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
Are you just trying to break up a string into substrings by spaces?
b1g4L is offline   Reply With Quote
Old Apr 26th, 2006, 9:35 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Are you just trying to break up a string into substrings by spaces?
Read the post.
__________________
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
DaWei is offline   Reply With Quote
Old Apr 26th, 2006, 11:00 PM   #6
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by The Dark
If the assignment is to write a parse, I am not sure that getting a program to generate one for you would be good enough.

You pretty much seem on track, programmingnoob. The parser takes tokens from the scanner and then processes them to see if there is a valid expression. IMO "Recursive Descent Parsers" are the easiest to write.
sorry! I forgot to mention .. i am supposed to write a recursive descent parser
programmingnoob is offline   Reply With Quote
Old May 26th, 2006, 5:53 PM   #7
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
i fell sick, so i took a break from the assignments
i'm really confused ...
even though i am supposed to generate parser trees, for now, let's suppose i am supposed to create a yes/no parser, meaning it just checks whether the expression is valid or not.
consider the rule below
<compound> ::= begin <stmt-list> end

^^ how would you write code for this particular rule? I believe if you write pseudocode for just one rule, i'll get the idea.

i have the scanner ready, it looks at a stream of text and returns tokens ... please ask me if this is not clear.

also, if you do not want to write the code, then that is totally understandable

(I'm writing a recursive descent parser)
programmingnoob is offline   Reply With Quote
Old May 26th, 2006, 6:14 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
For completeness, you have to have a definition of a statement list, which requires a definition of a statement. The definition of a statement may require additional definitions. Then, if you have one, and only one, 'begin', followed by any number of valid statements, followed by an "end", then the expression is okay. There may be some definition of whether or not zero statements is acceptable. I don't think you'll find that the evaluation of that expression is going to qualify for your assignment, but I may be wrong.
__________________
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
DaWei is offline   Reply With Quote
Old May 26th, 2006, 6:28 PM   #9
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 154
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by DaWei
For completeness, you have to have a definition of a statement list, which requires a definition of a statement. The definition of a statement may require additional definitions. Then, if you have one, and only one, 'begin', followed by any number of valid statements, followed by an "end", then the expression is okay. There may be some definition of whether or not zero statements is acceptable. I don't think you'll find that the evaluation of that expression is going to qualify for your assignment, but I may be wrong.
you're right, but right now, considering i'm so confused, i wanted just to evaluate the expression rather than generate the parser trees, if thats what you were talkin abt.
consider that my program is supposed to have only two rules given below (and only <, <> and integer tokens)
<type> ::= integer
<relation> ::= = | <> | < | >
now, consider the statement below
<55> <>
my scanner would return < as a token, and then return 55 as integer token etc
i think ... the problem i am facing is i dont know how to connect the parser part with my scanner that i already have
programmingnoob is offline   Reply With Quote
Old May 26th, 2006, 6:41 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC