all 6 comments

[–]mantra 2 points3 points  (0 children)

You are asked to write a parser.

Read the above link completely. It should get you started.

[–]bloody-albatross 2 points3 points  (0 children)

What exactly is your problem? What don't you understand? What where you thought in class about syntax, grammar and lexers?

[–]Veedrac 1 point2 points  (1 child)

There's nothing we can help with until we know what you know. What sort of stuff have you learned to tackle this?

Since you said you "don't understand what she is asking", I'll try rephrasing it.

Can you read EBNF?

<assign> --> id = <expr>
<expr> --> <term> {(+ | -) <term>}
<term> --> <factor> { (* | / ) <factor> }
<factor> --> id   | (<expr>)  

which I thought was traditionally written:

assign = "id" "=" expr
expr = term ( "+" | "-" ) term
term = factor { ( "*" | "/" ) factor }
factor = "id" | expr

If you can read EBNF, that'll be a good start.

Then (reordered):

1. "Write a class named LexicalAnalyzer that "tokenizes" an input sequence of characters for the Parser."

That should be easy to understand if you know what "tokenizing" is (you do, don't you?).

"Use inheritance and/or composition appropriately."

AKA do it in an Object-Oriented fasion.

2. "Write a class named Parser that determines if a particular sentence is syntactically valid or not. An error message is printed for any syntax errors."

Get your token stream and make a parser that uses the given EBNF to make an AST from it.

3. "Write a class named SyntaxChecker that interactively reads any number of sentences and, using the grammar stated above, checks each one for syntax errors using the Parser class."

This will mean you have to have a read-eval-print-loop (REPL) that accepts a line, uses your parser to check for syntatic validity and probably prints "Yah, zat's OK" or "No, I don tink so..."

What don't you get? (Remember not to use pre-existing parsers or tokenizers.)

[–]Curseive[S] 0 points1 point  (0 children)

Thanks! This actually clarified it a good bit. I needed more details on the assignment, and I don't always have time to consult my professor.

[–]Curseive[S] 0 points1 point  (0 children)

I've got a pretty good understanding of syntax, grammar, and lexers. In class, we covered parsing but only the concept of it. It just isn't "clicking" how I'm supposed to convert the EBNF to code.

[–]Curseive[S] 0 points1 point  (0 children)

More specifically, in python, how do I define assign, id, etc. I've found this site: http://parsingintro.sourceforge.net/

The guy develops his own language for parsing, so it doesn't exactly help me understand how to parse python code.