![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
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) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Programmer
|
Are you just trying to break up a string into substrings by spaces?
|
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
Quote:
![]() |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
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) |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 154
Rep Power: 3
![]() |
Quote:
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 |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
![]()
__________________
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|